LFCS: How to Install and Use vi/vim as a Full Text Editor – Part 2

[‘

n

A couple of months ago, the Linux Foundation launched the LFCS (Linux Foundation Certified Sysadmin) certification in order to help individuals from all over the world to verify they are capable of doing basic to intermediate system administration tasks on Linux systems: system support, first-hand troubleshooting and maintenance, plus intelligent decision-making to know when it’s time to raise issues to upper support teams.

n

Learning VI Editor in Linux
Learning VI Editor in Linux

n

Please take a look at the below video that explains The Linux Foundation Certification Program.

n

n

n

n

This post is Part 2 of a 10-tutorial series, here in this part, we will cover the basic file editing operations and understanding modes in vi/m editor, that are required for the LFCS certification exam.

n

Perform Basic File Editing Operations Using vi/m

n

Vi was the first full-screen text editor written for Unix. Although it was intended to be small and simple, it can be a bit challenging for people used exclusively to GUI text editors, such as NotePad++, or gedit, to name a few examples.

n

To use Vi, we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about the its powerful text-editing procedures.

n

Please note that most modern Linux distributions ship with a variant of vi known as vim (“Vi improved”), which supports more features than the original vi does. For that reason, throughout this tutorial we will use vi and vim interchangeably.

n

If your distribution does not have vim installed, you can install it as follows.

n

    n

  1. Ubuntu and derivatives: aptitude update && aptitude install vim
  2. n

  3. Red Hat-based distributions: yum update && yum install vim
  4. n

  5. openSUSE: zypper update && zypper install vim
  6. n

n

Why should I want to learn vi?

n

There are at least 2 good reasons to learn vi.

n

1. vi is always available (no matter what distribution you’re using) since it is required by POSIX.

n

2. vi does not consume a considerable amount of system resources and allows us to perform any imaginable tasks without lifting our fingers from the keyboard.

n

In addition, vi has a very extensive built-in manual, which can be launched using the :help command right after the program is started. This built-in manual contains more information than vi/m’s man page.

n

vi Man Pages
vi Man Pages

n

Launching vi

n

To launch vi, type vi in your command prompt.

n

Start vi Editor
Start vi Editor

n

Then press i to enter Insert mode, and you can start typing. Another way to launch vi/m is.

n

# vi filenamern

n

Which will open a new buffer (more on buffers later) named filename, which you can later save to disk.

n

Understanding Vi modes

n

1. In command mode, vi allows the user to navigate around the file and enter vi commands, which are brief, case-sensitive combinations of one or more letters. Almost all of them can be prefixed with a number to repeat the command that number of times.

n

For example, yy (or Y) copies the entire current line, whereas 3yy (or 3Y) copies the entire current line along with the two next lines (3 lines in total). We can always enter command mode (regardless of the mode we’re working on) by pressing the Esc key. The fact that in command mode the keyboard keys are interpreted as commands instead of text tends to be confusing to beginners.

n

2. In ex mode, we can manipulate files (including saving a current file and running outside programs). To enter this mode, we must type a colon (:) from command mode, directly followed by the name of the ex-mode command that needs to be used. After that, vi returns automatically to command mode.

n

3. In insert mode (the letter i is commonly used to enter this mode), we simply enter text. Most keystrokes result in text appearing on the screen (one important exception is the Esc key, which exits insert mode and returns to command mode).

n

vi Insert Mode
vi Insert Mode

n

Vi Commands

n

The following table shows a list of commonly used vi commands. File edition commands can be enforced by appending the exclamation sign to the command (for example, <b.:q! enforces quitting without saving).

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

n

xa0Key command xa0Description
xa0h or left arrow xa0Go one character to the left
xa0j or down arrow xa0Go down one line
xa0k or up arrow xa0Go up one line
xa0l (lowercase L) or right arrow xa0Go one character to the right
xa0H xa0Go to the top of the screen
xa0L xa0Go to the bottom of the screen
xa0G xa0Go to the end of the file
xa0w xa0Move one word to the right
xa0b xa0Move one word to the left
xa00 (zero) xa0Go to the beginning of the current line
xa0^ xa0Go to the first nonblank character on the current line
xa0$ xa0Go to the end of the current line
xa0Ctrl-B xa0Go back one screen
xa0Ctrl-F xa0Go forward one screen
xa0i xa0Insert at the current cursor position
xa0I (uppercase i) xa0Insert at the beginning of the current line
xa0J (uppercase j) xa0Join current line with the next one (move next line up)
xa0a xa0Append after the current cursor position
xa0o (lowercase O) xa0Creates a blank line after the current line
xa0O (uppercase o) xa0Creates a blank line before the current line
xa0r xa0Replace the character at the current cursor position
xa0R xa0Overwrite at the current cursor position
xa0x xa0Delete the character at the current cursor position
xa0X xa0Delete the character immediately before (to the left) of the current cursor position
xa0dd xa0Cut (for later pasting) the entire current line
xa0D xa0Cut from the current cursor position to the end of the line (this command is equivalent to d$)
xa0yX xa0Give a movement command X, copy (yank) the appropriate number of characters, words, or lines from the current cursor position
xa0yy or Y xa0Yank (copy) the entire current line
xa0p xa0Paste after (next line) the current cursor position
xa0P xa0Paste before (previous line) the current cursor position
xa0. (period) xa0Repeat the last command
xa0u xa0Undo the last command
xa0U xa0Undo the last command in the last line. This will work as long as the cursor is still on the line.
xa0n xa0Find the next match in a search
xa0N xa0Find the previous match in a search
xa0:n xa0Next file; when multiple files are specified for editing, this commands loads the next file.
xa0:e file xa0Load file in place of the current file.
xa0:r file xa0Insert the contents of file after (next line) the current cursor position
xa0:q xa0Quit without saving changes.
xa0:w file xa0Write the current buffer to file. To append to an existing file, use :w >> file.
xa0:wq xa0Write the contents of the current file and quit. Equivalent to x! and ZZ
xa0:r! command xa0Execute command and insert output after (next line) the current cursor position.

n

Vi Options

n

The following options can come in handy while running vim (we need to add them in our ~/.vimrc file).

n

# echo set number >> ~/.vimrcrn# echo syntax on >> ~/.vimrcrn# echo set tabstop=4 >> ~/.vimrcrn# echo set autoindent >> ~/.vimrcrn

n

vi Editor Options
vi Editor Options

n

    n

  1. set number shows line numbers when vi opens an existing or a new file.
  2. n

  3. syntax on turns on syntax highlighting (for multiple file extensions) in order to make code and config files more readable.
  4. n

  5. set tabstop=4 sets the tab size to 4 spaces (default value is 8).
  6. n

  7. set autoindent carries over previous indent to the next line.
  8. n

n

Search and replace

n

vi has the ability to move the cursor to a certain location (on a single line or over an entire file) based on searches. It can also perform text replacements with or without confirmation from the user.

n

a). Searching within a line: the f command searches a line and moves the cursor to the next occurrence of a specified character in the current line.

