Skip to main content

Posts about VIM

Quick vi / vim tutorial

This site is written to promote using VIM cause it's free, it has a lot of plugins and it can even be python IDE ;) and vi is also on every unix like system so logging to your clean instalation server it's good to know how to edit conf files.

If you don't want to read more on this site just have a look at basic commands which will be enough to edit and save file:

Remember all this commands should be executed within normal mode to do so press ESC.

:q QUIT (being in normal mode to be sure you can always use ESC :q) most important command remember it to avaoid being character from famous "VIM quit joke" how to generate cat /dev/urandom:
:q! quit without saving
:wq - save and quit
yy - copy line
p - paste line
i - insert mode after that you're no longer in normal mode you can than start typig text
h j k l - left down up right or you can use arrows in vim
you can also find undo u nad redo ctrlr useful

I was inspired by Onjin when I saw him typing in vim I was impressed of how his thoughts became text and functions in VIM so fast without even using a mouse :).

It takes a while to switch from another editor and get its habits (it took one month for me and I tried 3 times) but after all you will be happy using lightweight VIM.

To use vim you need to know that there are five modes (normal, insert, visual, select, command-line, Ex-mode)

There are 4 main modes you'll be using probably:

  • Normal - usually VIm starts in this mode where you can navigate through whole file and modify text. To get to this mode press ESC.

  • Insert : this mode is like it says - for inserting new text you can go to this mode by pressing i or a (being in normal mode ESC)

  • Visual : you can select text in this mode by pressing v (being in normal mode ESC) and moving for selection

  • Command-line : you activate it by pressing : (being in normal mode ESC)

Tip: Capital letters like A need to followed by Shift

Editing (enter commands below being in normal mode):

i - enter insert mode and place cursor at current position to start typing

image0


I - enter insert mode and place cursor at start of the line to start typing

imageI


a - enter insert mode and place cursor after current position\ to start typing

image5


A - enter insert mode and place cursor at the end of line to start typing

image5A


ea - append at end of word

image6


r - replace a single character (does not use insert mode)

J - join line below to the current one

image7


cc - change (replace) an entire line

cw - change (replace) to the end of word

c$ - change (replace) to the end of line

s - delete character at cursor and subsitute text

S - delete line at cursor and substitute text (same as cc)

d$ orD - delete after cursorxp - transpose two letters (delete and paste, technically)

u - undo

Ctrl r - redo

. - repeat last command

ESC - exit insert mode

Indedation

Examples:

:s/\n/ /g - replace all spaces with enter in current line
:%s/\n/ /g -replace all spaces with enter in all lines
:%s/\n/ /cg - replace all spaces with enter in all lines but with confirmation


Indedation

v then select block you want indent and press number of indedations2> - execute indentation on the selected block

Marking text (visual mode)

v - mark lines with starting visual mode

d - (after starting selection with v ) cut selected text

y - (after starting selection with v ) copy (yank) selected text

p - (after cutting or copying selection) paste selected text after cursor

P - (after cutting or copying selection) paste selected text before cursor example of all 4 commands above:

image8


V - start Linewise visual mode

image9


o - move to other end of marked area

image10


Ctrlv - start visual block mode

O - move to Other corner of block

image11


aw - mark a word (being in visual mode)

image12


ab - a () block (with braces) (or vab from normal mode)

image13


aB - a {} block (with brackets)

image14


ib - inner () block

iB - inner {} block

> - shift right

< - shift left

y - yank (copy) marked text

d - delete marked text

~ - switch case

Layout/windows:

Ctrlww - swith between windows

Ctrl w - move cursor up a window example using ctr ww and ctr w arrows in example before switching windows ESC was pressed to enter normal mode:

image15


ctrl w + - increase size of current window

ctrlw- - decrease size of current window

image16


ctrlw\_ - maximize current window

ctrlw= - make all equal size

image17


10ctrlw+ - increase window size by 10 lines

:vsplit file - vertical split

:sview - same as split, but readonly

:hide - close current window

:only - keep only this window open

:ls - show current buffers

:b 2 - open buffer #2 in this window

Movement:

w - next word by punctation

image20


W - next word by spaces

image21


b - back word by punctation

B - back word by spaces

e - end word by punctation

E - end word by spaces

h - move left

j - move down

k - move up

l - move right

^ - first non-blank character of line

) - zero) start of line

$ - end of line

G - Go To command (prefix with number 4j moves down 4 lines.

Operations on file:

:e - open file in new buffer

:w - write file to disk - save

:w filename save as

ZZ - write file to disk and quit like :wq

:wq - save file and quit

:n - edit a new file

:n! - edit new file without saving current changes

:q! - quit without changes

:q - quit editing file

:e . - directory explorer

:split - filename - split window and load another file

:sview file - same as split, but readonly

Split window:

CtrlWs - horizontal splitting
CtrlWv - for vertical splitting
Ctrlwq - close window

Split window: repeat insert in blockview ctrlv - select block then I -text ESC

Macros - recording