Working with TextLesson 4 of 4
Creating and editing files with vim
Create and change configuration files in vim, the editor that is always installed, with nano as a fallback.
vimvinanovimtutor:wq:set number
After this lesson you can
- Move, insert, change, delete, copy and paste in vim without leaving the keyboard home row.
- Search and replace, undo and redo, and save or quit with or without saving.
- Fix a mistake in a root-owned file safely with sudoedit or sudo vim.
The idea
vi (really vim) is installed on every Linux system you will meet, including a rescue shell, so you should be able to edit with it. It has modes. In normal mode keys are commands; in insert mode keys type text; in command-line mode (after :) you type commands such as save and quit. Esc always returns to normal mode.
| Key (normal mode) | Action |
|---|---|
i, a, o | Insert before the cursor, after it, on a new line below |
h j k l or arrows | Move left, down, up, right |
w, b, 0, $ | Next word, previous word, start, end of line |
gg, G, 12G | First line, last line, line 12 |
x, dd, dw | Delete a character, a line, a word |
yy, p, P | Copy a line, paste after, paste before |
u, Ctrl+R | Undo, redo |
/text, n, N | Search forward, next, previous |
:w, :q, :wq, :q! | Save, quit, save and quit, quit without saving |
:%s/old/new/g | Replace everywhere |
:set number | Show line numbers |
If you are lost, press Esc twice and type :q!. Run vimtutor once: it is a half-hour interactive lesson. On RHEL the vi command is a minimal vim; sudo dnf install vim-enhanced adds the full vim with colour and extra features.
Try it
vim ~/lab/notes.txt
Press i, type the text, press Esc, then type :wq and Enter. Back in the shell:
cat ~/lab/notes.txt
one
two
three
Editing a root-owned file
Do not run an editor as root unless you have to: a mistake there has no safety net. sudoedit copies the file, lets you edit the copy with your own editor and settings, then writes it back as root.
export EDITOR=vim
sudoedit /etc/hosts
getent hosts labserver
192.0.2.10 labserver
Going further
nano is also installed on RHEL: type, Ctrl+O then Enter to save, Ctrl+X to exit, and it prints the key list at the bottom. To make vim your default editor for sudoedit, crontab -e and git, put export EDITOR=vim in ~/.bashrc. :set paste before pasting avoids staircase indentation, and :w !sudo tee % saves a root-owned file you opened without sudo.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Create, save and quit
Open a new file
~/lab/notes.txtin vim, type three lines (one,two,three), save and quit. Confirm the content from the shell.Check your result
cat ~/lab/notes.txtprints the three lines.Hint
Press
ito insert, type,Escto leave insert mode, then:wqand Enter.Solution
vim ~/lab/notes.txt # i one<Enter>two<Enter>three Esc :wq cat ~/lab/notes.txt - Practice
Delete, copy and paste lines
Open
notes.txt. Delete line 2, undo it, copy line 1 and paste it after line 3 so the file readsone two three one. Save.Check your result
cat ~/lab/notes.txtprintsone,two,three,one.Hint
dddeletes a line,uundoes,yyyanks (copies) a line,ppastes below the cursor,ggandGjump to top and bottom.Solution
vim ~/lab/notes.txt # jdd u gg yy G p :wq - Practice
Replace across a file
Copy
/etc/hoststo~/lab/hosts.copy. In vim, replace everylocalhostwithLOCALin the copy, but first count how many lines contain it. Save.Check your result
grep -c LOCAL ~/lab/hosts.copyequals the earlier count of lines withlocalhost(grep -c localhost /etc/hosts).Hint
:%s/old/new/greplaces everywhere. Addcat the end to confirm each one.Solution
cp /etc/hosts ~/lab/hosts.copy grep -c localhost ~/lab/hosts.copy vim ~/lab/hosts.copy # :%s/localhost/LOCAL/g :wq grep -c LOCAL ~/lab/hosts.copy - Practice
Edit a system file safely
Add the line
192.0.2.10 labserverto/etc/hostsas a normal user with sudo, using an editor that works on a temporary copy. Confirm the name resolves.Check your result
getent hosts labserverprints192.0.2.10 labserver.Hint
sudoedit /etc/hosts(orsudo -e) edits a copy and writes it back as root. The editor comes from$EDITOR.Solution
export EDITOR=vim sudoedit /etc/hosts # G o 192.0.2.10 labserver Esc :wq getent hosts labserver - Challenge
Quit without saving, then recover
Open
notes.txt, make a mess (delete every line), quit without saving, and show that the file is unchanged. Then reopen it, changetwoto2using search and replace on that line only, and show the final file.Check your result
After the abandoned session
catstill shows the old text. After the second session only thetwoline changed.Hint
:q!quits without saving.:s/two/2/replaces on the current line only.Solution
vim ~/lab/notes.txt # ggdG :q! cat ~/lab/notes.txt vim ~/lab/notes.txt # /two Enter :s/two/2/ :wq cat ~/lab/notes.txt