Sunday, 25 March 2012

Vim commandline Tricks

ctrl+u  -------> undo in vim editor

:w filename -------> save as filename without exiting from vim

vim ctechz +25 -----> open file and go to line 54, any : command can be run using + on command line

vim -O ctechz1 ctechz2  -----> open ctechz1 and ctechz2 side by side

:ls    -----> list buffers

:bd  ----->  delete buffer and any associated windows

Ctrl+g    ----->  Show file info including your position in the file

vi filename   ----> Opening a file / Creating text

Edit modes: These keys enter editing modes and type in the text of your document. 

i     Insert before current cursor position
I     Insert at beginning of current line
a     Insert (append) after current cursor position
A     Append to end of line
r     Replace 1 character
R     Replace mode
<ESC> Terminate insertion or overwrite mode

Deletion of text:

x       Delete single character
dd     Delete current line and put in buffer
ndd   Delete n lines (n is a number) and put them in buffer
J        Attaches the next line to the end of the current line (deletes carriage
          return).

Undo:

u     Undo last command

Cut and Paste:

yy     Yank current line into buffer
nyy   Yank n lines into buffer
p       Put the contents of the buffer after the current line
P       Put the contents of the buffer before the current line

Cursor Positioning:

^d     Page down
^u     Page up
:n      Position cursor at line n
:$      Position cursor at end of file
^g     Display current line number
h,j,k,l Left,Down,Up, and Right respectivly. Your arrow keys should also work if
                                                your keyboard mappings are anywhere near sane.

String Substitution:

:n1,n2:s/string1/string2/[g]       Substitute string2 for string1 on lines
                                                 n1 to n2. If g is included (meaning global), 
                                                 all instances of string1 on each line
                                                 are substituted. If g is not included,
                                                  only the first instance per matching line is
                                                 substituted.

   ^  matches start of line
    .  matches any single character
    $  matches end of line

These and other "special characters" (like the forward slash) can be "escaped" with \ i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT".

Saving and Quitting and other "ex" commands:

These commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window. They are called "ex" commands because they are commands of the ex text editor - the precursor line editor to the screen editor  vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen)
Press <ESC> to exit from an editing mode.

:w                      Write the current file.
:w new.file         Write the file to the name 'new.file'.
:w! existing.file  Overwrite an existing file with the file currently being edited.
:wq                     Write the file and quit.
:q                        Quit.
:q!                       Quit with no changes.

:e filename          Open the file 'filename' for editing.

:set number         Turns on line numbering
:set nonumber      Turns off line numbering










No comments:

Post a Comment