n

For example, the command fh would move the cursor to the next instance of the letter h within the current line. Note that neither the letter f nor the character you’re searching for will appear anywhere on your screen, but the character will be highlighted after you press Enter.

n

For example, this is what I get after pressing f4 in command mode.

n

Search String in Vi
Search String in Vi

n

b). Searching an entire file: use the / command, followed by the word or phrase to be searched for. A search may be repeated using the previous search string with the n command, or the next one (using the N command). This is the result of typing /Jane in command mode.

n

Vi Search String in File
Vi Search String in File

n

c). vi uses a command (similar to sed’s) to perform substitution operations over a range of lines or an entire file. To change the word “old” to “young” for the entire file, we must enter the following command.

n

 :%s/old/young/g rn

n

Notice: The colon at the beginning of the command.

n

Vi Search and Replace
Vi Search and Replace

n

The colon (:) starts the ex command, s in this case (for substitution), % is a shortcut meaning from the first line to the last line (the range can also be specified as n,m which means “from line n to line m”), old is the search pattern, while young is the replacement text, and g indicates that the substitution should be performed on every occurrence of the search string in the file.

n

Alternatively, a c can be added to the end of the command to ask for confirmation before performing any substitution.

n

:%s/old/young/gcrn

n

Before replacing the original text with the new one, vi/m will present us with the following message.

n

Replace String in Vi
Replace String in Vi

n

    n

  1. y: perform the substitution (yes)
  2. n

  3. n: skip this occurrence and go to the next one (no)
  4. n

  5. a: perform the substitution in this and all subsequent instances of the pattern.
  6. n

  7. q or Esc: quit substituting.
  8. n

  9. l (lowercase L): perform this substitution and quit (last).
  10. n

  11. Ctrl-e, Ctrl-y: Scroll down and up, respectively, to view the context of the proposed substitution.
  12. n

n

Editing Multiple Files at a Time

n

Let’s type vim file1 file2 file3 in our command prompt.

n

# vim file1 file2 file3rn

n

First, vim will open file1. To switch to the next file (file2), we need to use the :n command. When we want to return to the previous file, :N will do the job.

n

In order to switch from file1 to file3.

n

a). The :buffers command will show a list of the file currently being edited.

n

:buffersrn

n

Edit Multiple Files
Edit Multiple Files

n

b). The command :buffer 3 (without the s at the end) will open file3 for editing.

n

In the image above, a pound sign (#) indicates that the file is currently open but in the background, while %a marks the file that is currently being edited. On the other hand, a blank space after the file number (3 in the above example) indicates that the file has not yet been opened.

n

Temporary vi buffers

n

To copy a couple of consecutive lines (let’s say 4, for example) into a temporary buffer named a (not associated with a file) and place those lines in another part of the file later in the current vi section, we need to…

n

1. Press the ESC key to be sure we are in vi Command mode.

n

2. Place the cursor on the first line of the text we wish to copy.

n

3. Type “a4yy” to copy the current line, along with the 3 subsequent lines, into a buffer named a. We can continue editing our file – we do not need to insert the copied lines immediately.

n

4. When we reach the location for the copied lines, use “a before the p or P commands to insert the lines copied into the buffer named a:

n

    n

  1. Type “ap to insert the lines copied into buffer a after the current line on which the cursor is resting.
  2. n

  3. Type “aP to insert the lines copied into buffer a before the current line.
  4. n

n

If we wish, we can repeat the above steps to insert the contents of buffer a in multiple places in our file. A temporary buffer, as the one in this section, is disposed when the current window is closed.

n

Summary

n

As we have seen, vi/m is a powerful and versatile text editor for the CLI. Feel free to share your own tricks and comments below.

n

Reference Links

n

    n

  1. About the LFCS
  2. n

  3. Why get a Linux Foundation Certification?
  4. n

  5. Register for the LFCS exam
  6. n

n

Update: If you want to extend your VI editor skills, then I would suggest you read following two guides that will guide you to some useful VI editor tricks and tips.

n

Part 1: Learn Useful ‘Vi/Vim’ Editor Tips and Tricks to Enhance Your Skills

n

Part 2: 8 Interesting ‘Vi/Vim’ Editor Tips and Tricks

n

n

‘]