Skip to content

Editing Files

Let's learn how to edit files without leaving the terminal.

When you need to change a file, you need a text editor. Linux has many terminal-based editors. Here are the most common ones.

Built-in editors: Comparison

Editor Learning Curve Best For Key Feature
Nano Low (beginner-friendly) Beginners, quick config edits Shortcuts shown at the bottom
Vim High (steep learning curve) Power users, developers Modal editing (super fast once learned)

Recommendation: Start with Nano. It's installed everywhere and works like a simple notepad. You can learn Vim later if you want to.


Nano Editor

nano myfile.txt
  • If myfile.txt exists, Nano opens it.
  • If it doesn't exist, Nano creates it when you save.

Essential Nano Shortcuts

When time comes you will get into the rabbit whole of editors. For now here are some shortcuts.

Shortcut What it does
Ctrl+X Exit (asks to save if changes exist)
Ctrl+O Save ("Write Out" the file)
Ctrl+W Search for text
Ctrl+K Cut the current line
Ctrl+U Paste the cut line
Ctrl+_ Go to a specific line number
Ctrl+C Show cursor position (line/column)
Ctrl+G Open help menu

Lost in Nano?

Look at the bottom two lines of your terminal. Nano shows shortcuts there.

Vim Editor

Optional - Want more power? Try Vim

Vim is another terminal editor. If it's your first time you will feel lost using it. It has a steep learning curve but is very powerful.

Vim is a modal editor, meaning it has different "modes" for different tasks. This is the biggest difference from Nano and the main reason for its steep learning curve.

vi myfile.txt
  • If myfile.txt exists, Vim opens it.
  • If it doesn't exist, Vim creates it when you save.

A short follow along:

Step Action
1 Press i (enter Insert mode)
2 Type Hello World from Vim
3 Press Esc (back to Normal mode)
4 Type :wq + Enter (save and quit)

Vim deserves a full course since it has many many features. There's a meme that people use Vim to this day because they were never able to quit Vim.