How can I edit multiple files in Vim?

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?

Asked By: cwd

||

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.

Answered By: Hanan N.

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

Answered By: Birei

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
Answered By: bsd

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.

Answered By: Not Now
:n -> Move to next file
:N -> Move to previous file
Answered By: diham

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 ctrlw ctrlw.
  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!

Answered By: xtian

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.

Answered By: aleroy

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.

Answered By: Cory Klein

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

Answered By: Michael Durrant

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

:p for previous file

:n for next file

Answered By: bala selvam
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*.

Answered By: Dylan
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.