Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim interpret argument with colon(s) as filename:line:column

Tags:

vim

Is it possible to configure VIM in a such way that if I type

vim filename:123:89

it opens file filename goes to line 123 and column 89?

If not through VIM maybe with a hack for the shell?

like image 561
matec Avatar asked Nov 04 '14 11:11

matec


2 Answers

You can install the file-line plugin to open a file to the line and column specified after the filename. (github mirror)

From the Readme on github

When you open a file:line, for instance when coping and pasting from an error from your compiler vim tries to open a file with a colon in its name.

Examples:

vim index.html:20 
vim app/models/user.rb:1337

With this little script in your plugins folder if the stuff after the colon is a number and a file exists with the name especified before the colon vim will open this file and take you to the line you wished in the first place.

like image 113
FDinoff Avatar answered Sep 28 '22 08:09

FDinoff


I'm not sure how to skip to the column, but I've wanted the same feature for ages, so I just hacked up the "jump to line" functionality. In your .bashrc, set

VIM=$(which vim)

function vim {
    local args
    IFS=':' read -a args <<< "$1"
    "$VIM" "${args[0]}" +0"${args[1]}"
}

This splits the argument to Vim by :, then constructs a command line of the form

vim <filename> +0<line>

The +0 is a hack to make sure the default line number is zero.

(If you're not using Bash, you can adapt this into a script and put it in your path, or translate it to your favorite shell language. To edit filename:with:colons, use $VIM.)

like image 43
Fred Foo Avatar answered Sep 28 '22 10:09

Fred Foo