Skip to content

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

bash
# 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

bash
# 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

CommandFunction
:qQuit (unmodified)
:q!Force quit (discard changes)
:wSave
:wqSave and quit
:xSave and quit (same as :wq)
ZZSave and quit (normal mode)
ZQForce quit (normal mode)

Normal Mode Operations

Cursor Movement

KeyFunction
hMove left one character
jMove down one line
kMove up one line
lMove right one character
wNext word beginning
bPrevious word beginning
eCurrent/next word ending
0Line beginning
^Line beginning (non-whitespace)
$Line end
ggFile beginning
GFile end
5GJump to line 5
Ctrl+fNext page
Ctrl+bPrevious page
Ctrl+dDown half page
Ctrl+uUp half page

Enter Insert Mode

KeyFunction
iInsert before cursor
IInsert at line beginning
aAppend after cursor
AAppend at line end
oCreate new line below
OCreate new line above
sDelete character and insert
SDelete entire line and insert

Delete Operations

KeyFunction
xDelete character at cursor
XDelete character before cursor
ddDelete entire line
dwDelete to word end
d$ or DDelete to line end
d0Delete to line beginning
dGDelete to file end
dggDelete to file beginning
5ddDelete 5 lines

Copy and Paste

KeyFunction
yyCopy entire line
ywCopy word
y$Copy to line end
5yyCopy 5 lines
pPaste after cursor/next line
PPaste before cursor/previous line

Undo and Redo

KeyFunction
uUndo
UUndo entire line changes
Ctrl+rRedo
.Repeat last operation

Search and Replace

bash
# 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

bash
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

bash
: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

bash
: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

bash
: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:

vim
" 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

bash
# Usually pre-installed, install if needed
$ sudo apt install nano

Basic Usage

bash
# Open file
$ nano filename.txt

# Open to specific line
$ nano +10 filename.txt

Common Shortcuts

Screen bottom shows common shortcuts, ^ represents Ctrl key.

ShortcutFunction
Ctrl+OSave file
Ctrl+XExit
Ctrl+KCut entire line
Ctrl+UPaste
Ctrl+WSearch
Ctrl+\Search and replace
Ctrl+GShow help
Ctrl+CShow cursor position
Ctrl+_Jump to specific line
Alt+UUndo
Alt+ERedo
Ctrl+AJump to line beginning
Ctrl+EJump to line end
Alt+\Jump to file beginning
Alt+/Jump to file end

Selection and Copy

bash
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:

bash
# 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.

bash
# 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.

bash
# 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.

bash
$ sudo apt install ne
$ ne filename.txt

Editor Selection Recommendations

ScenarioRecommended
Quick config file editingNano
Daily development workVim / VS Code
Remote server editingVim
System administration tasksVim / Nano
Learning Unix cultureVim / Emacs

Vim Learning Tips

  1. Use vimtutor

    bash
    $ vimtutor

    Interactive tutorial, learn basics in 30 minutes.

  2. 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
  3. 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

Content is for learning and research only.