How can I edit multiple files in Vim?
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
Related videos on Youtube
cwd
Updated on September 18, 2022Comments
-
cwd over 1 year
I know I can open multiple files with
vim
by doing something likevim 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?
-
Grzegorz Wierzowiecki about 12 yearsRelated: gvim -p limit of opened tabs?
-
-
Not Now over 12 yearsThis. Also open files in tabs with:
vim -p file1 file2
-
Sasquiha over 12 yearsYou 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 almost 12 yearsYou can use
:vs
instead of:vsp
-
KillerSpaz about 11 yearsWith 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 over 10 yearsAMAZE! I'd never come across this before, despite having used vim for years. This changes everything.
-
Ruslan over 9 yearsOr even just
Ctrl+W, v
for vertical split andCtrl+W, s
for horizontal split. -
David Braverman over 8 yearsNote that tabs are not intended to work with multiple files. They are intended to work with multiple layouts. See article.
-
Арсений Черенков over 7 yearsWelcomme to U&L, this hardly add anything to other answer posted years ago.
-
Luv2code over 7 yearsAside: 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 about 6 yearsI 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 about 6 yearsI 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 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 over 5 yearsMore info on
:bw
in case you are curious: vi.stackexchange.com/questions/2212/… -
Adrian almost 5 yearsConsider linking to usr_07.txt: vimdoc.sourceforge.net/htmldoc/usr_07.html
-
Nicholas Cousar almost 4 yearsIs 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 over 3 years@Kowh, also
:wN
is shorthand for write and open previous file 😀