Q. How do I open and edit multiple files under VIM text editor running under Ubuntu Linux / UNIX operating systems to improve my productivity?
A. Vim offers multiple file editing with the help of windows. You can easily open multiple files and edit them using the concept of buffers.
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 (other file saving related) command.
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
Start vim as follows to open two windows,split horizontally:
$ vim -o /etc/passwd /etc/hosts
$ vim -o file1.txt resume.txt
(Fig.01: split horizontal windows under VIM)
The -O option allows you to open two windows, split vertically, enter:
$ vim -O /etc/passwd /etc/hosts
How do I switch or jump between windows?
This operation is also known as moving cursor to other windows:
- Press CTRL + W + <Left arrow key> to activate left windows
- Press CTRL + W + <Right arrow key> to activate right windows
- Press CTRL + W + <Up arrow key> to activate to windows above current one
- Press CTRL + W + <Down arrow key> to activate to windows down current one
- 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 text.
How do I close windows?
Press CTRL+W CTRL-Q to 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 - Create a new window and start editing an empty file in it.
How do I split current window in two?
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:
(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
|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):
Note: 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:
- Refer "Splitting windows" help by typing :help under vim itself.
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop