Friday, March 19, 2010

Very Interactive(VI) editor commands

This has been a request from couple of people, putting it down for them and for everyone who is looking for frequently used vi editor commands.

Edit commands:
-------------------
x - delete one character at a time
nx - delete n characters at a time, replace n with number you want
dd - delete/cut a line
ndd - delete/cut x lines (replace n with number you want Eg: 10dd would delete 10 lines)
yy - yank a line(copy to clipboard)
nyy - yank n lines(replace n with number you want Eg: 10yy would yank 10 lines)
p - paste from the clipboard(deleted lines are like copy too, they will be in clipboard, so p after dd or yy will paste whats there in the clipboard)

*Remember: All the above commands are relative to the current cursor position. i.e. If you are at line 20 and performing dd, line at that position will be deleted/cut. Similarly p operation will paste after 20th line.

More Edit commands:
--------------------------
dw - delete/cut a word
ndw - delete/cut n words (replace n with number you want Eg: 3dw will delete 2 words)
cw - change or replace word (move the cursor to the word you want to change and give cw, then type in the replacement word)
ncw - change n words at a time(Eg: 2cw will help you replace two words)
o - to insert a line below the line where the cursor is positioned
shift+o - to insert a line above the line where the cursor is positioned

Cursor positioning or movement:
---------------------------------------
<- or h - to move left -> or l - to move right
up arrow or k - to move up
down arrow or j - to move down
0(zero) - to move to the first character of the current line
$ - to move to last character of the line
w - to move cursor to the beginning of the next word
b - to move cursor to the first character of the preceding word
:0 - to move to the first line of the file(what you see here is zero)
:n - to move to the nth line of the file(replace n with number of your choice)
:$ - to move the cursor to the last line of the file

* CR- carriage return/Enter key in the keyboard

Scroll commands:
---------------------
^d (ctrl+d) - scroll half screen down
^u (ctrl+u) - scroll half screen up
^f (ctrl+f) - scroll full screen down
^b (ctrl+b) - scroll full screen up

(do it for yourself to see the difference, choose a bigger file to see the difference between these scrolls for yourself)

Search commands:
-----------------------
/string - to search for a string (replace string with your search string)
(n to find the next match, shift+n to find the previous match.

Search and Replace:
------------------------
:s/old_string/new_string - replaces the first occurrence of "old_string" with "new_string" in the current line (Eg:- :s/import/export will replace first occurrence of the word import with export in the current line)
:s/old_string/new_string/g - will replace all occurrences of "old_string" with "new_string" in the current line
:%s/old_string/new_string/g - will replace all occurrences of old_string with new_string in the file.

(**Remember everything is case sensitive here, to make your search & replace case insensitive see below)
:s/old_string/new_string/i
:s/old_string/new_string/gi
:%s/old_string/new_string/gi

see the 'i' at the end makes it case in-sensitive.

To perform search and replace between selected lines, you can use the commands below.
:l1,l2s/old_string/new_string/g - replace l1 and l2 with line numbers you want. (Eg:- :1,10s/import/export/g you know what this means... isn't it?)

Undo:
--------
u - to undo previous commands

Switching modes:
----------------------
i - insert mode
a - append mode
esc - to get out of any mode you already chosen

I will stop with these basic commands you would need to start.

In case if people need more useful commands at advance level, I will put a separate post later.

Happy VI'ing :))

7 comments:

  1. edit the post and add 'n' in cmds like del 'n' words.
    scroll cmds - 'ctrl f' not 'ctrl u'

    can add this - :+n (move n lines ahead relatively) also :-n

    Thanks for doing this bro!

    ReplyDelete
  2. Thanks anand. I hope to make it useful for all others Lets see. ctrl+f & ctrl+u both are there.

    I added '<'n'>' for n and did not see the preview before posting... the editor just removed '<'n'>' treated it like a tag I guess.

    ReplyDelete
  3. Thanks for ur idea anna..It will be very much useful for us... :-)

    ReplyDelete
  4. thanks anna.by using this only i am going to start.

    ReplyDelete
  5. @bhuvana & buvi: You are welcome people. This is just one way of doing it.. I mean the SQL to Relational Algebra... do some thinking and do it efficiently... actually I hate the way I am doing it right now... I see some redundant code... so happy programming.. do well!

    ReplyDelete
  6. @anand & prasanna: you have been the inspiration for starting this blog... hope I will keep it alive.

    ReplyDelete