Vim Editing Multiple Files and Windowing Support Under Linux / UNIX

How do I open and edit multiple files on a VIM text editor running under Ubuntu Linux / UNIX-like operating systems to improve my productivity?

Vim offers multiple file editing with the help of windows. You can easily open multiple files and edit them using the concept of buffers. [donotprint]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements VIM
Est. reading time 2m

Understanding vim buffer

A buffer is nothing but a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file using w or other file saving related commands.

Understanding vim window

A window is noting but a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers. By default, Vim starts with one window, for example open /etc/passwd file, enter:
$ vim /etc/passwd

Open two windows using vim at shell promot

Start vim as follows to open two windows stacked, i.e. split horizontally:
$ vim -o /etc/passwd /etc/hosts
$ vim -o file1.txt resume.txt
Sample outputs:

(Fig.01: split horizontal windows under VIM)

The -Ooption allows you to open two windows side by side, i.e. split vertically, enter:
$ vim -O /etc/passwd /etc/hosts

How do I switch or jump between open windows?

This operation is also known as moving cursor to other windows. You need to use the following keys:

  1. Press CTRL + W + <Left arrow key> to activate left windows
  2. Press CTRL + W + <Right arrow key> to activate right windows
  3. Press CTRL + W + <Up arrow key> to activate to windows above current one
  4. Press CTRL + W + <Down arrow key> to activate to windows down current one
  5. Press CTRL-W + CTRL-W (hit CTRL+W twice) to move quickly between all open windows

How do I edit current buffer?

Use all your regular vim command such as i, w and so on for editing and saving text.

How do I close windows?

Press CTRL+W CTRL-Qto close the current windows. You can also press [ESC]+:q to quit current window.

How do I open new empty window?

Press CTRL+W + n to create a new window and start editing an empty file in it.

Press CTRL+W+ s to split current window in two.

How do I open exiting file in a new windows?

Press [ESC]+:new /path/to/file. This will create a new window and start editing file /path/to/file in it. For example, open file called /etc/hosts.deny, enter:
:new /etc/hosts.deny
Sample outputs:

(Fig.02: Create a new window and start editing file /etc/hosts.deny in it.)

(Fig.03: Two files opened in a two windows)

How do I resize Window?

You can increase or decrease windows size by N number. For example, increase windows size by 5, press [ESC] + 5 + CTRL + W+ +. To decrease windows size by 5, press [ESC]+ 5 + CTRL+ W + -.

Moving windows cheat sheet

Key combination Action
CTRL-W h move to the window on the left
CTRL-W j move to the window below
CTRL-W k move to the window above
CTRL-W l move to the window on the right
CTRL-W t move to the TOP window
CTRL-W b move to the BOTTOM window

How do I quit all windows?

Type the following command (also known as quit all command):
If any of the windows contain changes, Vim will not exit. The cursor will automatically be positioned in a window with changes. You can then either use “:write” to save the changes:
or “:quit!” to throw them away:

How do save and quit all windows?

To save all changes in all windows and quite , use this command:
This writes all modified files and quits Vim. Finally, there is a command that quits Vim and throws away all changes:

Further readings:
  • Refer “Splitting windows” help by typing :help under vim itself.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 25 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
