Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often use grep with the -n option to list line numbers, e.g.

$ grep -n xyz .*
/ext1/acheong/foo/bar/a.cc:42:    inline bool abc(int xyz);
/ext1/acheong/foo/bar/a.cc:43:    inline bool def(int xyz);
/ext1/acheong/foo/bar/b.cc:415:            int xyz = result;
/ext1/acheong/foo/bar/b.cc:490:                xyz += result;
$

Would it be possible to alias vim in such a way that doing

vim /ext1/acheong/foo/bar/b.cc:415

would open the file and seek line 415 automatically?

Is it possible in csh? (Yes, yes, csh brings out the devil and all that is unholy; but I've no choice...) An answer for other shells would be useful, too.

share|improve this question
up vote 5 down vote accepted

Well, it's not an alias, you can use vim like this:

vim +<LineNumberHere> fileName

So, for example

vim +150 .bash_history

opens your .bash_history file (for bash), and navigates to line 150.

Incidentally, you can also do this with search terms. For example

vim +/MyTerm MyFile

opens MyFile and navigates to the first occurrence of MyTerm from the top.

Enjoy!

share|improve this answer
1  
that should be "vim +/MyTerm MyFile" – Uwe Apr 19 '13 at 20:32
    
thanks for this – sdjuan Apr 20 '13 at 6:44
    
Thanks, I updated the text. – David Moye Apr 21 '13 at 15:07
    
Apologies for the late accept; I'd been wanting to make a csh alias that would map to the above form, but never got around to it. – Andrew Cheong Aug 27 '13 at 19:36
    
this is great. It works with commands inside vim too: :tabe +150 file, :tabe +/FooBar file – Dvir Azulay Jul 8 '15 at 10:19

One solution to this is to use the file_line.vim plugin, which lets you specify a file name and line number as an argument to Vim or on Vim's command line, just as you've shown.

Another is this script,

$ cat $(which vimgrep)
#!/bin/bash

tmp=$(mktemp)

cat > $tmp
exec < /dev/tty
vim --cmd 'let &efm=&gfm' -q $tmp "$@"
rm $tmp

which can be used like this:

$ grep -Hn xyz .* | vimgrep

and which loads all the matches into Vim's quickfix error list. See

:help quickfix.txt

Note the -H option to grep to ensure that the file name is included in grep's output even if .* expands to only one file name.

share|improve this answer
    
Due to the ad appearing right at the bottom of my screenheight for this thread, I never even saw this answer until today; apologies. I've already accepted the other answer, but Karma is on its way :) – Andrew Cheong Aug 27 '13 at 19:38

You could do your grepping from Vim itself:

:grep xyz *

then open the quickfix window:

:cw
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .