When I pass the names of multiple files to vim on the command line (e.g. vim foo.txt bar.txt), I almost always want to add the -o option, because I would like them to be visible side-by-side in separate windows. However, I often forget to add -o. I cannot find an appropriate setting, but is there a way to set this as the default? (for example, in my .vimrc?). I know I could probably create a shell alias, but that only works if it is invoked interactively from a shell (not from another program).
- 6,729
- 7
- 28
- 41
-
1Related: How do I make opening new tabs the default? – muru Jun 25 '19 at 09:06
2 Answers
As a solution to the generalized question How can I apply a command-line option every time I open vim? (there are options that don't have configuration equivalents) one approach is as follows.
- Put your personal bin dir (
~/bin) ahead of vim's path in your PATH environment variable. If the bin dir is not already in your path you can use this (in .profile, or .bashrc or whatever you use):PATH=$HOME/bin:$PATH Create a wrapper shell script named
vimin that directory:$ cat > ~/bin/vim <<'EOF' #!/bin/sh exec /path/to/vim -o "$@" EOFPer OP's request I'm using
-oin this example. Obviously you'll need to change/path/toto actual path.Make it runnable:
$ chmod +x ~/bin/vimNow if you run
vim foo barthen "foo" and "bar" each get their own windows.
- 19,834
- 2
- 30
- 57
-
1Thanks @muru that little formatting trick was just what the doctor ordered. – B Layer Jun 26 '19 at 07:10
-
1Thanks. That's reasonable, unfortunately it doesn't quite address scenarios such as diff mode being enabled (which to be fair I didn't ask about, but realised would be an issue once I read the related question!). It would be nicer to have a configuration-based approach. But it's definitely the best answer I've seen so far. – Andrew Ferrier Jun 26 '19 at 10:48
-
Hi @AndrewFerrier I knew this answer wouldn't be what you'd consider ideal but after I came up short on it I become a bit skeptical that there was a configuration solution. Well, at least I could improve on the alias-based solution you mentioned as well as solve the problem generally for all flags with no vimrc analogue. An ideal solution may yet appear and it'll be great if it does. I knew this might be relegated to secondary/alternative status. I'm just glad a number of people seem to have found it useful so far. – B Layer Jun 26 '19 at 15:29
-
BTW, what's the diff mode scenario you mention? If I understand how it breaks down in some scenarios I may be able to improve it a bit. – B Layer Jun 26 '19 at 15:31
A shorter version of the accepted answer is to use aliasing, supported by many shells (for bash, see https://tldp.org/LDP/abs/html/aliases.html). That is, you can simply say
$ alias vim="vim -o"
and your next vim foo.txt bar.txt will open 2 buffers. You can make this permanent by putting the line into your shell init script (~/.bashrc for bash)
- 121
- 1