1

For example, I want to write a shell script to use vim to open 5 files. In first tab show only the first file, in second tab the rest 4 files shows in left upper corner, right upper corner, left lower corner, right lower corner accordingly. And the current tab is in the first tab. How to do this? If I can do this with one line command like

vim -option file -option

it will be nice. But I tried a little bit and failed to do so. Do you have any suggestions on that?


PS. I know how to edit multiple files in vim. I kind of do this everyday. What I'm trying to do now is to open multiple files simultaneously from bash. And yes I know the -p and -O and -o options of vim in bash. But they kind of open file iteratively in a same manner, which doesn't suit my requirement. What I am seeking is the way of building shell script to satisfy the requirement that specified above. Thanks.

Chong
  • 933
  • 2
  • 10
  • 28

2 Answers2

4

You can put those configuration in a customized script file, then optionally use that script file to open your workspace configuration.

Script file:

:bl
:bf
:tabe
:n
:split
:vsplit
:wincmd l
:n
:wincmd j
:n
:n
:vsplit
:wincmd l
:n
:wincmd h
:wincmd k
:tabn

And you can call it as such:

vim -S script.vim file1.txt file2.txt file3.txt file4.txt file5.txt

This will open vim on file1.txt, with an open tab next to it, according to your configuration, and put the cursor on the upper left file (file2.txt) when you switch to it.

Added explanation of each line:

Note that when we split, the cursor will stay on the original window, and that each window will show different files, which can be navigated independently using :n and :N. When a new window is created, it will display the same file as the window we're in when we created the window.

As noted in my comment, the first two lines is to tell VIM that we have read every file, so VIM won't complain "4 more files to edit" at the end of the session.

:bl       Go to last file
:bf       Go to first file
:tabe     Create new tab
:n        Open next file (file2.txt)
:split    Split horizontally (will create new window below with the content of file2.txt)
:vsplit   Split vertically (will create new window on the top right with the content of file2.txt)
:wincmd l Go to the window to the right (which currently displays file2.txt)
:n        Open next file (file3.txt)
:wincmd j Go to window at the bottom
:n        Open next file (file3.txt, because bottom window was displaying file2.txt)
:n        Open next file (file4.txt)
:vsplit   Split vertically (will create new window on the right with content file4.txt)
:wincmd l Go to window to the right (which is bottom right)
:n        Open next file (file5.txt, because bottom-right window was displaying file4.txt)
:wincmd h Go to window to the left
:wincmd k Go to window above (this sets the cursor on file2.txt on top left)
:tabn     Go to next tab (the first one, displaying file1.txt)
justhalf
  • 8,960
  • 3
  • 47
  • 74
  • So it will read a next file from your bash command automatically at the command tabe split and esplit etc. in script? – Chong Nov 15 '13 at 08:01
  • 1
    Yes. I used `:n` in the script to navigate through the multiple files in the tabs, and display the file you wanted in each tab. The `:bl :bf` in the beginning is to tell vim that we have read every file, so it won't complain "4 more files to edit" at the end of your session. – justhalf Nov 15 '13 at 08:11
1

According to OP it should be done from the shell, in this exact same order:

$vim -c ":edit first|:tabnew second|:vsplit third|:split fourth|:wincmd w|:split fifth"

You can add a final |:tabnext if you want to being from the first tab after Vim launches.

Notice it's also a bit shorter and more readable (purposeful).

Replacing fixed names (first, second, ...) with shell variables will keep the same workflow across many other projects.

Enjoy.

Mario G.
  • 99
  • 1
  • 6