when I use vim, I often like to open multiple tabs with header and cpp files split on the same tab so that they are side by side and easy to associate with one another.
This is easily achievable by using a combination of :vsp
and :tabe
from within vim, however, I would like a way to do this from the command line prior to opening vim because I usually have a shell script to open all of my project files so that I don't have to do this every time I close my session.
I am aware that the -o
, -O
, and -p
flags exist for vim, however, using these flags opens all files in tabbed or split mode. Combining them does not work the way I want them to. I want to be able to open a mixture of tabs and splits directly from the command line.
Any thoughts on how to achieve this?
The key to this problem is to be found at the same place you found -o
, -O
, and -p
: in $ vim --help
:
-c <command> Execute <command> after loading the first file
The idea is to do in your shell what you would do manually in Vim:
$ vim file1.cpp -c "vsplit file1.h" -c "tabedit file2.cpp" -c "vsplit file2.h"
There are other approaches possible, like putting all those commands together in a script and sourcing it at startup:
" in myscript.vim
:vsplit file1.h
:tabedit file2.cpp
:vsplit file2.h
" in your shell
$ vim file1.cpp -S myscript.vim
Or you could open every *.cpp
file in a tab page with -p
and then loop through them to open the corresponding header:
vim -p file1.cpp file2.cpp -c "tabdo vsplit %<.h"
All the examples above have the same outcome: