Vim: Difference between revisions

From SaruWiki
Jump to navigation Jump to search
(add tabel)
(added deletion table + updated insertion table)
Line 8: Line 8:


==Installation and configuration==
==Installation and configuration==
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.
 
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:
sudo update-alternatives --set editor /usr/bin/vim.tiny
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==
==Using VIM==


Vim operates in 2 modus. A command mode and a insert mode. You start in command mode so almost every key is a command. You can switch to insert mode with ''i'' to return in command mode press ''esc'' or ( ''ctrl ['' )
Vim operates in two modes: a command mode and an insert mode. You start in command mode, so almost every key is a command. You can switch to insert mode with ''i'' or ''a''; to return in command mode press ''<esc>'' or ( ''<ctrl> ['' )
Start in insert mode and typ some text. To save your progress return in command mode with ''esc'' Now we can use the ''ex-commands'' typ '':w'' to save the document. or use these commands.
Start in insert mode and typ some text. To save your progress return in command mode with ''<esc>'' Now we can use the ''exit-commands'' typ '':w'' to save the document. or use these commands.


{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5"
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5"
Line 22: Line 27:
|Save
|Save
|-
|-
|:w
|:w ''filename''
|'filename' Save with this filename
|Save with this filename
|-
|-
|:q
|:q
Line 82: Line 87:
{| 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)
!style="background:#ffdead;"|Movement
!style="background:#ffdead;"|Search
|-
|-
|/''pattern''
|/''pattern''
Line 91: Line 96:
|-
|-
|n
|n
|repeats last pattern search (either / or ?)
|repeats last pattern search (either forward or backward)
|-
|}
|}


==Text insertion==
==Text insertion==
a   Appends text after cursor. Terminated by escape key.
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5"
A   Appends text at the end of the line. Terminated the escape key.
!style="background:#ffdead;"|Key(s)
i   Inserts text before cursor. Terminated by the escape key.
!style="background:#ffdead;"|Effect
I   Inserts text at the beginning of the line. Terminated by the escape key.
|-
o   Opens new line below the current line for text insertion. Terminated by the escape key.
|a
O   Opens new line above the current line for text insertion. Terminated by the escape key.
|appends text after cursor & switches to Insert mode
DEL Overwrites last character during text insertion.
|-
ESC Stops text insertion. The escape key on the DECstations is the F11 key
|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
|-
|<del>
|overwrites last character during text insertion
|-
|<esc>
|stops text insertion. The escape key on the DECstations is the <F11> key
|-
|}


==Notes==
==Text deletion==
In Debian Etch, the default editor is nano instead of vim. This can be changed in the following way:
{| class="wikitable" style="text-align:center" border="1" cellspacing="0" cellpadding="5"
sudo update-alternatives --set editor /usr/bin/vim.tiny
!style="background:#ffdead;"|Key(s)
Now '''all''' commands that invoke an editor will use ''vi'' instead of ''vim''. 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.
!style="background:#ffdead;"|Effect
|-
|x
|deletes character under the cursor
|-
|dd
|deletes current line
|-
|dw
|deletes word under the cursor
|-
|d)
|deletes 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)
|-
|P
|"paste", puts back text from the previous deletion at the position of the cursor
|-
|}

Revision as of 19:30, 25 May 2008

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. 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 usable.

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:

sudo update-alternatives --set editor /usr/bin/vim.tiny

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. You can switch to insert mode with i or a; to return in command mode press <esc> or ( <ctrl> [ ) Start in insert mode and typ some text. To save your progress return in command mode with <esc> Now we can use the exit-commands typ :w to save the document. or use these commands.

Command Output
:w Save
:w filename Save with this filename
:q quit vim
:q! quit without save
:wq save and quit

About modal editors

Cursor positioning

Key(s) Movement
j down one line, same column
k up one line, same column
h one character back
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
nG to beginning of line n; if no number given, the last line of the file
:n also to beginning of line n (but this is visible as command)

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)

Text insertion

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
<del> overwrites last character during text insertion
<esc> stops text insertion. The escape key on the DECstations is the <F11> key

Text deletion

Key(s) Effect
x deletes character under the cursor
dd deletes current line
dw deletes word under the cursor
d) deletes 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)
P "paste", puts back text from the previous deletion at the position of the cursor