Linux: Text Editors — nano Basics and vim Fundamentals

On a Linux server, there's no VS Code GUI. You can only rely on terminal text editors to modify configuration files and write code. The two most common are nano (zero learning curve) and vim (efficiency king).

📋 Prerequisites: You should first complete

1. What You Will Learn


2. A Story About Editing Files in the Terminal

(1) The Pain: Can't Use VS Code After SSH Login

Alex logged into a server via SSH and wanted to edit the Nginx configuration file, but the server had no VS Code and no GUI. He'd heard of vim but didn't know how to use it — after opening it, he couldn't even figure out how to exit.

Senior engineer Bob taught him the essential key sequences: i to enter insert mode and start writing, Esc to return to normal mode, :wq to save and quit.

(2) The Three-Step Method

Bob said: "Remember these three steps and you can edit any file with vim."

💡 Tip: vim's core is the "three-step method" — i to enter insert mode and start editing, Esc to return to normal mode, :wq to save and quit. With just these three steps, you can handle most editing tasks in vim; the rest you can learn gradually.

BASH
vim /etc/nginx/nginx.conf
# 1. Press i to enter insert mode — start editing
# 2. After editing, press Esc to return to normal mode
# 3. Type :wq and press Enter — save and quit

(3) The Benefit: No Longer Afraid to Edit Configuration Files

After learning vim basics, Alex found that editing server configs was actually fast — open the file, find the line to change, press i to modify, :wq to save. The whole process takes less than 30 seconds.


3. Knowledge Points

(1) nano — The Zero-Barrier Editor

nano is a simple editor pre-installed on all Linux systems. Its operation hints are displayed at the bottom of the screen.

ℹ️ Note: If you only occasionally edit a configuration file, nano is perfectly adequate with zero learning cost; if you need to edit text extensively every day, vim is far more efficient. Start with nano, and switch to vim when you feel nano isn't fast enough.

Opening a file:

BASH
nano filename
nano /etc/nginx/nginx.conf   # When sudo is needed: sudo nano /etc/nginx/nginx.conf

Common shortcuts (^ means the Ctrl key):

Shortcut Action
Ctrl + O Save file
Ctrl + X Exit nano
Ctrl + W Search text
Ctrl + K Cut current line
Ctrl + U Paste
Ctrl + G Show help
Alt + U Undo
Alt + E Redo

(2) vim's Three Modes

vim's biggest feature is mode-driven — the same key has different functions in different modes.

TEXT

┌─────────────────────────────┐
│         Normal Mode          │ ← Default mode on startup, for movement and operations
│  Press i / a / o → Insert Mode   │
│  Press v / V → Visual Mode       │
│  Press : → Command-line Mode     │
└──────────────┬──────────────┘
               │
    ┌──────────┴──────────┐
    ↓                     ↓
┌──────────────┐   ┌──────────────┐
│  Insert Mode │   │  Visual Mode │
│              │   │              │
│  Press Esc → │   │  Press Esc → │
│  Back to Normal│   │  Back to Normal│
└──────────────┘   └──────────────┘

(3) vim Cursor Movement (Normal Mode)

TEXT
h       ← Move left one character
j       ↓ Move down one line
k       ↑ Move up one line
l       → Move right one character
w       Jump to the start of the next word
b       Jump to the start of the previous word
0       Jump to the beginning of the line
$       Jump to the end of the line
gg      Jump to the beginning of the file
G       Jump to the end of the file
:line_number    Jump to a specific line (e.g., :42 jumps to line 42)
Ctrl+F  Scroll down one page
Ctrl+B  Scroll up one page
💡 Tip: In vim, number+command repeats the action — 3dd deletes 3 lines, 5yy copies 5 lines, 10j moves down 10 lines. This is one key to vim's efficiency: use count prefixes to avoid repeated keystrokes.

(4) vim Editing Operations (Normal Mode)

TEXT
i       Insert before cursor (enter insert mode)
a       Insert after cursor
o       Open a new line below and enter insert mode
O       Open a new line above and enter insert mode

x       Delete character under cursor
dd      Delete current line
dw      Delete a word
d$      Delete to end of line
yy      Copy current line (yank)
yw      Copy a word
p       Paste after cursor
P       Paste before cursor
u       Undo
Ctrl+R  Redo

(5) vim Search and Replace

