4

I know that a single file can be opened at a different line using:

vim foo.c +123
vim +123 foo.c # same result

Multiple files can be opened using:

vim foo.c bar.c

But how can I combine these two actions? vim +123 foo.c +456 bar.c does not work, bar.c is opened at line one instead of 456.

I saw Vim : Open multiples files on different lines, but this opens different tabs while I am used to :prev and :n for navigating.

Community
  • 1
  • 1
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • Hmm, looking at [this Q/A](http://stackoverflow.com/q/53664/427545), tabs seems to be faster. Anyway, is there a faster way to open files at given lines? – Lekensteyn Mar 12 '13 at 15:00
  • From looking at the link you posted, it does not open the files in tabs but loads the second file into a new split window. If you only need a new buffer, you could try `vim +6 file1 +"e +3 file2"`. – Michael Härtl Mar 12 '13 at 15:06
  • Nope, `view +10 action-chain.c +'e +5 empathy-camera-monitor.c'` did not work. And wouldn't `e` just overwrite the current buffer? – Lekensteyn Mar 12 '13 at 15:09
  • `:e` loads a file into a new buffer. And the above works for me. Make sure you have `set hidden` in your `.vimrc`, which allows to change the buffer. – Michael Härtl Mar 12 '13 at 15:15
  • No luck unfortunately, even with `set hidden`, it complains that there is no next file. (using `:n` to navigate). Using 7.3.798 – Lekensteyn Mar 12 '13 at 15:39
  • If you open vim with one file, can you edit another file with `:e file2.txt`? Can you still navigate to both buffers? If not, i wonder a bit, how you open multiple files with your vim configuration at all. – Michael Härtl Mar 12 '13 at 15:44
  • Yes I am able to use `:e foo` to open `foo` in the current buffer. No change with `set hidden` – Lekensteyn Mar 12 '13 at 15:52
  • Not sure, why `:e file2.txt` does not open the file in a new buffer for you. It always did for me. But for your original problem, how about this: `vim +123 foo.c +'badd +456 bar.c'` – Michael Härtl Mar 12 '13 at 15:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26044/discussion-between-lekensteyn-and-michael-hartl) – Lekensteyn Mar 12 '13 at 16:06

3 Answers3

1

I don't know if there's an easier way, but here's a workaround that is very simple and won't affect your workflow because you're not a tab user.

  1. First, set up an auto command in your .vimrc to issue tabo[nly] (which closes all other tabs) immediately after you enter vim.

    autocmd VimEnter * tabo
    
  2. Next, use the solution in your linked question to open files in multiple tabs with

    vim +123 foo.c +"tabnew +456 bar.c"
    

So what happens is that behind the scenes, vim opens the files in tabs, but immediately closes everything but the last. All the opened files should be available in your open buffers.

This works fine even if you open vim normally without multiple files (you'll see a harmless message — "Already only one tab page" — which disappears on the next key stroke). Also, since this command is issued only on VimEnter, it does not prevent you from using tabs (if you wish to) later on in the session.

abcd
  • 41,765
  • 7
  • 81
  • 98
1

With the file:line - Allows you to open file:line and it does the right thing plugin, you can use

$ vim foo.c:123 bar.c:456

It doesn't work with -o splits, but it does for your use case of presetting the line numbers for files in the argument list.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 1
    I'm currently using [vim-fetch](https://github.com/kopischke/vim-fetch) which is more maintained than the linked file_line plugin. What are you using now? – Lekensteyn Oct 04 '15 at 20:12
  • @Lekensteyn I'm still with file:line; the plugin is so simple (and Vim so stable) that there's no maintenance necessary. – Ingo Karkat Oct 10 '15 at 18:57
0

Besides the file:line plugin, as Ingo suggested, I just made a bash function hack at https://gist.github.com/xim/6123691 – as an answer to Vim : Open multiples files on different lines.

file:line appears not to handle file names with spaces and other special chars, and didn't open my files in tabs when using vim -p – It may be a better option for you though, Lekensteyn, as it doesn't force you to tab the files.

Community
  • 1
  • 1
Morten
  • 634
  • 5
  • 13