Friday, November 26, 2010

Using markers in vi/vim

This feature is useful if for example we want to delete a few lines or characters but we are too lazy to count the lines or characters to use dd or dw command. We can set up to 26 marker using lowercase a-z as the marker's name. The main usage of marker is to mark any location in file.

How to use (we put 'a' as the marker's name):

ma - mark current cursor position as marker named 'a'
`a (backquote a) - move to character marked as 'a'
'a (quote a) - move to first non blank character (line) containing marker 'a'
`` (backquote backquote) - move to last operated marker or toggles with last cursor position, if no marker is set, cursor moves to beginning of file (BOF)
'' (quote quote) - move to beginning og line (BOL) operated marker or toggles with BOL of last cursor position, if no marker is set, cursor moves to BOF

Example on using marker:

If you want to indent 5 lines starting from current line: majjjjj>'a
If you want to delete five words using marker: mawwwwwmb`ad`b

That's all friends :)

2 comments:

:wq said...

u should have chosen a better example for marker :)

> If you want to indent 5 lines starting from current line: majjjjj>'a

another way is to use the ':' operator, this operator operates on lines of text.

:.,+4>

this select the current line
and the next 4 line; and indent it

> If you want to delete five words using marker: mawwwwwmb`ad`b

5dw

blackorga said...

Hi :wq,

Thank you for the suggestions, I just want to make the example easy to understand :). if you have good example in using marker, feel free to comment.

Thank you