claf
claf

Reputation: 9263

Vim : Open multiples files on different lines

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 ...

Upvotes: 19

Views: 2715

Answers (7)

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>'

Upvotes: 1

dragon788
dragon788

Reputation: 3931

There is a vim plugin called file-line that lets you easily do this, though it has a couple issues with opening multiple files to specific lines currently. I like some of the workarounds above, but file-line does work if you want to get a buffer list :ls full of the files with their respective line numbers, and then you could use something like bufdo to open them all in splits.

https://github.com/bogado/file-line

Upvotes: 1

Sooth
Sooth

Reputation: 3114

To go to line 3 on file1.txt and line 4 on file2.txt I do this:

vim -c ":e file1.txt|:3|:e file2.txt|:4"

Upvotes: 1

Morten
Morten

Reputation: 654

I just wrote https://gist.github.com/xim/6123691

Thanks to Pikrass for the idea =)

EDIT:

As claferri says, using '+tabnew ...' limits you to 10 files. Updated the gist to use -S.

The vim function will build a string ($script) containing a vim script that opens the files, at appropriate lines, in tabs. Explaining using an example, this is done by changing vim -R file1:42 file2 file3:1337 to vim -S <script> -R file1 file2 file3 – containing a vim script, in this case:

tab all
tablast
1337
tabprev
tabprev
42

So: we are opening the files normally, then executing the script: Jumping to the last tab, then jumping to the appropriate line for each file while moving towards the first file.

This hack is only moderately tested, comment on any errors.

RE-EDIT:

Fixed the script so even a crazy example like vim -R 1:3 "a file" foo -- -- "some other file":34 -R works as expected.

Upvotes: 5

Neil
Neil

Reputation: 55422

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.

Upvotes: 3

Justin Smith
Justin Smith

Reputation: 2591

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.

Upvotes: 1

Pik&#39;
Pik&#39;

Reputation: 7097

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...

Upvotes: 13

Related Questions