Native
With native Vim there's no pretty way to do it. You'll have to submit a couple Ex commands using the -c
or +
command-line flag:
vim +123 ~/.zshrc -c 'e ~/.profile | 10'
That is, after startup the command :e ~/.profile
will open ~/.profile
and the next command :10
will take you to line 10.
You can, of course, do this with as many files as you'd like.
One thing worth noting is that after startup the current buffer will be the last one specified on the command line rather than the first as is normally the case. As a workaround you can add -c first
to the end of the command line (as is done in the wrapper script, below). For example:
vim foo.txt +10 -c 'e bar.txt | 20' -c 'e baz.txt | 30' -c first
The current buffer in this case will be foo.txt
(with the cursor on line 10).
Plugin
If you don't mind installing a plugin you can use vim-fetch which allows a command line like:
vim ~/.zshrc:123 ~/.profile:10
It works but it's a little wonky, IMOin my opinion. It seems that it loads a (non-existent) file named, for example, ~/zshrc:123
and only upon navigating to that buffer does the plugin parse out the line number and load the actual file. You may or may not notice this happening. (I noticed it because I saw the temporary file:line
name in my tab line.)
Wrapper
Before I found that plugin I had whipped up a wrapper script for Vim that uses the same parameter format. You'd call it instead of VimThe script name is substituted for vim
. (If you really wanted to you could name it vim
and put it in your PATH
ahead of Vim itself.) I'm posting it just in case anyone's interested:
#!/bin/bash
for arg; do
# If an existing, regular file is followed by ':' and a number...
if [[ $arg =~ ^[^:]*:[[:digit:]]+$ && -f ${arg%%:*} ]]; then
cmds+=(-c "e ${arg%%:*} | ${arg##*:}")
else
other+=($arg)
fi
done
/bin/vim "${other[@]}" "${cmds[@]}" -c first
This was just a quick thing I did for funthis quickly as a little Bash exercise so I'm sure it'sit may not 100% bulletproofwork perfectly for every conceivable set of command line arguments...caveat emptor. :)