Linux Text Editing Commands: Vim, Nano, Emacs

1. Detailed Commands for Vim/Vi

1.1 Purpose

Vim (Vi IMproved) is an enhanced version of Vi and is one of the most powerful text editors in Linux systems. Vi is the earliest text editor for Unix systems, and almost all Linux systems come pre-installed with Vi. Vim supports syntax highlighting, plugin extensions, and modal editing, making it known as the “programmer’s editor,” particularly suitable for system administration and programming development.

1.2 Syntax

vim filename        # Open file
vi filename         # Open file with vi
vim +10 filename    # Open and jump to line 10
vim -O file1 file2  # Open multiple files in horizontal split

Vim has three main modes:

  • Normal Mode: Enter by pressing <span>Esc</span>, used for executing commands and navigation
  • Insert Mode: Enter by pressing <span>i</span>, <span>a</span>, or <span>o</span>, used for entering text
  • Command Mode: Enter by pressing <span>:</span>, used for executing save, exit, and other operations

1.3 Examples

Cursor Movement:

# In Normal Mode
h/j/k/l    # Move left/down/up/right
0          # Move to the beginning of the line
$          # Move to the end of the line
gg         # Move to the beginning of the file
G          # Move to the end of the file
:n         # Jump to line n (Command Mode)

Edit Operations:

# Enter Insert Mode
i          # Insert before the cursor
I          # Insert at the beginning of the line
a          # Insert after the cursor
A          # Insert at the end of the line
o          # Insert a new line below the current line
O          # Insert a new line above the current line

# Delete Operations
x          # Delete the current character
dd         # Delete the entire line
dw         # Delete a word

# Copy and Paste
yy         # Copy the current line
p          # Paste
u          # Undo
Ctrl+r     # Redo

Save and Exit:

:w          # Save the file
:q          # Exit
:wq         # Save and exit
:x          # Save and exit (equivalent to :wq, exit directly if the file is not modified)
:q!         # Force exit without saving
:wq!        # Force save and exit

Find and Replace:

/pattern    # Search forward
?pattern    # Search backward
n           # Next match
N           # Previous match
:%s/old/new/g        # Replace all occurrences
:1,10s/old/new/g     # Replace in lines 1-10

Split Screen Operations:

:split      # Horizontal split
:vsplit     # Vertical split
Ctrl+ww     # Switch windows

Visual Mode:

v          # Enter character visual mode
V          # Enter line visual mode
Ctrl+v     # Enter block visual mode

Macro Recording:

qa         # Start recording a macro, save to register a
q          # Stop recording
@a         # Play macro a

Practical Scenario Examples:

# Edit SSH configuration file
sudo vim /etc/ssh/sshd_config

# Quickly comment multiple lines (Block Visual Mode)
Ctrl+v     # Enter block selection
Select multiple lines and press I#␣  # Insert comment symbol
Press Esc to apply

# Execute external commands in Vim
:!chmod +x %    # Add execute permission to the current file
:!./%           # Execute the current script

1.4 Source Code

Vim is an open-source project, and the source code can be obtained in the following ways:

  • Vim Official Website: https://www.vim.org
  • GitHub Repository: vim/vim
  • Vi is part of early Unix tools, and the source code can be found in various Unix distributions

1.5 References

  • System Manual: <span>man vim</span> or <span>man vi</span>
  • Official Documentation: Vim Official Documentation
  • Linux Man Pages: man7.org vim
  • Built-in Tutorial: Type <span>vimtutor</span> in the terminal to start the interactive tutorial

(Link to obtain at the bottom of the article)

2. Detailed Commands for Nano

2.1 Purpose

Nano is a lightweight text editor developed by the GNU project, with a simple and intuitive operation, suitable for beginners. The bottom of the interface displays common shortcut key hints, allowing quick access without memorizing complex commands. It is suitable for quickly editing configuration files, writing simple scripts, and other scenarios.

2.2 Syntax

nano filename        # Open file
nano -w filename     # Disable automatic line wrapping

2.3 Examples

File Operations:

Ctrl+O      # Save file (Write Out)
Ctrl+X      # Exit the editor

Edit Operations:

Ctrl+K      # Cut the current line
Ctrl+U      # Paste
Ctrl+6      # Mark text (for copying/cutting multiple lines)

Search and Replace:

Ctrl+W      # Search text
Ctrl+\      # Replace text
Alt+W       # Search next

Cursor Movement:

