247

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?

cwd
  • 45,389

11 Answers11

258

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
bsd
  • 11,036
96

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.

Hanan
  • 5,771
  • 29
    This. Also open files in tabs with: vim -p file1 file2 – Not Now Dec 25 '11 at 01: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
  • 5
    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 09:26
  • 9
    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
  • Also generally available if you use the tab approach: :tabnew xyz, :Texplore for selecting a file to open (and :Hexpl, :Vexpl...), :tabn3 for e.g. going to the third tab. I've read the linked article in a comment above a few times over the years, and remain unconvinced that I shouldn't use tabs this way :) – Gertlex Feb 07 '23 at 01:54
59

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

Birei
  • 8,124
  • 2
    :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: https://vi.stackexchange.com/questions/2212/what-bad-things-can-happen-if-i-use-bwipeout – wisbucky Oct 05 '18 at 23:06
39

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.

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

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.

Woot!

xtian
  • 583
  • 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. – Display name May 04 '20 at 01:15
14

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.

Cory Klein
  • 18,911
7
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*.

Dylan
  • 435
4

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.

aleroy
  • 133
2

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

0

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

:p for previous file

:n for next file

dr_
  • 29,602