the world of vi (vim) is different from other editors.. this makes it special, some people don't recommend it for novice coders. thats because you need to know its commands, this post should give you ONLY a start point for using vim :)
down to business, well you may know or not yet that vim have different modes probably as follows:
- Insert mode: activated by pressing i character, disable by Esc
- Replace mode : activated by pressing r or R character, disables by Esc
- Command mode: this is the default where you are passing commands to vim. commonly started by colon followed by command like :r filenme
- Visual mode: only in vim not vi, invoked by v
Invoking vim
Command | Description |
vi file1 file2 .. | opening file(s) to edit |
vi -r file | recovering previously not saved file(crashed) from swp file |
vi -n file | start without writing swp file , if your editing session crashed or your computer ran out of power then you'll loose your work.. solution save the file as soon as you change it |
files handling
Command | Description |
:r file | read file and isert its contents inside the current document at the cursor postion |
:n | move to next file to edit, this is mainly when you invoke vim by multiple files to edit, like vim file1 file2 file3. if you were editing file1 this command will open file2 and close file1 |
:N | move to previous opened file, opposite of the previous command |
:w | save changes to file but dont exit |
:x | save and exit |
:q | quit of saved file(s) |
:q! | quit anyway |
:n file4 | edit new file,and closing files list if already saved |
:n! file4 | start editing new file anyway |
:N! | move to previous file, ignoring change to current file |
searching
Command | Description |
/keyword | search for keyword forward |
?keyword | search for keyword backward |
n | move to next found match |
N | move to previous found match |
replacing
:s/oldtext/newtext | searching for oldtext and replace it with newtext, only once in the line |
:%s/oldtext/newtext/ | searching for oldtext and replace it with newtext, all occuarance in the line |
:%s/oldtext/newtext/g | searching for oldtext and replace it with newtext, all occuarance in whole document |
Inserting
i | start insert text before cursor. pres Esc to stop |
I | (this is upper case i not l) start insert text at the beginning of the line. pres Esc to stop |
a | start insert text after cursor. pres Esc to stop |
A | start insert text at the end of the line. pres Esc to stop |
p | put text already stored in buffer, after the current line |
P | put text already stored in buffer, before the current line |
yanking, selecting
y | yank(copy) the current line |
v | start selecting text, move up down right left to control the selected text |
deleting, restoring
x | delete the character under the cursor |
X | delete the character before the cursor |
dd | delete current line |
D | delete the rest of the line |
dw | delete current word |
. | (dot) redo the previous action |
U | restore current line |
u | undo the last change, you can prepend it with anumber for example 7u undo previous 7 changes |
moving the cursor
0 | move to the start of the line |
$ | move to the end of the line |
G | move to the end of the document |
w | move one word forward |
replacing
r | replace character below the cursor |
R | start replacing mode, ESC to stop |
Examples:
replacing all new lines with ; and new line:
:%s/\n/;\r/g
displaying line numbers with set command:
:set number or
:set nu
searching help for something for example delete
:help delete
Note: Debian doesn't install vim by default but
vim_tiny (minmal installation of vim) to install full vim with all cool stuff and help files and docs run the following :
sudo apt-get install vim
when you get used to vim.. you'll learn much more about this decent editor
-------------------
cheers
No comments:
Post a Comment