Friday, June 6, 2008

Basic VIM

So let's start our first post with a VIM tutorial. VIM is a command-line text/code editor. It has syntax highlight and auto indentation amongst other useful features. I'll run through basic procedures.

To create a new file just start a terminal emulator and type
$ vim

It will open a "window" inside the terminal window. Notice you can't write right away. VIM uses specific commands to start writing in different ways. To simply start writing press "i" (please that notice it's case sensitive). A label "INSERT" will appear in the bottom and everything you write will appear on the screen. To stop the "INSERT" mode, simply press esc. Here's a table of commands you can try to write differently:







aText after cursor
AText at end of line
iText before cursor
IText at beginning of line
aNew line + Insert
aNew line(above) + Insert


or to delete input text:




xErases character
dndDelete n lines (dd to erase 1 line)
DErase the rest of the linte


and some other goodies:




ynyCopies n lines (yy for 1 line)
pPastes lines (copied with yny or cut with dnd)
uUndo last change


There are some useful navigation commands you can try as well. They are very useful for browsing through extensive code. The "/" [slash] character starts a search. If you type
/hello

the page will scroll until the next occurrence of "hello". If you want to go to the next after that, type
//

and so forward. Here's a list of some other useful navigation commands:








HTop of screen
MMiddle of screen
LBottom of screen
ggFirst line
GLast line
nGLine n
%Finds the matching (,[,{


Now to save your file you must press ":" to open an inside command-line. There are a series of commands to be put here. I'll go through the basic:
:w

will write the modifications to the file
:wq

will write the modifications to the file and then exit
:q!

will exit without saving the file.

Besides that, I use three other commands that are very useful:
:set [no]nu

Turns the number of lines on and off
:set [no]ai

turns the indentation on and off
:set syntax=value

sets the syntax to value or if you want to turn it off, set it to value=off.



And that was it. Enjoy.

No comments: