guest@linuxthings:~/guides$ man editors
Text Editors
Every Linux system has at least one terminal-based text editor. Learn the basics of both nano and vim: other programs (such as crontab -e, git commit and visudo) may drop you into either one.
Choosing Your Default Editor
export EDITOR=nano # Add to ~/.bashrc to make it permanent
export VISUAL=nano # Some programs check this one instead
sudo update-alternatives --config editor # System default on Debian/Ubuntunano
The shortcuts are listed along the bottom of the screen. ^ means Ctrl and M- means Alt.
- Ctrl+O: Save (Write Out)
- Ctrl+X: Exit
- Ctrl+K: Cut line
- Ctrl+U: Uncut (Paste) line
- Ctrl+W: Search (Alt+W for the next match)
- Ctrl+\: Search and replace
- Ctrl+_: Go to a line number
- Alt+U: Undo
- Alt+E: Redo
- Alt+A: Start selecting text (then Ctrl+K to cut it)
- Ctrl+G: Help
nano file.txt
nano -l file.txt # Show line numbers
nano +25 file.txt # Open at line 25vim (vi)
Vim has modes. Normal mode is for moving around and running commands, Insert mode is for typing text. If you are ever unsure, press Esc to get back to Normal mode.
- i: Enter Insert Mode (to type text)
- a: Insert after the cursor
- o: Open a new line below and insert
- Esc: Return to Normal Mode
- :w: Save
- :q: Quit
- :wq: Save and Quit
- :q!: Quit without saving
- dd: Delete line
- u: Undo
- Ctrl+R: Redo
vim: Moving Around
Keys | Move |
|---|---|
h j k l | Left, down, up, right |
w / b | Next / previous word |
0 / $ | Start / end of the line |
gg / G | First / last line of the file |
42G or :42 | Line 42 |
Ctrl+F / Ctrl+B | Page down / page up |
vim: Editing
Keys | Action |
|---|---|
x | Delete the character under the cursor |
dw | Delete a word |
D | Delete to the end of the line |
yy | Copy (yank) the current line |
p / P | Paste after / before the cursor |
v / V | Select characters / whole lines (then d, y or >) |
. | Repeat the last change |
5dd | Delete 5 lines (most commands take a count) |
vim: Search and Replace
/pattern # Search forward (n = next, N = previous)
?pattern # Search backward
:%s/old/new/g # Replace every match in the file
:%s/old/new/gc # ...asking for confirmation each time
:set number # Show line numbers
:set nonumber # Hide them againRun vimtutor for a 30-minute interactive lesson.
Editing Protected Files
sudoedit /etc/hosts # Edit a copy as yourself, saved back as root
sudo nano /etc/hosts # Simpler, but runs the whole editor as root
sudo visudo # Always use this for /etc/sudoers