Ctrl+F      # Move forward one character
Ctrl+B      # Move back one character
Ctrl+P      # Previous line
Ctrl+N      # Next line
Ctrl+A      # Move to the beginning of the line
Ctrl+E      # Move to the end of the line
Ctrl+V      # Next page
Ctrl+Y      # Previous page

Other Functions:

Ctrl+G      # Open help document
Ctrl+C      # Show current cursor position
Ctrl+T      # Spell check (if available)

Practical Scenario Examples:

# Edit configuration file
sudo nano /etc/hosts

# Create a new file and edit
nano script.sh

# Disable automatic line wrapping while editing (suitable for editing long lines)
nano -w config.txt

2.4 Source Code

Nano is part of the GNU project, and the source code can be obtained in the following ways:

  • GNU Nano Official Website: https://www.nano-editor.org
  • Source Repository: GNU Nano Git Repository

2.5 References

  • System Manual: <span>man nano</span>
  • Official Documentation: Nano Official Documentation
  • Built-in Help: Press <span>Ctrl+G</span> in Nano to view the help document

(Link to obtain at the bottom of the article)

3. Detailed Commands for Emacs

3.1 Purpose

Emacs is a powerful text editor developed by the GNU project, known for its high customizability and extensibility, referred to as “the operating system within an operating system.” In addition to text editing capabilities, Emacs can handle email, project management, debugging programs, and even run games, making it a comprehensive working environment.

3.2 Syntax

emacs filename        # Open file in graphical interface
emacs -nw filename    # Open file in terminal (no window)

3.3 Examples

File Operations:

Ctrl+X Ctrl+F    # Open file (Find File)
Ctrl+X Ctrl+S    # Save file (Save)
Ctrl+X Ctrl+W    # Save as (Write File)
Ctrl+X Ctrl+C    # Exit Emacs

Edit Operations:

Ctrl+K    # Delete from cursor to end of line
Ctrl+Y    # Paste (Yank)
Ctrl+D    # Delete one character
Ctrl+Space    # Set mark (for selecting text)
Ctrl+W    # Cut selected region
Alt+W     # Copy selected region

Cursor Movement:

Ctrl+P    # Move up one line (Previous)
Ctrl+N    # Move down one line (Next)
Ctrl+B    # Move left one character (Backward)
Ctrl+F    # Move right one character (Forward)
Ctrl+A    # Move to the beginning of the line
Ctrl+E    # Move to the end of the line
Alt+F     # Move forward one word
Alt+B     # Move back one word
Ctrl+V    # Next page
Alt+V     # Previous page

Search and Replace:

Ctrl+S    # Search forward (Incremental Search)
Ctrl+R    # Search backward
Alt+%     # Interactive replace (Query Replace)
Ctrl+Alt+%    # Regular expression replace

Window Operations:

Ctrl+X 1    # Close other windows, keep only the current window
Ctrl+X 2    # Horizontal split window
Ctrl+X 3    # Vertical split window
Ctrl+X o    # Switch to other window

Buffer Operations:

Ctrl+X Ctrl+B    # List all buffers
Ctrl+X b         # Switch to specified buffer
Ctrl+X k         # Close current buffer

Practical Scenario Examples:

# Open file in terminal
emacs -nw script.py

# Open in graphical interface
emacs document.txt

# Open file after starting Emacs
# In Emacs, press Ctrl+X Ctrl+F, then enter the file path

3.4 Source Code

Emacs is part of the GNU project, and the source code can be obtained in the following ways:

  • GNU Emacs Official Website: https://www.gnu.org/software/emacs/
  • Source Repository: GNU Emacs Git Repository

3.5 References

  • System Manual: <span>man emacs</span>
  • Official Documentation: Emacs Official Documentation
  • Built-in Tutorial: Press <span>Ctrl+H</span> then <span>T</span> in Emacs to start the interactive tutorial
  • Help System: Press <span>Ctrl+H</span> in Emacs to access the help system

(Link to obtain at the bottom of the article)

Summary

Each Linux text editor has its own characteristics, and choosing the right editor can greatly improve work efficiency:

  • Nano: Simple and easy to use, suitable for beginners and quick edits
  • Vim/Vi: Powerful features, suitable for system administrators and developers
  • Emacs: Highly extensible, suitable for users seeking a comprehensive working environment

It is recommended to start with Nano and gradually transition to Vim or Emacs. The most important thing is to practice consistently and apply the learned commands in daily work.

Link to obtain: Follow the public account and reply with the corresponding command name, such as “vim”

Leave a Comment