How to Vim - for beginners

40
How to vim author: Marcin Rogacki <[email protected] >, <[email protected]> experience in vim: 6 months

Transcript of How to Vim - for beginners

How to vim

author: Marcin Rogacki <[email protected]>, <[email protected]>experience in vim: 6 months

Why try?1. Successfully used as IDE by others.

2. Don’t need X enviroment (Production quick fix? No problem!)

3. Lightweight & fast.

4. Able to edit large files 50MB, 100MB, 1GB (Ok... search the 1GB file is

an horror but still is openable).

5. Code is written faster (with enough vim experience).

6. Your keyboard skills will be improved.

place of new recruits is to the proving ground

run shell command: vimtutor

training

Now time for ‘Missions’.They show practical tricks (at least I have hope that they are practical) or just examples how to

fight with vim.

Basics

● move: hjklweb$0gGf● edit: aiorxdvc● search: /nf● undo and redo: u, ctrl+r● copy & paste: ypdx

combine all of above into “advanced basics”don’t use arrows or mouse!

Armory

outp

ut

inpu

t

Mission: delete from here to end

Accomplished: d,shift + g

line no. 1line no. 2line no. 3line no. 4line no. 5

line no. 1line no. 2

outp

ut

inpu

t

Mission: delete from here to line 4

Accomplished: d,4,shift+g

line no. 1line no. 2line no. 3line no. 4line no. 5

line no. 1line no. 5

outp

ut

inpu

t

Mission: copy & paste - line

Accomplished: yy,j,p

line no. 1line no. 2line no. 3line no. 4line no. 5

line no. 1line no. 2line no. 3line no. 4line no. 3line no. 5

outp

utin

put

Mission: cut & paste - text refactoring

Accomplished: v,w,d,p

$product->setName(‘Not so very long name’);

$name = ‘Not so very long name’;$product->setName($name);

outp

utin

put

Mission: cut & paste - text refactoring v2

Accomplished: v,f,’,d,p

$product->setName(‘Not so very long name’);

$name = ‘Not so very long name’;$product->setName($name);

outp

utin

put

Mission: cut & paste - text refactoring v3

Accomplished: d,f,’,p

$product->setName(‘Not so very long name’);

$name = ‘Not so very long name’;$product->setName($name);

outp

utin

put

Mission: cut & paste - text refactoring v4

Accomplished: c,f,’,p

$product->setName(‘Not so very long name’);

$name = ‘Not so very long name’;$product->setName($name);

outp

ut

inpu

t

Mission: insert column beetwen

Accomplished: ctrl + v, A, ESC, j

$productObj->doSmth();$productObj->doSmthElse();$productObj->doAnother();$productObj->doNothing();

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doNothing();

outp

ut

inpu

t

Mission: replace column

Accomplished: ctrl + v, c, ESC, j

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doNothing();

$item->doSmth();$item->doSmthElse();$item->doAnother();$item->doNothing();

outp

ut

inpu

t

Mission: add at very last

Accomplished: $, ctrl + v, shift + a, ESC, j

$product->doSmth()$product->doSmthElse()$product->doAnother()$product->doNothing()

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doNothing();

outp

ut

inpu

t

Mission: comment out

Accomplished: 0, ctrl + v, I, ESC, j

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doNothing();

//$product->doSmth();//$product->doSmthElse();//$product->doAnother();//$product->doNothing();

The menu bar & tabs

Armory:edit <file> - open in current window:newtab <file> - open new tab:vsplit <file> - vertical split and open:split <file> - horizontal split and:r <file> - read content into current file:! - execute shell command

:<text> - is a vim command

ctrl + ww - switch tabgt - next window

outp

ut

inpu

t

Mission: copy & paste from other file

Accomplished: :r docs/beer-license.txt

License LicenseTHE BEER-WARE LICENSE:Can do whatever you want.You can buy me a beer in return.

outp

ut

inpu

t

Mission: copy & paste shell console

Files list

Accomplished: :r !ls -l

Files listcode/file.phplib/file.phppublic/file.js

outp

ut

inpu

tMission: copy&paste between tabs/windows

Accomplished: g,t,y,y,g,t,p

Some text window 1

Some text window 2

Some text window 1Some text window 2

Some text window 2

Text manipulations

Armory/ - search:s/<search>/<replace>/g - search & replacectrl + p - text autocomplete backwardctrl + n - text autocomplete forward

:<text> - is a vim command

outp

ut

inpu

t

Mission: Search & replace

Accomplished: :s/product/item/gc

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doNothing();

$item->doSmth();$item->doSmthElse();$item->doAnother();$item->doNothing();

outp

ut

inpu

t

Accomplished: ctrl + p or ctrl + n (input mode!)

Mission: autocoplete

$product->doSmth();$product->doSmthElse();$product->doAnother();$product->doS;

$item->doSmth();$item->doSmthElse();$item->doAnother();$item->doSmthElse();

Macros

Armory

q - record macro (in fly!)

small but powerful

how to use macros1. find some ‘regularities’

2. record macro by q and pick an <register> (e.g. ‘a’ key)

3. work as usual with vim

4. stop recording by q

5. set cursor at next ‘regularity’ <number of repeats>@<register>

outp

ut

inpu

t

Mission: add ‘irregular’ text

Accomplished: qakf(wyw0jf(pa,ESCF(wctrl+a0jq10@aBefore start ensure that cursor is set at 2-nd line, keys: ggj. Then just invoke this sequence.

INSERT INTO category VALUES(1,'Basics'); INSERT INTO category VALUES('Request'); INSERT INTO category VALUES('Render',); INSERT INTO category VALUES('EAV'); INSERT INTO category VALUES(‘Admin'); INSERT INTO category VALUES('Catalog');INSERT INTO category VALUES('Model');INSERT INTO category VALUES('Helper');INSERT INTO category VALUES('Config');INSERT INTO category VALUES('System');INSERT INTO category VALUES('Shell');INSERT INTO category VALUES('Db');

INSERT INTO category VALUES(1,'Basics'); INSERT INTO category VALUES(2,'Request'); INSERT INTO category VALUES(3,'Render',); INSERT INTO category VALUES(4,'EAV'); INSERT INTO category VALUES(5,‘Admin'); INSERT INTO category VALUES(6,'Catalog');INSERT INTO category VALUES(7,'Model');INSERT INTO category VALUES(7,'Helper');INSERT INTO category VALUES(8,'Config');INSERT INTO category VALUES(9,'System');INSERT INTO category VALUES(10,'Shell');INSERT INTO category VALUES(11, 'Db');

Plugins

Armoryvim-pathogen - simplifies installing the pluginsnerdtree - project treevim-snipmate - text templatesphpcomplete.vim - code autocomplete support (php)fuzzyfinder - file searcher (by pattern)DBGPavim - php debuggerand many more… (google.pl)

Mission: project tree

Accomplished: nerdtree

Mission: text templates

http://www.youtube.com/watch?v=xV2IsE5OHd4

Accomplished: vim-snipmate

Mission: code autocomplete

Accomplished: phpcomplete.vim

Mission: file search

Accomplished: fuzzyfinder

Mission: debug

Accomplished: DBGPavim (not tested)

Last words

1. Vim is not better than other IDE's. It is different. There is advantages

and disadvantages e.g. I think native IDE will alwasy have better code

autocomplete.

2. Before grabbing a mouse, early try do it with keyboard. Power of vim

is: to be always ready for text typing.

3. Beginnings are difficult, but after time will appear the conditioned

response, in the other words copy&paste/movement/refactor will take

milliseconds.

Mission Accomplished