I am trying to automate some manual labor, using a bash script. Basically I open multiple files with Vim in 1 terminal-window (and only one terminal-tab), using several splits, vsplits and tabs. I will now describe a general case to explain what I usually do manually.
I use 3 tabs (referring from here on as A, B and C) and I open 4 files in each tab in a 2x2 layout:
bash:~$ cd ~/Workspace/
bash:~/Workspace$ vim A11.m
:vsplit A12.m
:hsplit A22.m
"move cursor to A12.m"
:hsplit A21.m
:tabedit B11.m
:vsplit B12.m
:hsplit B22.m
"move cursor to B12.m"
:hsplit B21.m
:tabedit C11.m
:vsplit C12.m
:hsplit C22.m
"move cursor to C12.m"
:hsplit C21.m
What I would like to create is a shell script into which the file names and location(s) are hard-coded, which upon execution would do all the above. Can anyone suggest to me an approach which would make this possible (if possible at all)? Thanks in advance!
ps: In my .vimrc I have added some configurations such that :split opens the new file beneath (instead of above) the current file and :vsplit opens the new file to the right (instead of to the left).
What about :help mksession
?
Set up all your tabs and windows.
Do :mks /path/to/mysession.vim
to save the current settings and layout into /path/to/mysession.vim
.
Quit Vim.
Open Vim with $ vim -S /path/to/mysession.vim
to restore your workspace.
You can also restore your session with $ vim -c "so /path/to/mysession.vim"
or, when Vim is already runing, with :so /path/to/mysession.vim
.
There are at least four ways to do this:
vim -s <(echo $':vsplit A12.m\n:hsplit A22.m\n…')
vim -S <(echo $'vsplit A12.m\nhsplit A22.m\n…')
it is similar to what @romainl, but you create the script by yourself and do not use any file besides the bash script itself (can be as well just an alias or a line in history). It is possible to use HERE-strings in bash, they are more readable:
vim -S <(cat <<<EOF
vsplit A12.m
hsplit A22.m
…
EOF
)
vim -c 'vsplit A12.m | hsplit A22.m | …'
You can have multiple -c
keys which are run in sequence, but not more then ten thus you have to join commands in one -c
with bar.
Instead of a bash script use vim one:
#!/usr/bin/vim -S
vsplit A12.m
hsplit A22.m
…
. Then you can chmod +x
it and run like any other executed file. Vim treats #!
as a comment leader just for that reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With