How can I edit multiple files in Vim?

314,222

Solution 1

First of all, in vim you can enter : (colon) and then help help, ala :help for a list of self-help topics, including a short tutorial. Within the list of topics, move your cursor over the topic of interest and then press ctrl] and that topic will be opened.

A good place for you to start would be the topic

|usr_07.txt|  Editing more than one file

Ok, on to your answer.

After starting vim with a list of files, you can move to the next file by entering :next or :n for short. :wnext is short for write current changes and then move to next file; :wn is an abbreviation for :wnext.

There's also an analogous :previous, :wprevious and :Next. (Note that :p is shorthand for :print. The shorthand for :previous is :prev or :N.)

To see where you are in the file list, enter :args and the file currently being edited will appear in [] (brackets).

Example:

vim foo.txt bar.txt
:args

result:

[foo.txt] bar.txt

Solution 2

you can open another file while vim is open with :tabe filename and to switch to the other file you type :tabn or :tabp for next and previous accordingly.

The keyboard shortcuts gT and gt can also be used to switch tabs when you are not in editing mode (i.e. not in insert, replace etc modes). On some systems Ctrl+Alt+Page Up and Ctrl+Alt+Page Down also allow tab-switching, but this does not always work (for example, it won't work in the OS X terminal 'out of the box').

And you can see the filename at the top of the vim app.

Solution 3

Commands to switch between buffers:

:bf            # Go to first file.
:bl            # Go to last file
:bn            # Go to next file.
:bp            # Go to previous file.
:bw            # Close file.

:help buffer to find more information

To know filename use Ctrl+G,:file or :f

Solution 4

Another option apart from the answers given, is to split the window with:

:sp
:vsp

:vsp is for vertical split. Then use Ctrl+W <ARROW_KEYS> to move in panes.

Solution 5

:n -> Move to next file
:N -> Move to previous file
Share:
314,222

Related videos on Youtube

cwd
Author by

cwd

Updated on September 18, 2022

Comments

  • cwd
    cwd over 1 year

    I know I can open multiple files with vim by doing something like vim 2011-12*.log, but how can I switch between files and close the files one at a time?

    Also, how can I tell the file name of the current file that I'm editing?

  • Not Now
    Not Now over 12 years
    This. Also open files in tabs with: vim -p file1 file2
  • Sasquiha
    Sasquiha over 12 years
    You can also do :rew to get back to the first file. :e# swaps between 2 files. :n! goes to next file without writing. Also look at :set autowrite.
  • Tomek
    Tomek almost 12 years
    You can use :vs instead of :vsp
  • KillerSpaz
    KillerSpaz about 11 years
    With respect to multiple buffers / windows / tabs also see StackOverflow's How to effectively work with multiple files in Vim? and Using Vim's tabs like buffers.
  • me_and
    me_and over 10 years
    AMAZE! I'd never come across this before, despite having used vim for years. This changes everything.
  • Ruslan
    Ruslan over 9 years
    Or even just Ctrl+W, v for vertical split and Ctrl+W, s for horizontal split.
  • David Braverman
    David Braverman over 8 years
    Note that tabs are not intended to work with multiple files. They are intended to work with multiple layouts. See article.
  • Арсений Черенков
    Арсений Черенков over 7 years
    Welcomme to U&L, this hardly add anything to other answer posted years ago.
  • Luv2code
    Luv2code over 7 years
    Aside: If you're already in vim and want to add another file, you can use: :argadd filename and then :next to it. Likewise, you can use :argdel filename to remove one of the files in edit.
  • Harry Moreno
    Harry Moreno about 6 years
    I found chapter 22 "Finding the file to edit" very useful as well. It goes over using the directory explorer to edit the files you want.
  • Jeach
    Jeach about 6 years
    I have been using CTRL+W for years and recently I do a lot of VI editing inside web pages. Every single time, I type the command without even thinking and ... whammmm ... browser tab is GONE! I keep forgetting. I really need to learn a new shortcut.
  • Dylan
    Dylan over 5 years
    :bw close file haha. Don't forget to read the help for :bw where it says that you should only use it if you know what you're doing. It's not a big deal, but unless you want to wipe out all of the marks and information about the file, better use :bd or :q like a normal person
  • wisbucky
    wisbucky over 5 years
    More info on :bw in case you are curious: vi.stackexchange.com/questions/2212/…
  • Adrian
    Adrian almost 5 years
    Consider linking to usr_07.txt: vimdoc.sourceforge.net/htmldoc/usr_07.html
  • Nicholas Cousar
    Nicholas Cousar almost 4 years
    Is there an option like number 2 that lets you open multiple files from bash and split them vertically or horizontally, except one of those windows is the current terminal? I know I can do something similar similar from within Vim with the :terminal command, but I would like to be able to do it from bash.
  • Edwin Pratt
    Edwin Pratt over 3 years
    @Kowh, also :wN is shorthand for write and open previous file 😀