Vim: Difference between revisions
m (→Text insertion: del/backspace) |
m (small addition) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 34: | Line 34: | ||
Another thing to remember: if you're in command mode and you start entering a number ''before'' a command, the command will get repeated that many times. The most used feature of this is deleting lines. Deleting the current line is done with the command ''dd''; deleting (for example) twelve lines can be accomplished using ''12dd''. Another example is the use of a number in front of G: command ''12G'' (use <shift>-g) will bring you to the beginning of the 12th line. A special number is zero; pressing 0 as the first character in a command means immediately going to the beginning of the line. | Another thing to remember: if you're in command mode and you start entering a number ''before'' a command, the command will get repeated that many times. The most used feature of this is deleting lines. Deleting the current line is done with the command ''dd''; deleting (for example) twelve lines can be accomplished using ''12dd''. Another example is the use of a number in front of G: command ''12G'' (use <shift>-g) will bring you to the beginning of the 12th line. A special number is zero; pressing 0 as the first character in a command means immediately going to the beginning of the line. | ||
=== | ===Opening and saving files=== | ||
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | {| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | ||
!style="background:#ffdead;"|Command | !style="background:#ffdead;"|Command | ||
!style="background:#ffdead;"|Output | !style="background:#ffdead;"|Output | ||
|- | |||
|:o ''filename'' | |||
|open file | |||
|- | |||
|:o ''directory'' | |||
|open file within ''directory'' interactively | |||
|- | |- | ||
|:w | |:w | ||
Line 45: | Line 50: | ||
|:w ''filename'' | |:w ''filename'' | ||
|Save with this filename | |Save with this filename | ||
|} | |||
===Exiting vim=== | |||
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | |||
!style="background:#ffdead;"|Command | |||
!style="background:#ffdead;"|Output | |||
|- | |- | ||
|:q | |:q | ||
Line 54: | Line 64: | ||
|:wq | |:wq | ||
|save and quit | |save and quit | ||
|- | |||
|ZZ | |||
|''also'' save and quit (no colon, just two caps) | |||
|} | |} | ||
===Cursor positioning=== | ===Cursor positioning=== | ||
Line 86: | Line 98: | ||
|beginning of next line | |beginning of next line | ||
|- | |- | ||
|0 (zero) | |^, 0 (zero) | ||
|beginning of line | |beginning of line | ||
|- | |- | ||
|$ | |$ | ||
|end of line | |end of line | ||
|- | |||
|G, 0G | |||
|to the beginning of the last line of the file | |||
|- | |- | ||
|''n''G | |''n''G | ||
|to beginning of line ''n'' | |to the beginning of line ''n'' | ||
|- | |- | ||
|:''n'' | |:''n'' | ||
|also to beginning of line ''n'' (but this is visible as command) | |also to beginning of line ''n'' (but this is visible as command) | ||
|- | |||
|gg, 0G | |||
|to the first line | |||
|- | |- | ||
|} | |} | ||
Line 116: | Line 134: | ||
|- | |- | ||
|} | |} | ||
<br> | |||
===Search and replace=== | |||
If you think searching is fun, then search-and-replace is even more fun! Check out the "substitute" command! | |||
:[range]s[ubstitute]/{pattern}/{string}/[&][c][e][g][p][r][i][I] [count] | |||
For each line in [range] replace a match of {pattern} with {string}. For the {pattern} see |pattern|. {string} can be a literal string, or something special; see |sub-replace-special|. When [range] and [count] are omitted, replace in the current line only. When [count] is given, replace in [count] lines, starting with the last line in [range]. When [range] is omitted start in the current line. [range] can be something like<br> | |||
% = whole file<br> | |||
1,$ = also whole file<br> | |||
3,7 = lines 3 through 7<br> | |||
.,+5 = current line (denoted with the period) through the next five lines<br> | |||
'a,'b = between marks ''a'' and ''b''.<br> | |||
=== | Well that sounds wonderful, but how do you use this in practice? Usually something like this: | ||
:%s/{pattern}/{string}/gic | |||
This means: search the whole file for case-insensitive occurrances of {pattern}, and replace each occurrance (even multiple on a single line) with {string} ''after'' confirmation of each replacement. | |||
===Switching to and from insert mode=== | |||
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | {| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | ||
!style="background:#ffdead;"|Key(s) | !style="background:#ffdead;"|Key(s) | ||
Line 151: | Line 183: | ||
|} | |} | ||
===Text deletion=== | ===Text cutting (deletion)=== | ||
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | {| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | ||
!style="background:#ffdead;"|Key(s) | !style="background:#ffdead;"|Key(s) | ||
Line 157: | Line 189: | ||
|- | |- | ||
|x | |x | ||
| | |cut character under the cursor | ||
|- | |||
|dw | |||
|cut word under the cursor | |||
|- | |- | ||
|dd | |dd | ||
| | |cut current line | ||
|- | |- | ||
| | |''n''dd | ||
| | |cut ''n'' lines starting with the current one | ||
|- | |- | ||
|d) | |d), d$ | ||
| | |cut to the end of the current sentence (up to and including the dot) | ||
|- | |- | ||
|D | |D | ||
Line 173: | Line 208: | ||
|d$ | |d$ | ||
|same (deletes to the end of the current line) | |same (deletes to the end of the current line) | ||
|- | |||
|} | |||
===Text pasting (insertion)=== | |||
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5" | |||
!style="background:#ffdead;"|Key(s) | |||
!style="background:#ffdead;"|Effect | |||
|- | |- | ||
|p | |p |
Latest revision as of 19:41, 26 May 2009
Introduction
The screen-oriented text editor vim stems from vi, written in '76 for an early BSD Unix release by Bill Joy. vi is old, nonintuitive, and complex. However, it was the successor of the line editor ex; the very name vi stands for "visual", and in the seventies, going from ex to vi was a big improvement indeed.
vim stems from 1991, and stands for Vi IMproved, but its improvements do not lessen it's nonintuitivity or complexity.
So why do we feel that vim is an essential system tool? Well, it's because
- vi can be found on just about any Linux and Unix system
- vi is very powerful
- with some practice, it's even quite usable
but most importantly
- if something goes horribly wrong, vi might just be the only means left to change your configuration files
About vi as a modal editor
vi is a modal editor; this type of editors will operate differently depending on which mode is selected. A vi session knows two modes:
- insert mode, where typed text is added into the document at the place of the prompt, and
- normal mode or command mode, where keystrokes are interpreted as commands that control the session, or act on the document (depending on the command).
vi begins in normal mode, but several commands (notably i and a) will switch it to insert mode. Once in insert mode, most anything you type becomes part of your document. Only when you press <esc> does your vi return to normal mode.
Installation and configuration
To get vim, simply use sudo apt-get install vim or sudo aptitude, and find vim in the available packages. Nice to know: when you install vim, you also get xxd, a tool to make a hexdump, or convert a hex dump back to it's original binary form.
In Debian Etch, the default editor is nano instead of vim. This can be changed in the following way:
which vim.basic sudo update-alternatives --set editor /usr/bin/vim.basic
(The first line is to check that you really have vim.basic in place) Now all commands that invoke an editor will use vim instead of nano. Is that a good thing? We're not sure, but we do want to keep our vi skills up to par, so we do this.
Using vim
Vim operates in two modes: a command mode and an insert mode. You start in command mode, so almost every key is a command. Several commands you can issue will bring you into --INSERT-- moode, e.g. i or a; to return in command mode press <esc> or ( <ctrl> [ ).
It can be confusing to remember if you're in command mode or insert mode (even though there's a bright marker in the left bottom of the vim screen :-). So remember: when you're in insert mode, <esc> will bring you to command mode; when you're already in command mode, <esc> does nothing (no harm done). So press <esc> two or three times, and you're sure to be in command mode.
Another thing to remember: if you're in command mode and you start entering a number before a command, the command will get repeated that many times. The most used feature of this is deleting lines. Deleting the current line is done with the command dd; deleting (for example) twelve lines can be accomplished using 12dd. Another example is the use of a number in front of G: command 12G (use <shift>-g) will bring you to the beginning of the 12th line. A special number is zero; pressing 0 as the first character in a command means immediately going to the beginning of the line.
Opening and saving files
Command | Output |
---|---|
:o filename | open file |
:o directory | open file within directory interactively |
:w | Save |
:w filename | Save with this filename |
Exiting vim
Command | Output |
---|---|
:q | quit vim |
:q! | quit without save |
:wq | save and quit |
ZZ | also save and quit (no colon, just two caps) |
Cursor positioning
Key(s) | Movement |
---|---|
h | one character back |
j | down one line, same column |
k | up one line, same column |
l,<space> | one character forward |
b | one word back (to beginning of previous word) |
e | one word back (to end of previous word) |
w | one word forward |
<enter> | beginning of next line |
^, 0 (zero) | beginning of line |
$ | end of line |
G, 0G | to the beginning of the last line of the file |
nG | to the beginning of line n |
:n | also to beginning of line n (but this is visible as command) |
gg, 0G | to the first line |
Note that h,j,k,l is one block on your (US) keyboard
Searching
Key(s) | Search |
---|---|
/pattern | Moves the cursor to the next occurrence of pattern; will wrap around to the beginning of the file from the last occurence |
?pattern | Moves the cursor backward to the previous occurrence of pattern; will wrap around to the end of the file from the first occurence |
n | repeats last pattern search (either forward or backward) |
Search and replace
If you think searching is fun, then search-and-replace is even more fun! Check out the "substitute" command!
:[range]s[ubstitute]/{pattern}/{string}/[&][c][e][g][p][r][i][I] [count]
For each line in [range] replace a match of {pattern} with {string}. For the {pattern} see |pattern|. {string} can be a literal string, or something special; see |sub-replace-special|. When [range] and [count] are omitted, replace in the current line only. When [count] is given, replace in [count] lines, starting with the last line in [range]. When [range] is omitted start in the current line. [range] can be something like
% = whole file
1,$ = also whole file
3,7 = lines 3 through 7
.,+5 = current line (denoted with the period) through the next five lines
'a,'b = between marks a and b.
Well that sounds wonderful, but how do you use this in practice? Usually something like this:
:%s/{pattern}/{string}/gic
This means: search the whole file for case-insensitive occurrances of {pattern}, and replace each occurrance (even multiple on a single line) with {string} after confirmation of each replacement.
Switching to and from insert mode
Key(s) | Effect |
---|---|
a | appends text after cursor & switches to Insert mode |
A | appends text at the end of the line & switches to Insert mode |
i | inserts text before cursor & switches to Insert mode |
I | inserts text at the beginning of the line & switches to Insert mode |
o | opens new line below the current line for text insertion & switches to Insert mode |
O | opens new line above the current line for text insertion & switches to Insert mode |
<backspace> | removes last character during text insertion |
<del> | removes next character during text insertion |
<esc> | stops text insertion. The escape key on the DECstations is the <F11> key |
Text cutting (deletion)
Key(s) | Effect |
---|---|
x | cut character under the cursor |
dw | cut word under the cursor |
dd | cut current line |
ndd | cut n lines starting with the current one |
d), d$ | cut to the end of the current sentence (up to and including the dot) |
D | deletes to the end of the current line |
d$ | same (deletes to the end of the current line) |
Text pasting (insertion)
Key(s) | Effect |
---|---|
p | "paste", puts the buffer (e.g. text from the previous deletion) after the position of the cursor |
P | "Paste", puts the buffer (e.g. text from the previous deletion) before the position of the cursor |