Using vi or Vim Editors - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Appendix A

Using vi or Vim Editors


· Using the vi editor

· Starting/quitting the vi editor

· Moving around in vi

· Changing and deleting text

· Using Ex commands

· Using visual mode

Although easy-to-use graphical text editors (such as gedit and kedit) are readily available with Linux, most power users still use vi, nano, or Emacs to edit text files. In addition to the fact that those text-based editors will work from any shell (no GUI required), they offer other advantages. For example, your hands never have to leave the keyboard and they offer integration with useful utilities. And unlike GUI editors, text-based editors will work if no graphical interface is installed (as is true with many Linux servers and specialty devices).

This appendix focuses on features of the vi editor that can not only help you with basic editing, but also help you do some advanced text manipulation. I chose to cover vi rather than Emacs because vi is more universal and leaner, and also because vi keyboard shortcuts require fewer contortions.

Because many Linux systems use the Vim (Vi IMproved) editor by default, in place of the older vi editor, the descriptions in this appendix are extended to cover Vim as well. Some features in Vim that are not in vi include multiple undo levels, syntax highlighting, and online help.

Note If you have never used vi or Vim before, try out the tutor that comes with the vim-enhanced package. Run the vimtutor command and follow the instructions to step through many of the key features of vi and Vim.

Starting and Quitting the vi Editor

If you want to experiment with using vi, you should copy a text file to practice on. For example, type:

$ cp /etc/services /tmp

Then open that file using the vi command as follows:

$ vi /tmp/services

To benefit from all the improvements of Vim, make sure you have the vim-athena package installed (which gets installed by default on Ubuntu). On many systems, vi is a symbolic link to the vim command. On Ubuntu, both commands launch vim.

Here are a few other ways you can start vi:

$ vi +25 /tmp/services Begin on line 25

$ vi + /tmp/services Begin editing file on the last line

$ vi +/tty /tmp/services Begin on first line with word “tty”

$ vi -r /tmp/services Recover file from crashed edit session

$ view /tmp/services Edit file in read-only mode

When you are done with your vi session, there are several different ways to save and quit:

· To save the file before you are ready to quit, type :w

· To quit and save changes, type either ZZ or :wq

· To quit without saving changes, type :q!

If you find that you can’t write to the file you are editing, it may have been opened in read-only mode. If that’s the case, you can try forcing a write by typing :w! or you can save the contents of the file to a different name. For example, type the following to save the contents of the current file to a file named myfile.txt:

:w /tmp/myfile.txt

The vi editor also enables you to line up several files at a time to edit. For example, type:

$ cd /tmp

$ touch a.txt b.txt c.txt

$ vi a.txt b.txt c.txt

In this example, vi will open the a.txt file first. You can move to the next file by typing :n. You may want to save changes before moving to the next file (:w) or save changes as you move to the next file (:wn). To abandon changes while moving to the next file, type :n!.

You will probably find it easier to open multiple files by splitting your vi screen. When you’re in vi and have a file open, you can split your screen multiple times either horizontally or vertically:

:split /etc/motd.tail

:vsplit /etc/motd.tail

Use the Tab key to complete the path to the files, just like you would in a bash shell. To navigate between split windows, press Ctrl+w, followed by the w key. To close the current window, use the usual vi exit command (:q).

Moving Around in vi

The first thing to get used to with vi is that you can’t just start typing. Vi has multiple modes that enable you to perform a different set of tasks. You start a vi session in Normalmode, where vi is waiting for you to type a command to get started.

While you are in Normal mode, you can move around the file, to position where you want to be in the file. To enter or modify text, you need to go into Insert or Replace modes.

Assuming vi is open with a file that contains several pages of text, the following list shows some keys and combinations you can type to move around the file while in Normal mode.

PageDown or Ctrl+f—Move down one page.

Ctrl+d—Move down half page.

Shift+g—Go to last line of file.

Shift+h—Move cursor to screen top.

Shift+m—Move cursor to middle of screen.

Enter—Move cursor to beginning of the next line.

Home or $—Move cursor to end of line.

(—Move cursor to beginning of previous sentence.

{—Move cursor to beginning of previous paragraph.

w—Move cursor to next word (space, new line, or punctuation).

b—Move cursor to previous word (space, new line, or punctuation).

e—Move cursor to end of next word (space, new line, or punctuation).

Left arrow or Backspace—Move cursor left one letter.

k or up arrow—Move cursor up one line.

/string—Find next occurrence of string.

n—Find same string again (forward).

PageUp or Ctrl+b—Move up one page.

Ctrl+u—Move up half page.

:1—Go to first line of file (use any number to go to that line).

Shift+l—Move cursor to screen bottom .

Ctrl+l—Redraw screen (if garbled).

- —Move cursor to beginning of the previous line.

End or ^ or 0—Move cursor to line beginning.

)—Move cursor to beginning of next sentence.

}—Move cursor to beginning of next paragraph.

Shift+w—Move cursor to next word (space or new line).

Shift+b —Move cursor to previous word (space or new line).

Shift+e—Move cursor to end of next word (space or new line).

Right arrow or l—Move cursor right one letter.

j or down arrow—Move cursor down one line.

?string—Find previous occurrence of string.

Shift+n—Find same string again (backwards).

Changing and Deleting Text in vi

To begin changing or adding to text with vi, you can enter Insert or Replace modes, as shown in the following list. When you enter Insert or Replace mode, the characters you type will appear in the text document (as opposed to being interpreted as commands).

Press the Esc key to exit to Normal mode after you are done inserting or replacing text.

i—Typed text appears before current character.

a—Typed text appears after current character.

o—Open a new line below current line to begin typing.

s—Erase current character and replace with new text.

c?—Replace ? with l, w, $, or c to change the current letter, word, end of line, or line.

r—Replace current character with the next one you type.

Shift+i—Typed text appears at the beginning of current line.

Shift+a—Typed text appears at the end of current line.

Shift+o—Open a new line above current line to begin typing.

Shift+s—Erase current line and enter new text.

Shift+c—Erase from cursor to end of line and enter new text.

Shift+r—Overwrite as you type from current character going forward.

The following list contains keys you type to delete or paste text.

x—Delete text under cursor.

d?—Replace ? with l, w, $, or d to cut the current letter, word, or end of line from cursor or entire line.

y?—Replace ? with l, w, or $ to copy (yank) the current letter, word, or end of line from cursor.

p—Pastes cut or yanked text after cursor.

Shift+x—Delete text to left of cursor.

Shift+d—Cut from cursor to end of line.

Shift+y—Yank current line .

Shift+p—Pastes cut or yanked text before cursor.

Using Miscellaneous Commands

The following list shows a few miscellaneous, but important, commands you should know.

u—Type u to undo the previous change. Multiple u commands will step back to undo multiple changes.

. —Typing a period (.) will repeat the previous command. So, if you deleted a line, replaced a word, changed four letters, and so on, the same command will be done wherever the cursor is currently located. (Entering input mode again resets it.)

Shift+j—Join the current line with the next line.

Esc—If you didn’t catch this earlier, the Esc key returns you from an input mode back to command mode. This is one of the keys you will use most often.

Modifying Commands with Numbers

Nearly every command described so far can be modified with a number. In other words, instead of deleting a word, replacing a letter, or changing a line, you can delete six words, replace twelve letters, and change nine lines. The following list shows some examples.

7cw—Erase the next seven words and replace them with text you type.

5 Shift+d—Cut the next five lines (including the current line).

3p—Paste the previously deleted text three times after the current cursor.

9db—Cut the nine words before the current cursor.

10j—Move the cursor down ten lines.

y2)—Copy (yank) text from the cursor to the end of next two sentences.

5 Ctrl+f—Move forward five pages.

6 Shift+j—Join the next six lines.

From these examples, you can see that most vi keystrokes for changing text, deleting text, or moving around in the file can be modified using numbers.

Using ex Commands

The vi editor was originally built on an editor called ex. Some of the vi commands you’ve seen so far start with a semicolon and are known as ex commands. To enter ex commands, start from normal mode and type a colon (:). This switches you to command line mode.

In command line mode, you can use the Tab key to complete your command or filename, and the arrow keys to navigate your command history, as you would in a bash shell. When you press Enter at the end of your command, you are returned to Normal mode.

The following list shows some examples of ex commands.

:!bash—Escape to a bash shell. When you are done, type exit to return to vi.

:!date—Run date (or any command you choose). Press Enter to return.

:!!—Rerun the command previously run.

:20—Go to line 20 in the file.

:5,10w abc.txt—Write lines 5 through 10 to the file abc.txt.

:e abc.txt—Leave the current file and begin editing the file abc.txt.

:.r def.txt—Read the contents of def.txt into the file below the current line.

:s/RH/Red Hat—Substitute Red Hat for the first occurrence of RH on the current line.

:s/RH/Red Hat/g—Substitute Red Hat for all occurrences of RH on the current line.

:%s/RH/Red Hat/g—Substitute Red Hat for all occurrences of RH in the entire file.

:g/Red Hat/p—List every line in the file that contains the string Red Hat.

:g/gaim/s//pidgin/gp—Find every instance of gaim and change it to pidgin.

From the ex prompt you can also see and change settings related to your vi session using the set command. The following list shows some examples.

:set all—List all settings.

:set—List only those settings that have changed from the default.

:set number—Have line numbers appear to the left of each line. (Use set nonu to unset.)

:set ai—Sets autoindent, so opening a new line follows the previous indent.

:set ic—Sets ignore case, so text searches will match regardless of case.

:set list—Show $ for end of lines and ^I for tabs.

:set wm—Causes vi to add line breaks between words near the end of a line.

Working in Visual Mode

The Vim editor provides a more intuitive means of selecting text called visual mode. To begin visual mode, move the cursor to the first character of the text you want to select and press the v or V key. With v, you highlight character-by-character; with V it’s line-by-line. You will see that you are in visual mode because the following text appears at the bottom of the screen:

-- VISUAL --

At this point, you can use any of your cursor movement keys (arrow keys, Page Down, End, and so on) to move the cursor to the end of the text you want to select. As the page and cursor move, you will see text being highlighted.

When all the text you want to select is highlighted, you can press keys to act on that text. For example, d deletes the text, c lets you change the selected text, :w /tmp/test.txt saves selected text to a file, and so on.