Working with Vim editor (Text Editor)

If you’re working in command line mode, you may want to become familiar with at least one text editor that operates in the Linux console. The vi editor is the original editor used on Unix systems. It uses the console graphics mode to emulate a text-editing window, allowing you to see the lines of your file, move around within the file, and insert, edit, and replace text.

While it is quite possibly the most complicated editor in the world (at least in the opinion of those who hate it), it provides many features that have made it a staple for Unix administrators for decades. When the GNU Project ported the vi editor to the open-source world, they chose to make some improvements to it. Because it no longer resembled the original vi editor found in the Unix world, the developers also renamed it, to vi improved, or vim. To make system admin’s life easier, almost all Linux distributions create an alias named vi that points to the vim program:

# alias vi='vim'

Basics of vim

The vim editor works with data in a memory buffer. To start the vim editor, just type the file you want to edit :vim command (or vi if there’s an alias) and the name of the file

# vi /etc/fstab

If you start vim without a filename, or if the file doesn’t exist, vim opens a new buffer area for editing. If you specify an existing file on the command line, vim will read the entire contents of the file into a buffer area, where it is ready for editing, as shown below:

The vim editor detects the terminal type for the session and uses a full-screen mode to use the entire console window for the editor area. The initial vim edit window shows the contents of the file (if there are any) along with a message line at the bottom of the window. If the file contents don’t take up the entire screen, vim places a tilde on lines that are not part of the file.

The message line at the bottom indicates information about the edited file, depending on the status of the file, and the default settings in your vim installation. If the file is new, the message [New File] appears.

The vim editor has two modes of operation:

  • Normal mode
  • Insert mode

When you first open a file (or start a new file) for editing, the vim editor enters normal mode. In normal mode, the vim editor interprets keystrokes as commands.

In insert mode, vim inserts every key you type at the current cursor location in the buffer. To enter insert mode, press the i key. To get out of insert mode and go back into normal mode, press the Escape key on the keyboard.

In normal mode, you can move the cursor around the text area by using the arrow keys (as long as your terminal type is detected properly by vim). The vim commands include commands for moving the cursor:

  • h to move left one character
  • j to move down one line (the next line in the text)
  • k to move up one line (the previous line in the text)
  • l to move right one character

Moving around within large text files line by line can get tedious. Fortunately, vim provides a few commands to help speed things along:

  • PageDown (or Ctrl+F) to move forward one screen of data
  • PageUp (or Ctrl+B) to move backward one screen of data
  • G to move to the last line in the buffer
  • num G to move to the line number num in the buffer
  • gg to move to the first line in the buffer

The vim editor has a special feature within the normal mode called command line mode. The command line mode provides an interactive command line where you can enter additional commands to control the actions in vim. To get to command line mode, press the colon key in normal mode. The cursor moves to the message line, and a colon appears, waiting for you to enter a command. Within the command line mode are several commands for saving the buffer to the file and exiting vim:

  • q to quit if no changes have been made to the buffer data
  • q! to quit and discard any changes made to the buffer data
  • w filename to save the file under a different filename
  • wq to save the buffer data to the file and quit

Editing Data

While in insert mode, you can insert data into the buffer; however, sometimes you need to add or remove data after you’ve already entered it into the buffer. While in normal mode, the vim editor provides several commands for editing the data in the buffer. The below table lists some common editing commands for vim.

Command Description
x Delete the character at the current cursor position.
dd Delete the line at the current cursor position.
dw Delete the word at the current cursor position.
d$ Delete to the end of the line from the current cursor position.
J Delete the line break at the end of the line at the current cursor position (joins lines).
u Undo the previous edit command.
a Append data after the current cursor position.
A Append data to the end of the line at the current cursor position.
r char Replace a single character at the current cursor position with char.
R text Overwrite the data at the current cursor position with text, until you press Escape.

Some of the editing commands also allow you to use a numeric modifier to indicate how many times to perform the command. For example, the command 2x deletes two characters, starting from the current cursor position, and the command 5dd deletes five lines, starting at the line from the current cursor position.

A standard feature of modern editors is the ability to cut or copy data, then paste it elsewhere in the document. The vim editor provides a way to do this. Cutting and pasting is relatively easy. You’ve already seen the commands that can remove data from the buffer. However, when vim removes data, it actually keeps it stored in a separate register. You can retrieve that data by using the p command.

For example, you can use the dd command to delete a line of text, then move the cursor to the location in the buffer where you want to place it, and then use the p command. The p command inserts the text after the line at the current cursor position. You can do this with any command that removes text. Copying text is a little bit trickier. The copy command in vim is y (for yank). You can use the same second character with y as with the d command( yw to yank a word, y$ to yank to the end of a line). After you yank the text, move the cursor to the location where you want to place the text, and use the p command. The yanked text now appears at that location.

Yanking is tricky in that you can’t see what happened because you’re not affecting the text that you yank. You never know for sure what you yanked until you paste it somewhere. But there’s another feature in vim that helps you out with yanking. The visual mode highlights text as you move the cursor. You use visual mode to select text to yank for pasting. To enter visual mode, move the cursor to the location where you want to start yanking, and press v. You’ll notice that the text at the cursor position is now highlighted. Next, move the cursor to cover the text you want to yank (you can even move down lines to yank more than one line of text). As you move the cursor, vim highlights the text in the yank area. After you’ve covered the text you want to copy, press the y key to activate the yank command. Now that you’ve got the text in the register, just move the cursor to where you want to paste, and use the p command.

Search and Substitute

One can easily search for data in the buffer using the vim search command. To enter a search string, press the forward-slash (/) key. The cursor goes to the message line, and vim displays a forward slash. Enter the text you want to find, and press the Enter key. The vim editor responds with one of three actions:

  • If the word appears after the current cursor location, it jumps to the first location where the text appears.
  • If the word doesn’t appear after the current cursor location, it wraps around the end of the file to the first location in the file where the text appears (and indicates this with a message). It produces an error message stating that the text was not found in the file.
  • To continue searching for the same word, press the forward-slash character and then press the Enter key, or you can use the n key, for next.

The substitute command allows you to quickly replace (substitute) one word for another in the text. To get to the substitute command you must be in command line mode. The format for the substitute command is:

:s/old/new/

The vim editor jumps to the first occurrence of the text old and replaces it with the text new. There are a few modifications you can make to the substitute command to substitute more than one occurrence of the text:

:s/old/new/g        ## to replace all occurrences of old in a line
:n,ms/old/new/g     ## to replace all occurrences of old between line numbers n and m
:%s/old/new/g       ## to replace all occurrences of old in the entire file
:%s/old/new/gc      ## to replace all occurrences of old in the entire file, but prompt for each occurrence
Related Post