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
add comment

6 Answers 6

up vote 38 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
3  
: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
add comment

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
3  
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
add comment

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
add comment

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
add comment

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
add comment
:n -> Move to next file
:N -> Move to previous file
share|improve this answer
add comment

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.