Skip to content

Linux Foundations / Find your way around Linux ยท Step 48 of 63

Lesson: Text EditorsSwitch
โ† Linux Foundations

Shell and files

Text Editors

Meet the main terminal text editors and tell your system which one to use.

Much of Linux is configured with plain text files. Servers often have no graphical desktop, so you need an editor that runs inside the terminal. A word processor will not do: it stores formatting along with your words, while configuration files need plain text only.

The common choices

  • nano is small and shows its main shortcuts at the bottom of the screen. It is easy to start with.
  • Vim is an improved version of the old vi editor. It works with modes: the same key does different things depending on the mode you are in.
  • Emacs is a large, extensible editor. You type text directly and give commands with Control and Alt key combinations.

What is already installed

Some form of vi is present on most Linux systems, which is a good reason to learn its basics. POSIX, the Unix standard, describes vi. Do not assume that Emacs is there, and minimal images and containers may lack nano or even vi.

Check what you have:

bash
$ command -v vi vim nano emacs
/usr/bin/vi
/usr/bin/nano

Only the programs that exist are listed. Here vim and emacs are missing.

On Debian and Ubuntu, vi may be a cut-down build called vim.tiny. Install the full editors with your package manager:

bash
sudo apt install vim emacs-nox     # Debian, Ubuntu
sudo dnf install vim-enhanced emacs-nox   # Fedora and relatives

emacs-nox is the build without graphical support.

Choosing a default editor

Many programs open an editor for you, for example crontab -e. They commonly look at the VISUAL and EDITOR environment variables, usually preferring VISUAL.

bash
$ export VISUAL=vim
$ export EDITOR=vim
$ echo "$EDITOR"
vim

This lasts for the current shell session. To keep it, add the export lines to your shell startup file, such as ~/.bashrc.

Some programs have their own rules. Debian-based systems also offer sudo update-alternatives --config editor to pick the system-wide editor command.

Editing system files

For files owned by root, sudoedit /etc/hosts is safer than running a whole editor as root. It copies the file, lets you edit the copy as yourself, then writes it back. It chooses the editor from SUDO_EDITOR, VISUAL or EDITOR, within the limits of the system's sudo policy.

You do not have to pick a side between Vim and Emacs today. Learn enough of one to open, change, save and quit with confidence. Then try the other if you are curious.

Try it

  1. Run command -v vi vim nano emacs to see what your system has.
  2. Print your current setting with echo "$EDITOR". An empty line means it is not set.
  3. Set it with export EDITOR=nano and print it again.
  4. For a live Linux shell, open the files and links lab.

Check your understanding

Text Editors

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.