TEXT
# Search (normal mode)
/pattern      Search forward for pattern
?pattern      Search backward for pattern
n             Jump to next match
N             Jump to previous match

# Replace (command-line mode)
:%s/old/new/g          Replace all "old" with "new" in the entire file
:%s/old/new/gc         Replace all with confirmation for each match
:5,20s/old/new/g       Replace in lines 5-20

(6) ▶ Example: Edit a File with nano

BASH
# Create and edit a file
nano hello.txt

# In nano:
# 1. Type "Hello Linux!"
# 2. Ctrl + O → press Enter to save
# 3. Ctrl + X → exit nano

# View file contents
cat hello.txt

Output:

TEXT
Hello Linux!

(7) ▶ Example: vim Basic Operations

TEXT
# Open a file
vim practice.txt

# Practice sequence:
# 1. Press i → enter insert mode
# 2. Type "This is line 1" → press Enter
# 3. Type "This is line 2"
# 4. Press Esc → return to normal mode
# 5. Type :wq → press Enter to save and quit

(8) ▶ Example: vim Copy and Paste

TEXT
# 1. Open a file with vim
vim practice.txt

# 2. yy to copy current line
# 3. p to paste below
# 4. Press p repeatedly to paste multiple times
# 5. 5yy to copy 5 lines
# 5p to paste 5 times

# 6. dd to delete current line
# 7. 3dd to delete 3 lines

(9) ▶ Example: vim Search and Replace

TEXT
vim config.txt

# Search for nginx
/nginx
n    # Next match
N    # Previous match

# Replace all "port" with "PORT"
:%s/port/PORT/g

# Replace with confirmation
:%s/port/PORT/gc
# Each match will prompt: y confirm / n skip / a all

(10) ▶ Example: vim Configuration (~/.vimrc)

Create a simple .vimrc configuration file to make vim more user-friendly:

TEXT
cat > ~/.vimrc << 'EOF'
syntax on                          " Syntax highlighting
set number                         " Show line numbers
set tabstop=2                      " Tab width 2
set shiftwidth=2                   " Indent width 2
set expandtab                      " Use spaces instead of tabs
set autoindent                     " Auto indent
set cursorline                     " Highlight current line
set hlsearch                       " Highlight search results
set ignorecase                     " Case-insensitive search
set incsearch                      " Incremental search
EOF

(11) ▶ Comprehensive Example: Edit an Nginx Configuration File with vim

BASH
# Open the Nginx default site configuration
sudo vim /etc/nginx/sites-available/default

# Operation steps:
# 1. /server_name  Search for server_name
# 2. n Jump to next occurrence
# 3. i Enter insert mode, modify the domain
# 4. Esc Exit insert mode
# 5. /root  Search for root path
# 6. Modify the website root directory
# 7. :wq Save and quit
# 8. sudo nginx -t Test configuration
# 9. sudo systemctl reload nginx Reload configuration

❓ FAQ

Q: How do I exit vim (the classic question)? A: In order: Esc (ensure you're in normal mode) → : (enter command-line mode) → q (quit) or q! (force quit without saving) or wq (save and quit).

Q: Should I learn nano or vim? A: Start with nano (zero barrier, hints at the bottom), then gradually learn vim. Once you're comfortable with vim, it's far more efficient — but for "occasionally editing a config file," nano is perfectly adequate.

Q: How do I undo in vim? A: Press u in normal mode to undo, Ctrl + R to redo. Pressing u repeatedly undoes multiple changes.

Q: How do I show line numbers in vim? A: Type :set number in command-line mode. To show them by default, add set number to ~/.vimrc.

Q: Is there a better way to edit files remotely? A: The VS Code Remote - SSH extension lets you edit remote files in your local VS Code. JetBrains Gateway offers similar functionality. But you won't always have these tools — learning vim basics is still an essential skill.


📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Use nano to create and edit hello.txt, write three lines of text, save and exit, then open the same file in vim and practice cursor movement (h/j/k/l/w/b/gg/G)
  2. Intermediate (Difficulty ⭐⭐): In vim, copy the current line (yy) and paste (p) 5 times, then search for a word in the file and jump with n/N, and finally do a replacement with :%s/old/new/g
  3. Advanced (Difficulty ⭐⭐⭐): Create a ~/.vimrc configuration file (syntax highlighting + line numbers + Tab width 2 + search highlighting), then use vim to edit /etc/ssh/sshd_config (requires sudo), practicing search and replace operations
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