Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 4 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
add comment

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
add comment

You could do your grepping from Vim itself:

:grep xyz *

then open the quickfix window:

:cw
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.