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?


11 Answers 11


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.

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


vim foo.txt bar.txt


[foo.txt] bar.txt
  • 37
    :wn is the shortform of :wnext. – Kowh Dec 26 '11 at 4:34
  • 1
    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. – Sasquiha Jan 27 '12 at 21:41
  • 5
    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. – mmoya Feb 2 '13 at 17:31
  • 2
    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. – Harry Moreno Feb 1 '18 at 23:30
  • :wn awesome one. – rammayur Jun 9 '18 at 0:49

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.

  • 26
    This. Also open files in tabs with: vim -p file1 file2 – Not Now Dec 25 '11 at 1:15
  • 1
    AMAZE! I'd never come across this before, despite having used vim for years. This changes everything. – me_and Aug 21 '13 at 13:13
  • 4
    Note that tabs are not intended to work with multiple files. They are intended to work with multiple layouts. See article. – patryk.beza Oct 14 '15 at 9:26
  • 7
    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. – Luv2code Jan 19 '17 at 19:05

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

  • 1
    :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 – Dylan Aug 17 '18 at 16:45
  • 1
    More info on :bw in case you are curious: vi.stackexchange.com/questions/2212/… – wisbucky Oct 5 '18 at 23:06

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


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

  • 5
    You can use :vs instead of :vsp – Khaja Minhajuddin Jun 3 '12 at 8:09
  • 3
    Or even just Ctrl+W, v for vertical split and Ctrl+W, s for horizontal split. – Ruslan Jan 5 '15 at 18:20
  • 2
    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. – Jeach Feb 9 '18 at 16:56
:n -> Move to next file
:N -> Move to previous file

I asked a similar question at superuser,

"How to copy and paste between different bash windows and files opened with VI?"

The answer to my question was you can't, but you can open two files in one bash window using VIM's :split command:

  1. Open a file with $ vim file1, open a second file within VIM using :split file2 command.
  2. Or, use $ vim -o file1 file2 from bash.
  3. Switch between files--toggle active file--in VIM with ctrl-w ctrl-w.
  4. An example operation then is copy (or yank) in file1 yy, switch (3), then paste (or put) p contents into file2.
  5. Everything else is normal when either window is active, thus :q quits and :q! force quits.

My bash is black and white, so the file name of each screen is styled as a reversed 'selected' line with the file name cited there.


  • 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. – Nicholas Cousar May 4 '20 at 1:15

I find the most convenient method of editing multiple files is by using tabs. You can open multiple files in separate tabs via the command line like so:

vim -p file1.txt file2.txt

Or if you already have vim open, you can open a new file in a new tab like so:

:tabe file2.txt

Once you have the tabs open, use gt to view the next tab and gT to view the previous tab.

You can also jump to the first tab with 1gt, the second tab with 2gt, etc.

You can close tabs using :tabc

Finally you can move the current tab to the nth location with :ntabm where n is any number greater than or equal to 0.

argadd a/b c/d e/f

argadd ... will add all files to args, and also to :ls / :buffers and allow you to edit them.

See :h arglsit & :h argedit & :h argsd

Also, you can perform certain actions only on args with :argdo ..., and you can start afresh with your args by doing :argsd*.


There are a lot of way to do so. The first one, maybe the less convenient, is to call vim with the files you want to edit - create:

vim first_file second_file ...

It calls the two files in two buffers. To switch from a file to another, please use :n and :N. To list the files you are editing, :args will do the job. :help buffer will help you more on this.

This should answer your question. But here are more information:

There is a way to edit your files in a more convenient way by splitting your console screen using vim. To do this, open two frames with :split (:sp) or :vsplit (:vs) while using vim. Then navigate in these frames by using the combination Ctrl + W > Ctrl + W. You can also use Ctrl + W + the arrow or the key (H, J, K or L) corresponding to the next frame. When this is done, edit the file you want with :e file_name. :help opening-window will help you more on this.

Now, if you'd like to see the differences between files, use the -d argument or call the vimdiff program (it is the same) with the corresponding files. :help diff will help you more on this.

vim -d first_file second_file ...

Please let me know if you have some issue.


I like to use split windows - both horizontal and vertical - to edit multiple files.

I use tmux to manage those windows more easily

https://tmux.github.io/ "What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more. See the manual."

Example: Looking at files or running tests on the right, editing test and source files on the left: enter image description here


:e #(filename) to toggle between two opened files

:p for previous file

:n for next file

  • Welcomme to U&L, this hardly add anything to other answer posted years ago. – Archemar Jan 3 '17 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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