Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Related: gvim -p limit of opened tabs? –  Grzegorz Wierzowiecki Feb 2 '12 at 16:31

8 Answers 8

up vote 44 down vote accepted

First of all, in vim you can enter : 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 :p

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
:args

[foo.txt] bar.txt

share|improve this answer
4  
:wn is the shortform of :wnext. –  Kowh Dec 26 '11 at 4:34
    
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
1  
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

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.

share|improve this answer
4  
This. Also open files in tabs with: vim -p file1 file2 –  Not Now Dec 25 '11 at 1:15
    
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
    
+1. Great help! –  Nawaz Sep 26 '13 at 6:17

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.

share|improve this answer
3  
You can use :vs instead of :vsp –  Khaja Minhajuddin Jun 3 '12 at 8:09

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 or :file

share|improve this answer

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!

share|improve this answer
:n -> Move to next file
:N -> Move to previous file
share|improve this answer

Whenever I have to edit 3-4 text files in VIM, I used to run the command

vimdiff test1.txt test2.txt test3.txt ...

It will open all four files in parallel. Focus will be on first file by default. To navigate to other files, I use the command ctrl+ww

Though vimdiff is basically used to see the difference of files but one can use it to edit multiple files

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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