开发者

Vim Split window when opening multiple files

开发者 https://www.devze.com 2023-04-05 07:43 出处:网络
I would like to put something into my vimrc so that if I open 2 files they automatically open in separate split windows. I don\'t want to start it with -o because I sometimes open a lot of files at on

I would like to put something into my vimrc so that if I open 2 files they automatically open in separate split windows. I don't want to start it with -o because I sometimes open a lot of files at once and having 15+ splits would not work very good. so I would like some logic that checks how many files were open and if it is 2 to put each one into its own split window.

Thanks

PS I will most likely use this most often to view the .cc and .hh files of a c++ pro开发者_StackOverflowject.


The simplest way would be to add

if argc() == 2
  silent all
endif

to your .vimrc.


I accomplish the same thing, from the command-line alone, by adding to my vim command -c 'split | bn'. I can open as many files as I like in the same command; -c tells it to run a command, then in the single-quotes, 'split | bn' tells vim to split the window, then switch to the next buffer. So if I type, for example:

gvim foo1.cpp foo2.cpp foo3.cpp foo4.cpp -c 'split | bn'

Then gvim opens with the first two files, foo1.cpp and foo2.cpp, in the split windows. I like this way because I don't have to modify my .vimrc (so I can use it on any machine), and it's still concise enough for the command-line.

Note: I've only tested this in gvim, not vim in a terminal.


Maybe you can try

vim -o2 file1 file2 file3...

I know it isn't perfect solution to you (in this case vim opens everytime two windows).

But you can write (and use) a small script like this

if [ $# -ge 2 ];
    vim -o2 $*
else
    vim $*
fi

Almost perfect ;)


Perhaps could set up something in shell script like this:

#!/bin/bash
if [[ "$#" -eq "2" ]]
then
  /usr/bin/vim -o $1 $2 
else
  /usr/bin/vim $*
fi

If you are feeling a bit fancy then you could do something more complex, by looping through the parameters and checking if any are options and ignoring them.

0

精彩评论

暂无评论...
验证码 换一张
取 消