Open Vim to a specific line
Sometimes I find it useful to open a file in Vim from my terminal to a specific line. Perhaps I know the line number in question because a colleague was referencing it. Or maybe I saw it in a stack trace. Vim can do this out of the box:
vim myfile.js +10
– Opens myfile.js, jumps to line 10vim +10 myfile.js
– Same! Argument order doesn’t matter
This alone can be a useful trick. It works everywhere and requires no plugins. Yet it is still one keystroke more than just calling :10
after the file opens to jump to the line.
If you’ve ever read a stack trace, you’ve probably seen file paths and line numbers written like this:
./path/to/some/directory/file.rb:123
You may even see a column position amended like this:
./your/file/path/something.js:10:13
Several solutions exist for opening a file to a specific line and column number when written in these formats. The one I use is called Vim-fetch and it supports both of these formats, and many more. Here’s a gif showing both methods, with and without plugin, in action:
