Linux Common Editor Usage

February 26, 2024

preface

In the Linux environment, most of people choose vim as a text editor, even as an IDE (Integrated Development Environment). But except vim, there are still so many text editors like vi, nano, etc...
So in this post, I'll try to list below text editors' common usage to improve your development.
  • vi
  • vim
  • nano

Vi

Opening a File

Using vi {path2file} to edit file with vi.

modes

vi has different modes:
  • Normal Mode: Used for navigation and executing commands.
  • Insert Mode: Used for inserting or editing text.
  • Command-Line Mode: Used for saving changes, quitting, and other commands.
To switch from Normal Mode to Insert Mode, press i. To switch from Insert Mode to Normal Mode, press Esc.
Use arrow keys to move the cursor.
h for left, l for right, j for down, and k for up.
w to move the cursor to the beginning of the next word. b to move the cursor to the beginning of the previous word.
0 (zero) to move to the beginning of the current line. $ to move to the end of the current line.
gg to move to the beginning of the file. G to move to the end of the file.

Editing in Normal Mode:

x to delete the character under the cursor.
dd to delete the current line.
yy to yank (copy) the current line.
p to paste the yanked or deleted text after the cursor.

Saving and Quitting

In Normal Mode, type :w to save changes. Type :q to quit. Type :wq to save and quit. Type :q! to quit without saving.
TIP
using :w !sudo tee > /dev/null % to save when you forget using sudo to edit unprivileged file.

Searching:

In Normal Mode, type / followed by the search term and press Enter.
Use n to find the next occurrence.

undo/redo

u to undo the last change. Ctrl + r to redo a change.

Vim

The above vi commands can all be used in vim, so only the content that is only valid in Vim is shown here.

Visual Mode:

v: Enter Visual Mode to select characters. V: Enter Visual Line Mode to select whole lines. Ctrl + v: Enter Visual Block Mode to select a block of text.
d: Delete the selected text in Visual Mode. x: Cut the selected text in Visual Mode.
:s/old/new/g: Substitute all occurrences of 'old' with 'new' in the current line. :%s/old/new/g: Substitute all occurrences of 'old' with 'new' in the entire file. :s/old/new/gc: Substitute with confirmation.

Nano

Opening a File

Using nano {path2file} to edit file with vi.
Use arrow keys to move the cursor.
Ctrl + F: Move forward one page. Ctrl + B: Move backward one page. Ctrl + ↑: Move to the previous line. Ctrl + ↓: Move to the next line.

Editing

Since Nano is a user-friendly editor, you can edit it like a common GUI text editor. for example, append by type character, delete by backspace.
And there also has some commands for advanced usage.
Ctrl + K: Cut (delete) the current line. Ctrl + U: Uncut (paste) the previously cut text. Ctrl + O: Write the current contents to a file. Ctrl + X: Exit nano. If changes are made, you will be prompted to save.

Searching

Ctrl + W: Search for a specific term. Press Enter to search and Ctrl + W again to find the next occurrence.

to be continued...

The End