31

Several tools such as grep, py.test, etc ... use the pattern <FileName>:<line number>: to point to errors. For example:

; grep -Hn Common setup.cfg
setup.cfg:11:    Common

How can I modify vim and gvim so that I can invoke them like so:

gvim setup.cfg:11:

instead of

gvim setup.cfg +11

I know that I can write a small shell script that would parse things but I wonder if there is an easier way.

2
  • 2
    This isn't exactly analogous, but vim does have a feature to load the output from grep or (for example) compiler errors from make, and navigate between them. See :help quickfix for more info.
    – Random832
    Oct 5, 2012 at 12:48
  • better-gf is for the similar use case of needing to do gf on a (possibly relative, possibly including line and column number) reference to a file, whether in an embedded terminal or in any kind of buffer. Sep 5, 2023 at 8:12

4 Answers 4

20

You can use the file:line plugin available here, which does exactly what you want...

2
  • 1
    It sure does and with the added bonus that you can use vundle to install it. Jun 25, 2015 at 6:53
  • 3
    The references file-line plugin has some issues, including brokeness when tab pages are in use. vim-fetch does what file-line does, but better.
    – Lekensteyn
    Jul 16, 2015 at 9:06
9

There isn't a way, and I think a script is the only way. The reason being, what if you had a file called setup.cfg:11 and wanted to edit it?

Here is a quick script that does what you want as a oneliner...

editline() { vim ${1%%:*} +${1##*:}; }
2
  • Sorry this is not accurate: What if you had a file called +11? You can, of course, use -- for that. Oct 5, 2012 at 7:44
  • 3
    You can edit a +11 file by saying vim ./+11
    – Julian
    Oct 5, 2012 at 7:47
3

With Vundle.vim, add this to your .vimrc:

Plugin 'bogado/file-line'
2
  • @Sardathrion: Which issues do you have?
    – Dorian
    Oct 14, 2016 at 0:13
  • I moved to neovim and there are lots of issues that mark other modules (or whatever neovim calls them) as incompatible. Oct 14, 2016 at 6:50
0

You can also use such alias:

alias gd 'set number=`echo \!:1 | perl -pe "s/([0-9]*:)?(.+)[,:]([0-9]+).*/\3/"`; set file=`echo \!:1 | perl -pe "s/([0-9]*:)?(.+)[,:]([0-9]+).*/\2/"`; eval "if ($number == $file) then \\
   gvim $file \\
else \\
   gvim $file +$number \\
endif"'

You must log in to answer this question.

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