Text editing in the terminal: the basics of nano and vim

Linux 4 min read

Most of the server settings are contained in text configuration files, which must be edited via SSH in the terminal. Two text editors are commonly used for this: the simple nano, recommended for beginners, and vim (or vi), which is found on almost every Linux system but may seem unfamiliar at first.

Before you edit a configuration file

Make a copy of the original so that you can restore it in the event of an error:

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

You need root privileges to edit system files, so please run the editor using sudo.

nano

Installation

nano was not installed on the AlmaLinux 9 and 10 cloud images during testing; it was present on Ubuntu 24.04 and Debian 12 (version 7.2), where it is also the default editor. If the nano command cannot be found:

# AlmaLinux, Rocky Linux
sudo dnf install nano

# Ubuntu, Debian
sudo apt install nano

Usage

sudo nano /etc/hosts

The contents of the file are displayed, and you can start typing straight away by using the arrow keys. The most common commands are shown at the bottom of the screen; the ^ symbol represents the Ctrl key, and the M- symbol represents the Alt key.

Key Action
Ctrl+O followed by Enter save
Ctrl+X Exit (if you haven’t saved, you’ll be asked: Y for yes, N for no)
Ctrl+W Search (next result: Alt+W)
Ctrl+\ Search and replace
Ctrl+K Cut the current line
Ctrl+U Insert the cut-out line
Alt+U cancellation
Ctrl+_ Jump to a specific line (e.g. the line number specified in an error message)
Alt+N Enabling and disabling serial numbers

You can copy text from your own computer using the terminal’s standard paste function (right-click in Windows Terminal and PuTTY, Cmd+V on macOS, Ctrl+Shift+V on Linux).

vim

Vim is available on every server (at least under the name vi), and on AlmaLinux and Rocky Linux, several commands (such as crontab -e or visudo) open it by default. If you unexpectedly find yourself in this situation, it is worth familiarising yourself with this section.

On AlmaLinux 9 and 10, only the vim-minimal package is installed by default, which provides the vi command; there is no command called vim (‘command not found’). Use the vi command (the keys listed below work in the same way within it), or install the full version of Vim: sudo dnf install vim-enhanced. On Ubuntu and Debian, vim is also available.

Methods

The most important feature of Vim is that it has several operating modes:

  • Normal mode: this is the default mode. Keys here are used to enter commands, not text.
  • Insert mode: press the i key to enter this mode, at which point you can start typing. -- INSERT -- (or -- BESZÚRÁS --) will appear at the bottom.
  • Command line mode: in normal mode, this is opened by pressing the : key at the bottom. Here you can enter the save and exit commands.

The Esc key always returns you to normal mode.

Edit, save, exit

  1. Open the file: sudo vim /etc/hosts (on AlmaLinux without vim-enhanced: sudo vi /etc/hosts)
  2. Use the arrow keys to navigate to the section you wish to edit.
  3. Press the i key and enter your text.
  4. Press the Esc key.
  5. Type:wq, then press Enter.
Command (in normal mode) Action
:w save
:q Exit (if there are no unsaved changes)
:wq or ZZ Save and exit
:q! Exit without saving, discarding changes
u undo
Ctrl+r again (withdrawal of a withdrawal)
dd Delete (cut) the current line
yy Copy the current line
p insert below the cursor
/szoveg, then Enter Search (next result: n)
gg / G Jump to the top / bottom of the file
:42 Jump to line 42
:set number Displaying serial numbers

Paste into Vim

If the text you have copied into Vim is indented in a staggered manner, enable paste mode before pasting: in normal mode, type :set paste, then i and paste. Afterwards, type :set nopaste.

Setting the default editor

The crontab -e command and several others open the editor specified in the EDITOR environment variable. If you always want to use nano, add the following to your ~/.bashrc file:

echo 'export EDITOR=nano' >> ~/.bashrc
source ~/.bashrc

On Ubuntu and Debian, you can also select the system-wide default using the command sudo update-alternatives --config editor; in the test, nano was set as the default on both (/etc/alternatives/editor -> /bin/nano).

After modification

Most services will only recognise the modified configuration after a restart or reload (see: Managing services with the systemctl command). Before doing so, if the service allows it, check that the settings are correct, for example:

sudo sshd -t        # SSH szerver
sudo nginx -t       # Nginx
sudo apachectl -t   # Apache
Back: Linux articles (62)