Text Editors
Overview
Text editors are one of the most important tools in Linux systems. This chapter introduces several commonly used command-line text editors: Vim, Nano, and others.
Vim Editor
Vim (Vi IMproved) is one of the most powerful text editors, an enhanced version of the Vi editor.
Why Learn Vim?
- Pre-installed on almost all Linux systems
- Efficient keyboard operations
- Powerful text processing capabilities
- Highly customizable
- Programming support
Installing Vim
# Debian/Ubuntu
$ sudo apt install vim
# Fedora
$ sudo dnf install vim
# Arch Linux
$ sudo pacman -S vimVim Modes
Vim is a modal editor with the following main modes:
┌─────────────────────────────────────────────────────────────┐
│ │
│ Normal Mode (Normal) │
│ (Default mode) │
│ │ │
│ ┌───────────────┼───────────────┐ │
│ │ │ │ │
│ i,a,o等 v,V,Ctrl+v : │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Insert │ │ Visual │ │ Command │ │
│ │ (Insert) │ │ (Visual) │ │(Command) │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │ │ │ │
│ Esc Esc Enter/Esc │
│ │ │ │ │
│ └───────────────┴───────────────┘ │
│ │ │
│ ▼ │
│ Normal Mode │
│ │
└─────────────────────────────────────────────────────────────┘Starting and Exiting
# Open file
$ vim filename.txt
# Open to specific line
$ vim +10 filename.txt
# Open and search
$ vim +/pattern filename.txt
# Open in read-only mode
$ vim -R filename.txt
$ view filename.txtExiting Vim
| Command | Function |
|---|---|
:q | Quit (unmodified) |
:q! | Force quit (discard changes) |
:w | Save |
:wq | Save and quit |
:x | Save and quit (same as :wq) |
ZZ | Save and quit (normal mode) |
ZQ | Force quit (normal mode) |
Normal Mode Operations
Cursor Movement
| Key | Function |
|---|---|
h | Move left one character |
j | Move down one line |
k | Move up one line |
l | Move right one character |
w | Next word beginning |
b | Previous word beginning |
e | Current/next word ending |
0 | Line beginning |
^ | Line beginning (non-whitespace) |
$ | Line end |
gg | File beginning |
G | File end |
5G | Jump to line 5 |
Ctrl+f | Next page |
Ctrl+b | Previous page |
Ctrl+d | Down half page |
Ctrl+u | Up half page |
Enter Insert Mode
| Key | Function |
|---|---|
i | Insert before cursor |
I | Insert at line beginning |
a | Append after cursor |
A | Append at line end |
o | Create new line below |
O | Create new line above |
s | Delete character and insert |
S | Delete entire line and insert |
Delete Operations
| Key | Function |
|---|---|
x | Delete character at cursor |
X | Delete character before cursor |
dd | Delete entire line |
dw | Delete to word end |
d$ or D | Delete to line end |
d0 | Delete to line beginning |
dG | Delete to file end |
dgg | Delete to file beginning |
5dd | Delete 5 lines |
Copy and Paste
| Key | Function |
|---|---|
yy | Copy entire line |
yw | Copy word |
y$ | Copy to line end |
5yy | Copy 5 lines |
p | Paste after cursor/next line |
P | Paste before cursor/previous line |
Undo and Redo
| Key | Function |
|---|---|
u | Undo |
U | Undo entire line changes |
Ctrl+r | Redo |
. | Repeat last operation |
Search and Replace
# Search
/pattern # Search forward
?pattern # Search backward
n # Next match
N # Previous match
* # Search word under cursor
# Replace (command mode)
:s/old/new/ # Replace first occurrence on current line
:s/old/new/g # Replace all on current line
:%s/old/new/g # Replace all in entire file
:%s/old/new/gc # Replace all in entire file, confirm each
:5,10s/old/new/g # Replace lines 5-10Visual Mode
v # Character selection
V # Line selection
Ctrl+v # Block selection (rectangular)
# After selection, you can execute operations
d # Delete selected content
y # Copy selected content
> # Indent right
< # Indent leftCommand Mode
:w filename # Save as
:r filename # Read file content
:e filename # Open another file
:!command # Execute external command
:r !command # Insert command output
:set number # Show line numbers
:set nonumber # Hide line numbers
:set hlsearch # Highlight search results
:set nohlsearch # Disable highlighting
:noh # Temporarily disable highlightingMultiple Window Operations
:split filename # Horizontal split
:vsplit filename # Vertical split
:sp # Horizontal split current file
:vsp # Vertical split current file
Ctrl+w w # Switch windows
Ctrl+w h/j/k/l # Switch left/down/up/right
Ctrl+w c # Close current window
Ctrl+w o # Keep only current windowMultiple Tabs
:tabnew filename # Open file in new tab
:tabn # Next tab
:tabp # Previous tab
gt # Next tab
gT # Previous tab
:tabclose # Close tabCommon Configuration
Edit ~/.vimrc:
" Show line numbers
set number
" Syntax highlighting
syntax on
" Auto indent
set autoindent
set smartindent
" Tab settings
set tabstop=4
set shiftwidth=4
set expandtab
" Search settings
set hlsearch
set incsearch
set ignorecase
set smartcase
" Show cursor position
set ruler
" Highlight current line
set cursorline
" Show matching bracket
set showmatch
" Encoding settings
set encoding=utf-8
" Enable mouse
set mouse=a
" Theme
colorscheme desertNano Editor
Nano is a simple and easy-to-use text editor, suitable for beginners.
Installing Nano
# Usually pre-installed, install if needed
$ sudo apt install nanoBasic Usage
# Open file
$ nano filename.txt
# Open to specific line
$ nano +10 filename.txtCommon Shortcuts
Screen bottom shows common shortcuts, ^ represents Ctrl key.
| Shortcut | Function |
|---|---|
Ctrl+O | Save file |
Ctrl+X | Exit |
Ctrl+K | Cut entire line |
Ctrl+U | Paste |
Ctrl+W | Search |
Ctrl+\ | Search and replace |
Ctrl+G | Show help |
Ctrl+C | Show cursor position |
Ctrl+_ | Jump to specific line |
Alt+U | Undo |
Alt+E | Redo |
Ctrl+A | Jump to line beginning |
Ctrl+E | Jump to line end |
Alt+\ | Jump to file beginning |
Alt+/ | Jump to file end |
Selection and Copy
Alt+A # Start selection
Move cursor # Extend selection
Ctrl+K # Cut selected content
Alt+6 # Copy selected content
Ctrl+U # PasteNano Configuration
Edit ~/.nanorc:
# Show line numbers
set linenumbers
# Auto indent
set autoindent
# Tab to spaces
set tabstospaces
set tabsize 4
# Smooth scrolling
set smooth
# Enable mouse
set mouse
# Syntax highlighting (usually enabled by default)
include "/usr/share/nano/*.nanorc"Other Editors
Emacs
Powerful extensible editor, feature-rich.
# Install
$ sudo apt install emacs
# Start
$ emacs filename.txt
# Terminal mode
$ emacs -nw filename.txtBasic Operations
Ctrl+x Ctrl+s- SaveCtrl+x Ctrl+c- ExitCtrl+g- Cancel command
Micro
Modern terminal editor, easy to use.
# Install
$ sudo apt install micro
# Use
$ micro filename.txtFeatures:
- Conventional editor shortcuts (Ctrl+S save, Ctrl+Q quit)
- Syntax highlighting
- Mouse support
- Plugin system
ne (Nice Editor)
Simple and easy-to-use editor.
$ sudo apt install ne
$ ne filename.txtEditor Selection Recommendations
| Scenario | Recommended |
|---|---|
| Quick config file editing | Nano |
| Daily development work | Vim / VS Code |
| Remote server editing | Vim |
| System administration tasks | Vim / Nano |
| Learning Unix culture | Vim / Emacs |
Vim Learning Tips
Use vimtutor
bash$ vimtutorInteractive tutorial, learn basics in 30 minutes.
Step by step
- Week 1: Learn movement and basic editing
- Week 2: Learn search and replace
- Week 3: Learn visual mode and windows
- Continue: Learn more advanced tips
Learn by practicing
- Force yourself to use Vim
- When you encounter repetitive operations, look for better methods
Summary
This chapter introduced commonly used Linux text editors:
- Vim: Powerful, steep learning curve, but extremely efficient
- Nano: Simple and easy to use, suitable for beginners and quick editing
- Other options: Emacs, Micro, etc.
It's recommended to master basic operations of at least one command-line editor; Vim basics are particularly useful for system administration work.
Previous chapter: Pipes and Filters
Next chapter: Text Processing Tools