Question

I have some $somePaths array of 4 folders. I want to open some files from this folders in VIM. The following opens them in tabs.

vim -p `for i in ${somePaths[@];}; do echo $i/src/main.cpp; done`

Actually I'd like to have those files in split windows (cross-like). How it can be done?

Was it helpful?

Solution

Apart from -p, Vim also offers the -o and -O command-line arguments for horizontal / vertical splits. Unfortunately, they cannot be mixed. To build you own custom window layout, you have to pass the explicit window placement commands via -c. This example

 $ vim 1 -c 'bel vsplit 2' -c '1wincmd w' -c 'bel split 3' -c '3wincmd w' -c 'bel split 4'

creates a layout that looks like this:

+-----------+-----------+
|           |           |
|           |           |
|           |           |
|1          |2          |
+-----------+-----------+
|           |           |
|           |           |
|           |           |
|3          |4          |
+-----------+-----------+

To keep passing the list of files as one block, you can use the fact that the buffer numbers increase monotonically, and refer to buffer numbers in the command:

$ vim -c 'bel vert sbuf 2' -c '1wincmd w' -c 'bel sbuf 3' -c '3wincmd w' -c 'bel sbuf 4' a b c d

OTHER TIPS

vim has :vertical command, which could be useful in your case. give this a try:

vim +'vertical all' [your file list]

You can try using -O4 instead of -p.

The accepted solution posted above is good and solves your use case, however I would like to point out an alternate way of achieving something similar without much effort or configuration.

VIM has inbuilt support for record sessions, :h :mksession, what that allows you to do is save the current vim session (open files, splits, tabs, windows, etc, depending on the value of :h 'sessionoptions'. Although record and maintaining sessions can be slightly tedious, what I'd suggest is use either of xolox/vim-session (I've used it previously) or tpope/vim-obsession (use it now). That will allow you to re-open a session exactly as you left off!

Here is what I do, I have this snippet in my ~/.zshrc:

function vim() {
  tmux rename-window "vim - ${PWD##*/}"
  if test $# -gt 0; then
    env vim --servername ${PWD##*/} "$@"
  elif test -f ~/.vim/sessions/${PWD##*/}.vim; then
    env vim --servername ${PWD##*/} -S ~/.vim/sessions/${PWD##*/}.vim
  else
    env vim --servername ${PWD##*/} -c Obsession\ ~/.vim/sessions/${PWD##*/}.vim
  fi
}

What that does basically is check if vim is launched as it is without any arguments and checks if a session already exists & loads it, or using tpope/vim-obsession starts recording a new session, which will be loaded the next time you open vim. The session name is just the name of the directory you launch vim in. If you do pass any arguments to vim however it would behave like you'd expect and doesn't bother worrying about sessions.

This way, I can now just launch vim in any directory and first time I do so, it will start recording a new session, whereas on subsequent invocations it will load that session and tpope/vim-obsession keeps it updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top