Chapter 5: Text, editors and the shell environment
Edit files with vim
Linux keeps its configuration in text files, so an administrator edits text all day. vim is the editor you can count on finding on every server, even a minimal one with no desktop. It feels strange for the first hour because keys mean different things in different modes; once that clicks, it is fast.
- Explain vim's modes and move between them.
- Open, edit, save and quit a file, and get out safely when something goes wrong.
- Move, delete, copy, paste, undo and search with the essential keys, and configure vim with ~/.vimrc.
vi and vim
vi is the classic Unix editor; vim ("vi improved") is the version RHEL ships, in two packages. vim-minimal provides vi, a small build with the core editing features, installed even on minimal servers. vim-enhanced provides the full vim, with syntax colouring, built-in help and the vimtutor tutorial. Your lab has both, so type vim; everything in this lesson also works in vi.
[student@servera ~]$ vim notes.txtIf the file exists, vim opens it; if not, vim creates it when you save. On a server where vim isn't available, vi always is. For quick edits, nano (which you used in chapter 4) is also installed in the lab, but it is often missing from minimal servers, so learn vim.
Modes
In most editors, every key types a character. In vim, what a key does depends on the mode. Select a mode:
vim's modes: Select a mode to see what keys do there and how to get in and out.
vim starts in normal mode, where keys are commands: x deletes a character, dd deletes a line, u undoes. To type text, press i to enter insert mode, type, and press Esc to return to normal mode. : starts a command line at the bottom of the screen for commands such as saving and quitting.
The minimum you need
With only these, you can edit any file:
| Keys (normal mode) | Does |
|---|---|
i | Start inserting before the cursor |
| Esc | Back to normal mode |
| arrow keys | Move |
x | Delete the character under the cursor |
u | Undo; Ctrl+R redoes |
:w | Write (save) |
:wq | Write and quit (:x and ZZ do the same) |
:q! | Quit, discarding every change since the last save |
:q alone refuses to quit if there are unsaved changes, which protects you. :q! is the escape hatch: when an edit goes wrong, quit without saving and start again.
Work faster
Once the basics feel natural, these save a lot of time:
| Keys | Does |
|---|---|
h j k l | Left, down, up, right without leaving the home row |
w / b | Next / previous word |
0 / $ | Start / end of the line |
gg / G / :42 | First line / last line / line 42 |
a / A | Insert after the cursor / at the end of the line |
o / O | Open a new line below / above, in insert mode |
dd / 5dd | Delete (cut) a line / five lines |
yy / p / P | Yank (copy) a line / put (paste) below / above |
/text, n, N | Search forward, next match, previous match |
:%s/old/new/g | Replace every old with new in the file |
v, V, Ctrl+V | Select characters / whole lines / a column block; then y to copy, d to cut |
Many commands take a count: 3x deletes three characters, 2yy copies two lines.
Configure vim
Settings for every user go in /etc/vimrc; your own go in ~/.vimrc, which overrides them. Chapter 1 created yours:
[student@workstation ~]$ cat ~/.vimrc
set number
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set hlsearchLine numbers, automatic indentation, tabs typed as four spaces, and search matches highlighted. You can also try a setting in a running vim by typing it after a colon, for example :set nonumber.
Learn by doing: vimtutor
vimtutor opens a copy of a tutorial in vim, which teaches vim by having you edit the tutorial itself. Lesson 1 takes about ten minutes and covers everything above; the next exercise uses it.
Check your understanding
- You opened a file in vim and typed some letters, but they did odd things instead of appearing. Why?
- You made a mess of a configuration file in vim and want to leave without saving anything. What do you type?
- In normal mode, what does dd do?
- Which keys copy the current line and paste it below?
- Where do your personal vim settings go?
- How do you replace every occurrence of 8080 with 8443 in the file?