Featured Post

The great debacle of healthcare.gov

This is the first time in history when the president of the United States of America, or probably for any head of state around the world,...

Wednesday, November 7, 2007

Useful vi commands

Useful vi Commands

0. :q -> quit ; :w -> write the change ; :q! -> quite anyway without saving ; :qw -> quite after saving the changes

1. yw -> yank (copy) a word
2. yy -> copy the current line
3. yG -> copy from cursor to EOF
4. 2y -> copy 2 lines from cursor
5. y2w -> copy 2 words from cursor
6. p/P -> copy before or after cursor

7. :set number -> see the line numbers
:set nonumber -> hide the line numbers
8. :set list -> show the newline character
:set nolist -> hide the newline character

9. :%s/search-text/replace-text/g -> Search and replace text with greedy option set
10. /search-text -> search any text and then press n to search next or ? to search previous

11. Ctrl+f -> to move one screen forward
12. Ctrl+b -> to move one screen backward

13. :line_number -> to go to the specified line number

14. * (Shift+8) -> Hightlight the word at the cursor position and search for next match
:noh to remove the highlight
15. :e -> Open another file to edit

16. :J -> Join the line with next line giving a space in between

17. : %s/^I/\r/g -> Replace all with ; works on gVim
: %s/
^I/^M/g -> to achieve the same in unix vi. To get '^M', use Ctrl+v first then hit enter.
: %s/\m/,/g -> insert ',' between each character. Works on gVim
: %s/\n/,\r/g -> replace the new line (\n) using ',' and carriage return (\r) . works on gVim
: %s/.*ABCD/ABCD/g -> replace words from beginning of line up to word 'ABCD' and replace that with only word 'ABCD'
e.g. "10/17/2008 11:55 AM 229,888 ABCD XY - FOO - 1.0"
becomes "ABCD XY - FOO - 1.0"
:'<, '>s/^^I/&A/g -> replace all tabs from beginning of the line with A (the selected lines only)
:%s/^^I/&A/g -> Same as above without any selection
:%s/\s*$// -> replace all trailing white spaces

18. vEU -> change from the cursor to end of word to uppercase
v$U -> change from the cursor to end of line to uppercase
vEu -> change from the cursor to end of word to lowercase
v$u -> change from the cursor to end of line to lowercase

19. u -> undo delete
U -> undo the last change
Ctrl+r -> Redo the last change

20. : -> to recall history

21.: & -> repeat last :s command
: . -> repeat last text changing command
: , -> repeat last search command. In gVim use 'n'


22.
M -> Move to the middle of the current screen
H -> Move to the top of the current screen
0 -> Beginning of current line
+/Return -> First character of next line
dL -> Delete up to last line on the screen
D -> Delete remainder of the current line
"xyy -> copy current line to buffer x
"xp -> copy the line from buffer x

23. Vertical selection and insertion
^v to start vertical selection
press 'I' or 'i' then enter any letter you want to append and finally press escape.

24. Delete lines with search result
g/string/d - Delete lines that contains the 'string'
v/string/d - Delete lines that DOESN'T contain the 'string'

25. Sort a file
:sort

26. Counting words
:%s/pattern//gn - Count the pattern
:%s/[^ ]\+//gn - Count the space delimited words

27. Recording
Hit q and register character (alphanumeric) e.g. x
Hit q again to stop recording
@x to rerun the recorded keystroke recorded

28. Find next key word
i (insert mode) -> Ctrl -> i
this would show the keywords as drop down values to be picked for


Resources:

http://www.brezeale.com/technical_notes/vim_notes.shtml
http://www.rayninfo.co.uk/vimtips.html
https://xyz.pp.se/~thnov/vi.html
http://www.lagmonster.org/docs/vi2.html
http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm
http://vim.wikia.com/wiki/Vim_Tips_Wiki - Vim tips wiki

No comments: