Editing Files with nano and vim Basics

beginner
12 min

Editing Files with nano and vim Basics

Sooner or later you must change a file on a machine that has no graphical editor—a server over SSH, a container, a rescue shell. Two editors are almost always installed: nano, which is friendly and obvious, and vim, which is powerful and everywhere. This lesson gets you productive in nano and, crucially, teaches you how to survive (and exit!) vim.

nano: The Friendly Editor

Open or create a file:

bash
nano notes.txt nano /etc/hosts # add sudo if the file is root-owned

nano works like a plain notepad: type to insert, arrows to move. The shortcuts listed along the bottom use ^ for Ctrl:

  • Ctrl+O – write Out (save); confirm the filename with Enter
  • Ctrl+X – exit (prompts to save if there are changes)
  • Ctrl+K / Ctrl+U – cut the current line / paste it
  • Ctrl+W – search (Where is); Alt+W repeats the search
  • Ctrl+\ – search and replace
  • Ctrl+G – help

That is genuinely all you need. For quick config edits on a server, nano is a perfectly professional choice.

vim: Why Bother?

vim (Vi IMproved) has a steeper curve because it is a modal editor: keys mean different things depending on the mode you are in. The payoff is that editing becomes a language—"delete 3 words" is d3w—and your hands never leave the home row. Even if you stay a nano person, you must know vim basics because vi is the only editor guaranteed on minimal systems, and some tools (like git or visudo) may drop you into it.

vim Survival Kit

bash
vim notes.txt

vim opens in Normal mode, where letters are commands, not text. The five things everyone must know:

  1. i – enter Insert mode (now you can type text)
  2. Esc – return to Normal mode
  3. :w – save (write)
  4. :q – quit; :wq saves and quits
  5. :q! – quit WITHOUT saving (the famous escape hatch)

If the editor ever seems possessed—text not appearing, beeps, strange jumps—press Esc a couple of times and you are back in known territory.

Useful Normal-Mode Commands

Once the survival kit feels comfortable, these commands start paying rent:

text
h j k l move left / down / up / right (arrows also work) w b jump forward / back one word 0 $ start / end of line gg G top / bottom of file dd delete (cut) the current line yy yank (copy) the current line p paste below the cursor u undo Ctrl+r redo /error search forward; n = next match x delete the character under the cursor

Commands compose: d$ deletes to end of line, 5dd deletes five lines, y3w copies three words.

Search and Replace in vim

text
:%s/localhost/127.0.0.1/g replace in the whole file :s/foo/bar/ replace first match on this line :%s/tmp/temp/gc g = all matches, c = confirm each

Which Editor Should You Use?

  • Editing one line of a config over SSH? nano — zero friction.
  • Spending hours a day editing code in a terminal? Learning vim pays off within weeks.
  • On a minimal system where nano is missing? The vim survival kit saves you.

Set your preferred editor for tools that ask: export EDITOR=nano (in .bashrc, covered in lesson 12).

Practice Session

bash
nano practice.txt # type a few lines, Ctrl+O, Enter, Ctrl+X vim practice.txt # press i, add a line, Esc, then :wq vimtutor # vim built-in 30-minute interactive course

vimtutor is the single best way to learn vim—it is an actual file you edit as you read it.

Key Takeaways

  • nano: type freely, Ctrl+O saves, Ctrl+X exits, Ctrl+W searches.
  • vim is modal: i to type, Esc to command, :wq to save-quit, :q! to bail out.
  • Normal-mode commands compose like a language: dd, yy, p, u, /search.
  • Know both: nano for quick edits, vim for minimal systems and serious terminal work.

Next lesson: Process Management: ps, top, kill — see and control what is running.

Editing Files with nano and vim Basics - Linux & Command Line | CodeYourCraft | CodeYourCraft