19

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

claf
  • 9,043
  • 17
  • 62
  • 79

7 Answers7

13

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

Pik'
  • 6,819
  • 1
  • 28
  • 24
  • that does work! great, now i just have to figure how to use xargs to go from a list of file1 +10 file2 +14 ... to vim +10 file1 +"tabnew +14 file2" +"tabnew +... (yes i prefere tabnew :) i'll post my cmd line if i have enought faith to write it ;) thx! – claf Jan 27 '10 at 18:40
  • In fact, the bad thing is that this solution doesn't allow more than something like 10 files max at the same time. I'm using a command to get filenames and line and using xargs to pass them to vim. I'd like to be able to launch as much file as I want, i'm disappointed that vim can't handle more than 10 files to open at the same time ... – claf Jan 27 '10 at 18:57
  • Maybe you could use a `for` in the ViM command, whith a list of filenames and another for the cursor positions. – Pik' Jan 27 '10 at 19:23
5

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.

Morten
  • 634
  • 5
  • 13
3

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.

Neil
  • 54,642
  • 8
  • 60
  • 72
1

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.

Justin Smith
  • 2,503
  • 14
  • 12
1

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"
Sooth
  • 2,834
  • 23
  • 26
1

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

dragon788
  • 3,583
  • 1
  • 40
  • 49
1

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