Learn GNU/Linux Commands (5): Text Editor/Reader - vi, nano, cat, less

In a system without desktop environments, shell text editors can be helpful. We introduce "vi" and its most commonly used commands, as well as "nano" and its most commonly used shortcuts.


vi

"vi" is probably the most used Shell text editor. Most Unix-like systems have it preinstalled. By saying "vi", we mean the modern "vi".

Open a file

vi [FILE]

If FILE doesn't exist, it will be created when we try to save the changes.

Edit

After entering vi, we are in the Command mode, we can move the cursor with arrow keys and Home, End, PageUp, PageOn keys just as expected.

To make changes, press "i" to enter the Insert mode. Now we can type. We can delete characters with backspace and Del keys. We can move the cursor.

To switch back to the Command mode, press the "Esc" key.

Commands

In the Command mode, we can use the following commands.

    Save/Exit

  • type ":w" and hit Enter to save changes.
  • type ":wq" and hit Enter to save changes and exit.
  • type ":q!" and hit Enter to discard changes and exit.
  • Redo/Undo

  • press "u" to undo the last change.
  • press "Shift + u" to return the last line modified to its original state.
  • press "Ctrl + u" to redo changes which were undone (undo the undos).
  • Search/Replace

  • type ":s/" or "/", then enter a string to search for the string.
  • press "n" to search for the next occurrence of the string.
  • type ":s/", then the string to find, then "/", then the string to replace, to replace a string.
    For example, ":s/hi/hello" replaces the first occurrence of "hi" with "hello".
  • add "%" before "s" to replace all occurrences of the string to find.
    For example, ":%s/hi/hello" replaces all occurrences of "hi" with "hello".

There are several vi-like editors. "vim" ("Vi IMproved") is perhaps the most famous of them. In several GNU/Linux distros such as Ubuntu, the actual implementation of "vi" is "vim".


nano

"nano" may not be installed out of the box, but it is more beginner-friendly. There are guides and shortcuts with short explanation at the bottom of the editor.

GNU nano 2.9.8 New Buffer
[ Welcome to nano. For basic help, type Ctrl+G. ]
^G
Get Help
^O
Write Out
^W
Where Is
^K
Cut Text
^J
Justify
^C
Cur Pos
^X
Exit
^R
Read File
^\
Replace
^U
Uncut Text
^T
To Spell
^_
Go To Line

Open a file

nano [FILE]

If FILE doesn't exist, it will be created when we try to save the changes. We can start "nano" with no argument (pathname) and give a name later by following the guide when we try to save the changes.

Edit

After entering nano, we can edit the text directly just as expected.

Shortcuts

In the bottom two lines where shortcuts are shown, the beginning "^" of a shortcut can be entered either by using the Ctrl (control) key on the keyboard or pressing the Esc key twice. "M-" can be entered using either the Alt (Cmd) key or Esc key. For example, the shortcuts for getting help is ^G, which means to hold the Ctrl key and press the G key (not Shift + G) or to press the Esc key twice, then press the G key. The shortcut "M-^" means "Alt + Shift + 6", or to press Esc then hold the Shift key and press 6.

    Save/Exit

  • ^O: save changes.
  • ^X: exit. We will be asked whether to save changes.
  • Undo/Redo

  • M-U: Undo the last operation.
  • M-E: Redo the last operation.
  • Search/Replace

  • ^W: follow the guide to search for a string.
  • ^\: follow the guide to replace a string.
  • ^W: repeat the last search.
  • Mark/Copy/Cut/Paste

  • ^6: Mark text starting from the cursor position.
  • M-6: Copy current line (or marked region) and store it in cutbuffer.
  • ^K: Cut current line (or marked region) and store it in cutbuffer.
  • ^U: Uncut (paste) from the cutbuffer into the current line.

Display a File

To display a file instead of editing it, we can use the "cat" command.

cat [FILE]...

Concatenate FILE(s) to standard output.

Multiple files can be concatenated and displayed with one command. For example:

[texpion@com bin]$ cat file1 file2
This is the content of file1.This is the content of file2.[texpion@com ~]$


If the content is long, we can read it in the program "less".

less FILE

In "less", we can scroll forward and backward with arrow keys and PageUp/PageDn. To exit, press "q".



Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

OBS Studio settings for best quality (loseless) recording

Install VMAF on Fedora, CentOS/Red Hat (RHEL)