Home | EN | ID | AR | About | All Posts

vi(m)

In this page, I will post about vi and vim.

I hope that this note will help me later, when I need it.

Note: this is my old story of using vi, since I was in the collage. Please excuse my pun :D.

vi, is it scary?

hoho vi… the better version is vim (VIMprove) I think it’s an awesome editor, cool, powerfull, durable, efficent, etc, etc. But if compared to notepad++, vi might not stand a change. :P (edit, I’m still considering vim is better in its own league)

But in my heart, still vim :P. Yes, it’s in difference field with notepad++, both have their own advantages.

The other day, some friend said when we were working on assignment,

"using vi? isn't there anything else, which easier?"

I used to say that, but I think vim is quite easy, especially for editing the configuration in *nix. on Windows, there’s also vim for windows (gvim).

If you are in terminal, the best editor in my opinion is, of course vim. until now, vim is still developed and released, the latest version is 7.4.

alright, not to be so full of myself, I only want to poison you. here are some tips that I usually use.

First step. Normally to start vim, type

$ vim

di terminal tar bakal keluar editor yg blom ada isinya, ato klo dah tau file apa yg mo diedit, tinggal

$ vim fileku.txt

go to specific line in fileku.txt :

$ vim +60 fileku.txt 

ini bakal ke baris 60 di fileku (sangat gampang bagi programer, melihat bug dsb)

okeh, dah masuk vim :D mo pindah-pindah, gampang banget…

   j
 h   l
   k

pake arah yg ada dikeyboard jg bs ingat hjlk bs digunakan di mode command, by pressing esc.

untuk menulis, ketik dulu insert mode bisa pake i, I, a, A, o, O. untuk sekarang i aja dulu :P

sila ketik. selesai ketik, tekan esc, :, w, dan q.

i-> insert at the left of current cursor
I-> insert at the begining of current line
a-> insert at the right  of current cursor
A-> insert at the end of current line
o-> insert one line below current line
O-> insert one line above current line

search :

Untuk melakukan pencarian, kita harus berada di mode command. untuk memastikan bahwa kita berada di mode command, silahkan ketik esc terlebih dahulu.

Kemudian untuk mencari suatu kata, sila ketik /, diikuti dengan kata yang ingin dicari:

/kata_yang_dicari

Agar pencarian tidak case sensitive, sila ketik:

:set ic 

to ignore case sensitive.

If you want to search for bandung or jakarta:

/bandung\|jakarta

untuk find and replace, ketik :

:%s/hari/hary/gc

perintah ini akan mencari kata hari untuk digantikan dengan kata hary. flag g untuk semua, flag c untuk bertanya, apakah mau diganti(untuk konfirmasi).

When starting vi, to go to first occurent of string, we can use:

$ vi +/string file

eg:

$ vi +/JKT_IVRTSM1b_SERVICE /etc/hosts

akan menuju langsung ke JKT_IVRTSM1b_SERVICE.

or we want to go to specific line

$ vi +50 fileku

this will bring us to the 50th line of fileku.

splitting

in vi, type :sp for spliting the windows (mendatar), dan pindah dengan ctrl-w lalu arah, j(bawah) dan k(atas), h ke kiri dan l ke kanan.

:sp filename (horizontal)
:vsp filename (vertical)

atau dapat menggunakan control:

Ctrl-W s for horizontal splitting
Ctrl-W v for vertical splitting
Ctrl-w q to close one

in terminal: Normally if we want to open N windows (default: one for each file), we use:

# vi -o[N]                

if we want to display it all, use

# vi -O[N]                

it’s like -o but split vertically

restore edit

in vi, ketik :e! utk kembali ke awal, original file.

inser command to current edit file:

to insert from a command, in vi use:

:r !date

this will print output of date to the content of vi

:r !whoami
:r!pwd

vim in windows

edit vimrc di windows, open gvim, then edit file _vimrc:

:e _vimrc

then append :

set guifont=Consolas:h10:cANSI
colors slate

Aditional command:

7j -> move 7 baris ke bawah
dw -> delete a word
* -> cari kata yang sedang ditunjuk kursor (cool)
:new ~/Desktop/coba.txt -> open file coba.txt in new window in vim (cool)
:tabe -> go to tab edit
:tabn -> go to next tab
:tabp -> go previous tab

v -> to start visual, and you can select text, eg to copy using arrow comand
or any text movement to move around. (DAMN...THIS IS PRETTY COLL, I didn't know about this...Agghh..)
V -> visual yang bekerja pada baris

to input from standard output just use vim -R - (there’s dash at the end) eg:

# chkconfig --list | vim -R - 

will invoke vim which contain output of chkconfig

to browse file system and press to open the file

 :Vex 

or

 :Sex 

Vex=Vertical split explor Sex=Split explor

completation

command line completetion in vim. for example you want to know command start with a, type

:a  

then without space, followed by <ctrl-D> this will give you autocompletion a, and see possible completions:

abbreviate  amenu       argdelete   arglocal    augroup
abclear     anoremenu   argdo       args        aunmenu
aboveleft   append      argedit     argument    autocmd
all         argadd      argglobal   ascii

you can also use <tab> hehe

oh, gw baru tau.. ilmu baru.. you can select (and it will automatically store in cliboard, in other name, it coppied) then you can paste it by middle mouse button, or two tap simustaniously. yep select a text, then go to any other you wnat to paste, then you middle click there / doubletap at once there, the text will be pasted. COOLLLLLL

regular expression in vi

[...] -> match any character from the set
[^...] -> match any character outside the set
[...-...] -> any character from range
[^...-...] -> any character outside range
\< -> begining of word, eg /\<London matchs every line with begin with London
\> -> end of word

to copy all line type

:%y

to copy from current line to the end,press y followed by G

to join line type J, if you want to join 5 lines at one time, type 5 then J.

to repeat last action, type . . if you want to execute it 5 times, type 5 followed by dot

to redo or undo:

u, U, R

to execute command while in vi, execute :!your-command. eg:

:!pwd

while in vi, if you want to go to shell, type

:sh

and when you exit shell, you will be back to vi.

open file with single press: to open file with filename is under current cursor, press gf, then it will automatically open this file

in vim, to enter commandline history, type q then :

to disable .viminfo, add to .vimrc

set viminfo="NONE" 

other GREAT vimtips:

make lowercase:

in vim, to make all lines to lower case with guG

folding in vim:

to open folding : click left(h) or right(l) or zo to close folding : zc

autocompletion in vim: ctrl-N . THIS IS AWESOME…..

jump to tags: use ctrl-] to jump to tags and to go back use ctrl-T

keyboard mapping in vim this could be really useful:

:nmap - Display normal mode maps
:imap - Display insert mode maps
:vmap - Display visual and select mode maps
:smap - Display select mode maps
:xmap - Display visual mode maps
:cmap - Display command-line mode maps
:omap - Display operator pending mode maps

I just knew… that ESC can be replace by ctrl-[.

Tips and Tricks

Disabling auto indent temporarily to paste

Sometimes you only need to paste some snippet of code, that already has indentation, so you can disable it only during the paste operation with command:

:set paste

then,

:set nopaste

Need to add more and edit more and translate more.