Skip to content
elephantoo

Text editors: nano & vim basics

Lesson 7 of 31 15 min read

Edit config files with nano, survive and thrive in vim, and set your default editor.


Sooner or later every Linux user has to edit a file in the terminal: a config file on a server, a crontab, a commit message, a script. There's no mouse-driven GUI on a server, so you need a terminal text editor. You'll learn two: nano, which is simple and beginner-friendly, and vim, which is powerful, everywhere, and well worth knowing the basics of.

nano: the friendly editor#

Terminal
nano notes.txt              # open (or create) a file
nano -l /etc/hosts          # show line numbers
sudo nano /etc/ssh/sshd_config   # system files need sudo

nano works like a normal editor: just start typing. The bottom two lines list the shortcuts, where ^ means Ctrl and M- means Alt:

KeysAction
Ctrl+O, then Entersave ("Write Out")
Ctrl+Xexit (asks to save if there are changes)
Ctrl+Wsearch ("Where is"); Alt+W finds the next match
Ctrl+\search and replace
Ctrl+K / Ctrl+Ucut the current line / paste
Alt+U / Alt+Eundo / redo
Ctrl+_go to a line number
Alt+Astart selecting text (then move, then Ctrl+K to cut)
Ctrl+Ghelp

That's genuinely all you need to edit config files. A good habit before editing system files is to keep a backup copy:

Terminal
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config

vim: the editor that's everywhere#

vi (and its improved version vim, or the modern fork neovim) is installed on virtually every Unix-like system, including minimal servers and containers where nano might be missing. Many tools (git commit, crontab -e, visudo) may open it by default. Knowing the basics means you'll never be stuck.

Terminal
vim notes.txt

The key idea: modes

vim is a modal editor. Keys do different things depending on the mode:

ModeHow to enterWhat keys do
NormalEsc (it's where vim starts)move, delete, copy, paste: keys are commands
Inserti, a, o, …keys type text, as in any editor
Command-line: from normal modesave, quit, search and replace, settings
Visualv (characters), V (lines)select text, then act on it

If you're ever confused, press Esc (twice is fine) to get back to normal mode.

Survival kit

Keys (from normal mode)Action
iinsert before the cursor
ainsert after the cursor
o / Oopen a new line below / above and insert
Escback to normal mode
:w Entersave
:q Enterquit (only if saved)
:wq or ZZsave and quit
:q!quit without saving

Your first edit, step by step: vim hello.txt → press i → type Hello from vim → press Esc → type :wq → Enter. Done.

Moving around

KeysMoves
h j k lleft, down, up, right (arrow keys work too)
w / bnext / previous word
0 / $start / end of line
gg / Gfirst / last line
42G or :42line 42
Ctrl+d / Ctrl+uhalf a page down / up

Editing in normal mode

These are vim's superpower. Commands combine a verb with a motion and an optional count:

KeysAction
xdelete the character under the cursor
dddelete (cut) the line; 3dd cuts 3 lines
dwdelete to the next word
d$ or Ddelete to the end of the line
yyyank (copy) the line; yw copies a word
p / Ppaste after / before the cursor
cwchange a word (delete it and enter insert mode)
u / Ctrl+rundo / redo
.repeat the last change
>> / <<indent / unindent the line

So d3w deletes three words and y$ copies to the end of the line. Once the grammar clicks, editing gets very fast.

Search and replace

Output
/timeout          search forward for "timeout"; n = next, N = previous
?timeout          search backward
:%s/http/https/g  replace every "http" with "https" in the whole file
:%s/old/new/gc    the same, but confirm each replacement
:10,20s/^/# /     comment out lines 10–20 by adding "# " at the start

Handy settings

Type these in command-line mode, or put them (without the :) in ~/.vimrc to make them permanent:

Output
:set number         show line numbers
:set paste          paste text without auto-indent mangling it (:set nopaste afterwards)
:syntax on          syntax highlighting
:set ignorecase smartcase   case-insensitive search unless you type capitals

To learn properly, run vimtutor: a 30-minute interactive lesson built into vim.

Choosing your default editor#

Commands like crontab -e, git commit and sudoedit open the editor named in the EDITOR (or VISUAL) environment variable:

Terminal
export EDITOR=nano                   # for this session
echo 'export EDITOR=nano' >> ~/.bashrc   # permanently

On Debian/Ubuntu, sudo update-alternatives --config editor sets the system default, and select-editor sets your own default for some tools.

Editing system files safely#

  • sudoedit /etc/file (or sudo -e) copies the file to a temporary location, opens your editor as your user, and writes it back as root when you save. It's safer than running a whole editor as root.
  • visudo for /etc/sudoers and crontab -e for cron jobs check your syntax before saving. Always use them instead of editing those files directly.
  • Validate before restarting services: sudo nginx -t, sudo sshd -t, sudo apachectl configtest. A typo in sshd_config can lock you out of a server.

Other editors#

  • micro: a modern terminal editor with familiar Ctrl+S / Ctrl+C / Ctrl+V keys (sudo apt install micro).
  • emacs: the other legendary editor, endlessly extensible.
  • VS Code Remote-SSH: edit files on a server from your desktop editor. It's great for development, but you'll still want nano or vim for quick fixes.

Common mistakes#

  • Getting stuck in vim. Press Esc, then type :q! and Enter to quit without saving, or :wq to save and quit.
  • Typing commands in Insert mode in vim, so :wq ends up as text in the file. Press Esc first.
  • Editing system files without sudo, then being unable to save. Use sudo nano /etc/file, or better, sudoedit /etc/file.
  • Breaking a config file and restarting the service blindly. Make a backup copy first (sudo cp file file.bak) and use the program's config test (e.g. sudo nginx -t).

What's next#

You can now read and edit any file. Next: who's allowed to read and edit them, with permissions and ownership.

Check your understanding

Quick quiz

0/3 answered
  1. 1.In nano, the help bar shows ^O Write Out. What does ^ mean?

  2. 2.You opened a file in vim by accident and want to leave WITHOUT saving. What do you type?

  3. 3.In vim's normal mode, what does dd do?

Finished reading?

Mark this lesson complete to track your progress.