11

If I open vim with multiple files on the command line, for example vim debug.log* which loads debug.log, debug.log.1 and debug.log.2, can I navigate between them, assign them to multiple windows (:split/:vsplit) or such?

The only shortcut I know for this mode of operation is :n, which loads the next file on the list. What more can I do in this multi-file operation mode? (or which help subject covers that?)

1
  • 3
    :previous (or :prev) is the inverse of :n, and goes back a file.
    – Rich
    Feb 10, 2015 at 11:12

5 Answers 5

13

The most relevant help topic is :ls, also known as :files or :buffers.

In your vim debug.log* example, if you gave the :ls command, you would see:

:ls
  1 %a   "debug.log"                    line 1
  2      "debug.log.1"                  line 0
  3      "debug.log.2"                  line 0
Press ENTER or type command to continue

… indicating that the first one is the active buffer.

To open debug.log.2 in the current window, give the :b 3 or :b debug.log.2 command to show buffer 3.

To split the window and open buffer 2 in it, give the :sb 2 command. The vertical-split version of that is :vert sb 2.

There are many variants of the :b command; browse the help topics cited here to get a feel for them.


In addition, you can invoke vim -o debug.log* to open all of the files, each in its own split, or vim -O debug.log* to open them each in a vertical split.

7

You can also open vim with different parameters.

Opens files in tabs:

vim -p debug.log*

Opens files in vertical splits:

vim -o debug.log*

There is a cool and quite powerful plugin called CtrlSpace:

https://github.com/szw/vim-ctrlspace

It is very handy if you need to work with many files. Simply you can imagine ctrl-space as own buffer list for every tab. It provides very easy way to navigate between the files, as the author states:

Vim-CtrlSpace is a Vim plugin to control and organize your working space in Vim. Including managing buffers, files, tabs, sessions, and finally projects. Its approach is somewhat unique, but certainly you will feel at home, especially if you use already split windows, tabs, buffers, etc.

2
6

In addition to the raw vim functionality mentioned by 200_success, Vim's ecosystem is full of plugins to provide extra functionality, such as:

However, before you go and start using plugins, it's best to fully understand buffers and windows in vim.

1
5

If you want to open a particular file without needing to go through :ls, you can use any of the following:

  • :vs filename opens the file in a vertical split
  • :sp filename does the same, but in a horizontal split
  • :tabe filename opens in a new tab

It's worth knowing, too, that if you don't specify a filename, you will open a new split/tab of the current file, which can be handy for long files.

2
  • 2
    :t is the copy command. For :tabedit you need at least :tabe.
    – tommcdo
    Feb 9, 2015 at 11:19
  • Oh dang, you're totally right. Thanks, I'll edit!
    – Tom
    Feb 9, 2015 at 23:05
2

There are few useful shortcuts for navigating multiple files:

  • CTRL-^ (where N is the buffer number) to jump to the previously edited file.
  • N CTRL-^ (where N is the buffer number) to jump to specific buffer.

    For example:

    • 1Control+^ - go to 1st buffer
    • 2Control+^ - go to 2nd buffer
    • etc.

    This is equivalent to :buffer N.

  • Use :Ex/:Sex/:Vex to navigate the current directory.

  • Use :tabs instead (vim -p) with some custom shortcuts in your .vimrc such as:

    " TABS
    " To create a new tab (Control-T)
    nnoremap <C-t> :tabnew<CR>
    inoremap <C-t> <Esc>:tabnew<CR>
    " Tab Navigation (Control+Left <-> Control-Right, Shift+H <-> Shift+L).
    nnoremap <S-h> gT
    nnoremap <S-l> gt
    nnoremap <A-Left> gT
    nnoremap <A-Right> gt
    nnoremap <C-S-Tab> gT
    nnoremap <C-Tab> gt
    " Tab Close (Control-W)
    nnoremap <C-BS> :tabclose<CR>
    inoremap <C-BS> <Esc>:tabclose<CR>
    " Close All (Control-Q)
    nnoremap <C-q> :qa<CR>
    inoremap <C-q> <Esc>:qa<CR>
    
  • Use :ba/:vert ba to split, jump into selected buffer via Control+w,w/W, then make it the current window (:on).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.