Vim : Open multiples files on different lines

Viewed 2425

I was wondering if there's a way to open multiples files with Vim, but each file on a specific line number. I explain :

I often use the syntax : vim my/way/too/far/file +120 in order to edit this file at line 120 because gcc told me too :)

Now what i'm looking for is a way to do this for multiples files at the same time!

Of course, vim file1 +xx file2 +xx ... won't work (the + option only affect first file ... don't ask me why)

So if anyone know a way to fix this? I didn't found it in the manpage...

By the way, sometimes, file1 maybe the same file as file2 ...

7 Answers

Here's a way : vim +6 file1 +"sp +3 file2". Change sp to tabnew if you prefer tabs.

But it would be really useful only if someone could make a script with it...

vim can read your gcc output and create a quickfix list that allows you to navigate easily though all the errors in your code. You can read an existing error file using vim -q or if your project uses a Makefile then you can use the :make command to execute make from within vim and capture its output.

Another option would be using a script like utl, automatically creating a file full of hyperlinks to the file / line numbers based on the output of gcc (this should be trivial with sed).

A link would be formatted like this with utl: <url:error.c#line=10>

EDIT: linked to a more appropriate vim linking script.

Here's a command-line only solution (no plugins needed) that will also work with ':n' and ':prev' (which the :e solution does not) and does not require tabs or splits:

vim filea fileb filec -c ':10|:bu 2|:100|:bu 3|:200|:rewind'

This will open with buffers for filea, fileb, filec, and will be starting on filea at line 10, then ':n' will go to fileb at line 100, and then filec at line 200. The only downfall is that it prints out all the buffer names as messages upon opening all the files and you'll have to hit 'enter' to continue (though setting 'shortmsg=a' may help)

I tried using ':badd' (buffer add) which allows specifying line numbers, but then the buffers aren't loaded and there doesn't seem to be a way to force them to load so that ':n' works instead of just 'bu <num>'

Related