Linux › Section B · Basic commands and the directory hierarchy › Topic B.7
Text editors and getting online help
Almost everything on a Linux system is configured through plain-text files, and almost every command documents itself. Learning one terminal editor and the built-in help system makes you independent: you can change the system and look up what you need without leaving the shell.
Why an editor matters
Linux keeps its settings in plain-text files, most of them under /etc, and administrators edit them constantly. A text editor saves exactly the characters you type, with no hidden formatting, which is what configuration files need. A word processor would add styling and could break the file. Servers reached over SSH often have no graphical desktop, so you need an editor that runs inside the terminal window rather than one that opens its own window.
The main choices
Three editors cover nearly every situation. vi is the classic Unix editor and is specified by the POSIX standard, so some version of it is present on almost every system; on Linux it is usually Vim (Vi IMproved) or a minimal build of it. Emacs is a large, extensible editor with thorough built-in help that runs both in a terminal and in its own window. nano is a small, menu-driven editor that lists its main keystrokes at the bottom of the screen, which makes it easy for beginners. Pick one and learn it well, but learn enough vi to survive, because it may be the only editor available on a rescue system.
Vim's modes
Vim surprises newcomers because keys do different things in different modes. It starts in normal mode, where keys are commands: x deletes a character, dd deletes a line, yy copies (yanks) a line, p pastes, u undoes and /word searches forwards, with n jumping to the next match. Press i to enter insert mode, where typing inserts text, and Esc to return to normal mode. Typing a colon in normal mode opens command-line mode at the bottom of the screen: :w saves, :q quits, :wq saves and quits, and :q! quits and throws away unsaved changes. Running vimtutor starts a 30-minute interactive lesson.
Worked example: a quick fix in Vim
You need to change the line colour=blue to colour=green in a file called settings.conf. 1. Run vim settings.conf; Vim opens in normal mode. 2. Type /colour and press Enter to jump to the line. 3. Press dd to delete the whole line, then O (capital) to open a new line above the cursor in insert mode. 4. Type colour=green. 5. Press Esc to leave insert mode. 6. Type :wq and press Enter to save and quit. 7. Check the result with cat settings.conf. If anything goes wrong along the way, press Esc and type :q! to leave without saving, then start again.
nano and Emacs in brief
In nano the bottom two lines show shortcuts, where ^ means Ctrl. Ctrl+O writes the file out (saves), Ctrl+X exits, Ctrl+W searches, Ctrl+K cuts the current line, Ctrl+U pastes it back and Ctrl+G shows help. Emacs uses chords of Ctrl and Meta (usually Alt): Ctrl+X then Ctrl+S saves, Ctrl+X then Ctrl+C quits, and Ctrl+H then T opens its tutorial. Many programs, including crontab and git, open an editor for you; they choose it from the VISUAL or EDITOR environment variable, so setting EDITOR=nano or EDITOR=vim in your startup file sets your default.
Manual pages
For any command, man followed by its name opens the manual page, for example man ls. Pages follow a fixed layout: NAME, SYNOPSIS (the command's syntax, with optional parts in square brackets), DESCRIPTION, the options, and often EXAMPLES and SEE ALSO. The page appears in the less pager: Space moves down a screen, b moves back, /pattern searches, n repeats the search and q quits. Manual pages are reference material, not tutorials: they list every option without saying which matter most, so skim the SYNOPSIS and search for the option you need.
Manual sections
The manual is divided into numbered sections. Section 1 holds user commands, 2 system calls, 3 library functions, 4 special files such as those in /dev, 5 file formats such as /etc/passwd, 6 games, 7 conventions and overviews, and 8 system administration commands. Writers add the section in brackets, as in ping(8) or passwd(5). When a name exists in several sections, man shows the first one it finds, so man passwd shows the passwd command from section 1. To read the description of the /etc/passwd file instead, put the section number first: man 5 passwd.
Searching when you don't know the name
man -k keyword, which is the same as the apropos command, searches the one-line descriptions of all manual pages and prints each match with its section. It is the tool to use when you know what you want to do but not which command does it. man -f name, the same as whatis, prints just the one-line description of a known page. Both search a prebuilt index; if apropos finds nothing on a new system, the index may not exist yet, and running sudo mandb builds it on systems that use the man-db package, such as Debian and Fedora.
Worked example: tracking down a file format
You want to know what the fields in /etc/group mean. 1. Search the descriptions: man -k group. The list is long, so narrow it with grep: man -k group | grep '(5)'. 2. Among the illustrative results you would expect a line such as group (5) - user group file. 3. The (5) tells you this is the file-format page, not a command. 4. Open it with man 5 group. 5. Inside less, type /password to jump to the field descriptions, press n for further matches and q when done. The same steps work for any configuration file whose name you know.
Other sources of help
Most commands print a short summary with --help, and some use -h; ls --help lists every option in one screen-sized burst. Shell builtins such as cd and type are part of Bash itself, so in Bash you get their help with help cd; most have no manual page of their own, and some distributions only point man cd at a general page about Bash builtins. GNU projects often publish fuller, hyperlinked documentation in info format, read with info followed by the name. Finally, many packages install README files, change logs and examples under /usr/share/doc, one directory per package; on Debian-family systems every package directory there holds at least a copyright file.
Common mistakes to avoid
First, panicking in Vim: if keys do strange things, press Esc, then type :q! to leave without saving. Second, typing text while still in normal mode, where every letter is a command. Third, running man passwd and wondering why it describes a command: the file format is in section 5. Fourth, editing configuration files in a word processor, which can add formatting or change line endings. Fifth, looking for a manual page for a builtin such as cd, when help cd is the right tool in Bash. Sixth, assuming an empty apropos result means no command exists, when the index may simply need building.
Key takeaways
Configuration lives in plain-text files, so learn one terminal editor well and enough vi to survive. Vim has normal, insert and command-line modes: i inserts, Esc returns, :wq saves and quits, :q! abandons changes. nano shows its shortcuts on screen; Ctrl+O saves and Ctrl+X exits. man shows manual pages in sections 1 to 8, such as 1 for commands, 5 for file formats and 8 for administration; man 5 passwd picks a section. man -k (apropos) searches descriptions and man -f (whatis) gives one line. --help, help for builtins, info and /usr/share/doc fill the gaps.
Flashcards
Flashcard
The classic Unix editor, specified by POSIX; Vim is Vi IMproved
Test your understanding
Question 1 of 5
You have edited a file in Vim, made a mess, and want to leave without saving anything. What do you type?
Question 2 of 5
Which command shows the description of the /etc/passwd file format rather than the passwd command?
Question 3 of 5
You want to find a command that compresses files but cannot remember its name. Which command searches the manual page descriptions for you?
Question 4 of 5
In Vim, you open a file and start typing a sentence, but letters seem to delete lines and jump the cursor. What is happening?
Question 5 of 5
How do you get help for the Bash builtin cd?
