Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Tekstbestanden maken en bewerken met vim en nano
Read a file fast
Use cat for short files, less for long ones, and head/tail for the start or the end.
Know the modes of vim
vim has modes. In normal mode keys are commands. In insert mode keys write text. In command mode (after :) you save and quit.
| Mode | How you get there | What keys do |
|---|---|---|
| Normal | Esc | Move, delete, copy, paste |
| Insert | i, a, o | Type text |
| Command | : | Save, quit, search and replace |
| Visual | v, V | Mark text, then copy or delete it |
Onthoud
Lost? Press Esc two times. Then you are in normal mode again.
Notitie
vimtutor is a 30-minute hands-on course inside the terminal. Do it once — it is the fastest way to learn vim.
Save and quit in vim
Every vim user must know these four commands.
| Command | What it does |
|---|---|
:w | Save |
:wq or :x or ZZ | Save and quit |
:q | Quit (only works when nothing changed) |
:q! | Quit and throw away the changes |
:w newname | Save as a new file |
Edit text in vim
Most editing happens in normal mode with short key commands. A number before a command repeats it (3dd = delete 3 lines).
| Keys | What they do |
|---|---|
i / a | Insert before / after the cursor |
o / O | New line below / above, then insert |
x | Delete one character |
dd / 3dd | Delete (cut) the line / 3 lines |
yy / 5yy | Copy the line / 5 lines |
p / P | Paste below / above |
u / Ctrl+R | Undo / redo |
gg / G / :25 | Go to the first line / last line / line 25 |
w / b | Next word / word back |
0 / $ | Start / end of the line |
cw | Change the word |
Search and replace in vim
Search with /, replace with :s. With % the replace works in the whole file.
| Command | What it does |
|---|---|
/PermitRoot | Search forward; n = next, N = back |
?error | Search backward |
:s/old/new/ | Replace the first match in this line |
:%s/old/new/g | Replace every match in the whole file |
:%s/old/new/gc | The same, but ask for each match |
:set number | Show line numbers (:set nonumber hides them) |
:set list | Show tabs and line ends |
Example: change the SSH port in a copy of the config file.
Inside vim: /#Port Enter, x to delete the #, cw on 22, type 2222, Esc, :wq.
Use nano for quick edits
nano is easier for beginners: you type at once, and the key commands are shown at the bottom (^ = Ctrl).
| Keys | What they do |
|---|---|
Ctrl+O, Enter | Save |
Ctrl+X | Quit (asks to save) |
Ctrl+W | Search |
Ctrl+\ | Search and replace |
Ctrl+K / Ctrl+U | Cut the line / paste it |
Alt+U | Undo |
Ctrl+_ | Go to a line number |
Op het werk
Set your favourite editor for tools like visudo, crontab -e and systemctl edit: put export EDITOR=nano (or vim) in ~/.bashrc.
Write a file from the shell
For short files you do not need an editor: use echo with > / >>, or a here document (<<EOF).
echo "server1" > hostlist.txt
echo "server2" >> hostlist.txt
cat > /etc/motd << 'EOF'
Authorized users only.
Problems? Call the IT desk: ext. 4040
EOF
tee /etc/sysctl.d/90-test.conf << 'EOF'
vm.swappiness = 10
EOF>makes the file new (old content is gone),>>adds to the end.- With
'EOF'in quotes the shell does not change$signs in the text. tee FILEwrites the input to the file and shows it — and withsudo teeit works wheresudo echo … >does not.
Let op
sudo echo "text" > /etc/file fails with "Permission denied": the > is done by your shell, not by sudo. Use sudo tee instead.