25 comments… add one
  • Bash Jul 9, 2008 @ 21:21

    I find the vertical split to be more useful.



    for vertical split.

  • Ramesh | The Geek Stuff Jul 10, 2008 @ 5:50


    Yeah I agree with you. Vertical split is especially very good if you a wide screen monitor.

    I absolutely love the multiple window feature of the vim and it definitely enhances the productivity to a great extend.


  • mohan Jul 10, 2008 @ 6:10

    vimdiff file1.txt file2.txt

    will give you vertical split.

    • dev Oct 12, 2012 @ 6:26

      but that’s not the same thing dear.
      vimdiff is intended to show differences between/among 2/3 files, which would produce the result you refer here in case of files with totally dissimilar content, but not otherwise.

    • dev Oct 12, 2012 @ 8:36

      but that’s not what its intended for. Its meant to bring out (highlight) the diffenrences between/among 2/3 files, which would be same as vim -O [,], with highlighted differences.

  • Tim Jul 10, 2008 @ 11:30

    Your text states “You can then either use “:write” to save the changes, or “:quit!” to throw them away.”

    But your command says “:quite!”

    May wanna fix that ;)

  • Eric Lin Jul 14, 2008 @ 2:10

    When you are in split mode, you can type

    :set mouse=a

    to enable mouse command and then you can use your mouse to drag the window boundary to resize the all windows.


  • Aman Jul 30, 2008 @ 6:54


    If I have multiple files opened, say by
    $ vim file1.txt file2.txt file3.txt

    Now, :ls shows all the open buffers

    When I do :q ,all the buffers are closed, but is there a way to close a particular buffer, say only file2.txt

  • Chris Oct 2, 2008 @ 15:47


    It’s easy to close one buffer. In fact, there are a lot of ways to navigate through many open buffers. Here’s some

    Using your example:
    $ vim file1.txt file2.txt file3.txt

    you currently have 3 open buffers. Let’s say you are currently editing file1.txt

    :bn – go to next buffer (in this case, file2.txt)
    :bp – go to previous buffer (will go backwards to file3.txt)
    :bd – delete current buffer (file1.txt will be closed and file2.txt will become the current buffer) (can also take a filename or number as an argument)
    :b – go directly to a file

    If you are editing file2.txt and want to close file3.txt, type:

    :bd file3.txt

    You will still be editing file2.txt, but file3.txt will no longer be open.

    in vim,
    :ls – this command lists your open files and assigns an identifying number to each one.

    In your example, it would look like
    1 “file1.txt” line 0
    2 “file2.txt” line 0
    3 “file3.txt” line 0

    :b 3
    will go directly to file3.txt (which is buffer 3)

    :bd 2
    will delete the file2.txt buffer

    I think this is a good starting point. Google something like “vim buffers” for more info!


  • Syed K Jun 4, 2009 @ 11:26

    Very useful


  • Laxman Aug 2, 2009 @ 11:45

    Thanks dude
    I generally used gvim instead of vi/vim. I really like to woke with multi-file in split windows. This web page help me to switch window using keystrokes only which i have needed.

  • Shahid Sep 17, 2009 @ 11:07

    Great work!! It helped us soo much!! the navigation through multiple windows/files..
    Thanks a million!!!

  • Patrick Oct 17, 2009 @ 8:00

    How can I change the size of a vertical split without using the mouse. I know that you can use #,Ctrl+w,+ to re-size a horizontal split but I can’t seem to figure out how to get the same results for a vertical one.

  • Nishad Oct 20, 2009 @ 9:22

    Gr8. It helped me a lot to work around with Vim/vi.

    Many Many thanks


  • finn Mar 2, 2010 @ 19:03

    Ctrl w

    put a number before the to say how much

  • Mukesh Raghav Jul 20, 2010 @ 19:09

    try vi -d file1.txt file2.txt for diff,

    • Rambo Jul 27, 2010 @ 16:02

      Hey Raghav,

      Great …. I tried this command; we can make an awesome diff with this.


  • Sathish Mar 16, 2011 @ 5:25

    Really all are awesome and help a lot, thanks for all commenter come up with their various alternate
    thanks a lot!

  • siddhartha Aug 26, 2011 @ 6:50

    If i goto some dir and do vi *, then i have to see all the files in vi vertically..doing :sba shows all the files horizontally .. how could this be achived?

  • saina Oct 15, 2011 @ 10:28

    Really awesome.. thanks for all commenter

  • JadedEvan Oct 19, 2011 @ 17:54

    I would suggest updating your documentation above. Moving between vertical / horizontal split windows is CTRL + w + , not CTRL + W + . The uppercase W should be lowercase.

  • john Mar 21, 2013 @ 14:24

    great post!!

  • Krishnan May 14, 2014 @ 17:30

    I checked multiple forums to get a help on vim but found that this tutorial was most helpful . Thanks a lot for this post . wonderful

    • kgas May 24, 2014 @ 4:01

      One more thing to mention is switching the windows. If you have two windows with horizontal split and making them to vertical split use ctrl+w+K , for the reverse ctrl+w+L

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum