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 vim
Vim 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.txt
Exiting Vim
Normal Mode Operations
Cursor Movement
Enter Insert Mode
Delete Operations
Copy and Paste
Undo and Redo
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-10
Visual 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 left
Command 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 highlighting
Multiple 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 window
Multiple Tabs
:tabnew filename # Open file in new tab
:tabn # Next tab
:tabp # Previous tab
gt # Next tab
gT # Previous tab
:tabclose # Close tab
Common 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 desert
Nano 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 nano
Basic Usage
# Open file
$ nano filename.txt
# Open to specific line
$ nano +10 filename.txt
Common Shortcuts
Screen bottom shows common shortcuts, ^ represents Ctrl key.
Selection and Copy
Alt+A # Start selection
Move cursor # Extend selection
Ctrl+K # Cut selected content
Alt+6 # Copy selected content
Ctrl+U # Paste
Nano 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.txt
Basic Operations
Ctrl+x Ctrl+s - Save
Ctrl+x Ctrl+c - Exit
Ctrl+g - Cancel command
Micro
Modern terminal editor, easy to use.
# Install
$ sudo apt install micro
# Use
$ micro filename.txt
Features:
- 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.txt
Editor Selection Recommendations
Vim Learning Tips
-
Use vimtutor
Interactive 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