Linux··3 min

Text Editors

At some point you will need to edit a file directly in the terminal. Knowing how to use a terminal text editor is not optional if you want to work with Linux seriously. This post covers nano and vim.

At some point you will need to edit a file directly in the terminal. A configuration file, a script, a cron job. Knowing how to use a terminal text editor is not optional if you want to work with Linux seriously.

There are several options. This post covers two — nano and vim. Start with nano. Learn vim later when you are ready.

nano

nano is the friendliest terminal text editor. It works like you would expect a text editor to work. You open a file, you type, you save, you exit.

nano filename.txt

If the file does not exist, nano creates it. If it does, nano opens it.

Once inside nano, you just type. No modes, no special commands to enter before you can start writing.

At the bottom of the screen you will always see a list of available commands. The ^ symbol means Ctrl.

The ones you need to know:

  • Ctrl + O — save the file (Write Out)
  • Ctrl + X — exit nano
  • Ctrl + K — cut the current line
  • Ctrl + U — paste the cut line
  • Ctrl + W — search for text

To save and exit: press Ctrl + O, confirm the filename by pressing Enter, then press Ctrl + X.

A quick example

nano notes.txt

Type something. Press Ctrl + O, then Enter to confirm. Press Ctrl + X to exit. The file is saved.

cat notes.txt

cat prints the contents of a file to the terminal. You will see whatever you typed.

vim

vim has a reputation for being impossible to use. That reputation exists because vim works completely differently from any text editor you have used before. But once it clicks, it is extremely powerful.

The key thing to understand about vim is that it has modes.

When you open vim, you are in Normal mode. In Normal mode, every key on your keyboard is a command. You cannot just start typing.

To start typing, you need to enter Insert mode by pressing i. Now you can type normally. Press Esc to go back to Normal mode.

Opening and exiting vim

vim filename.txt

To exit vim — the question everyone asks at least once:

  • Press Esc to make sure you are in Normal mode
  • Type :q and press Enter to quit (only works if you have not made changes)
  • Type :wq and press Enter to save and quit
  • Type :q! and press Enter to quit without saving

Basic vim workflow

vim myfile.txt     # open the file
i                  # enter insert mode
# type your changes
Esc                # return to normal mode
:wq                # save and exit

Why bother learning vim?

vim is available on almost every Linux system by default, including servers that have no graphical interface and minimal software installed. If you ever SSH into a remote machine and need to edit a file, vim will probably be there. nano might not be.

You do not need to master vim. Knowing how to open a file, make a change, and exit is enough to start with.


For everyday editing on your own machine, use nano. It is simple and it works. Start learning vim slowly on the side — a few commands at a time. It is worth the investment.

Next up: shell scripting — how to write scripts that automate tasks you would otherwise type manually.

1 views