Tuesday, January 4, 2011

Understanding VIM : Beginner's Tutorial-2

Cont. from previous post.
Here we are going to know about copying/pasting & searching in vim editor. Let's open a file, created previously.
Now, we want to search 'gofer'. If you are not inside command mode, type [esc]. Now, type /gofer & enter. If you want to highlight next appearance of gofer, hit n. If you want to highlight previous appearance of gofer, hit N.

Use these syntax for further editing:
    * /text search for text in the document, going forward.
    * n move the cursor to the next instance of the text from the last search. This will wrap to the beginning of the document.
    * N move the cursor to the previous instance of the text from the last search.
    * ?text search for text in the document, going backwards.
    * :%s/text/replacement text/g search through the entire document for text and replace it with replacement text.
    * :%s/text/replacement text/gc search through the entire document and confirm before replacing text.

If you accidentally deleted the lines using dd key, you can paste it back using p & P keys. Use v/V to move cursor using arrow keys, then use y to copy some text & paste it using p/P.
    * v highlight one character at a time.
    * V highlight one line at a time.
    * Ctrl-v highlight by columns.
    * p paste text after the current line.
    * P paste text on the current line.
    * y yank text into the copy buffer.

Now, type : to get into last-line mode. hit w to write or if you want to write it in different file, do w & enter. You can also save the file using ZZ which will save & quit the file.   

No comments:

Post a Comment