Pages

Monday, January 7, 2013

Vi Editor Tips

vi +10 list.txt Open and File and Put the Cursor at line 10
vi +/END sample Open the File Sample and Goes to the first pattern matching END.
View <File Name> Only In Read Only Mode

0 moves to the immediate beginning of the line
$ moves to the immediate end of the line
^ moves to the first non-whitespace character of the line
~ Change the Case of the Character below Cursor
% Jump Parenthesis
Using the * key searches for the word under the cursor. Use # to do the same thing backwards.
ggn to jump to the first match, or GN to jump to the last.
Press / then Ctrl-r then Ctrl-w to copy the current word to the command line
press :s , to comment a Line
Using the * key searches for the word under the cursor. Use # to do the same thing backwards. more
Using the % key on brackets (([{}])) will find the matching one. more
Using the . key will repeat last change.
Use u to undo, and <C-R> to redo (reverse an undo).
Hitting gf will open the file under the cursor.
:read !ls : reads the Output of the ls command in the end of file
:12read !ls :reads the Output of the ls command in the 12 line of the file
V : select the lines
v : Selects Range Of Text Lines
CTRL + v : select in the column way of lines
gv : reset the selected block

With SHIFT
Shift + I Append Text at the Start of the Line
Shift + A Append Text at the end of the Line
Shift + K Man Page
Shift + O create a Blank line above
Shift + R Replace Mode
Shift + C Change the Rest Of the Line
Shift + R Replace the Line
Shift + A Append at the end of the line
Shift + V visual Mode To Select Lines
SHIFT + C Visaul Mode select lines and Delete the Selected Lines
Shift + zz Save and Exit
Shift :+ B Word By Word
Shift + D Delete from that point
Shift + J Back Ward
Shift + o new Line
Shift + C Delete the Current Line

Press Escape and Do these , Don’t use Colon
1G : Go To First Line
0G : Go To Last Line
gD : Place the Cursor under a Word ,it will take you to the first occurrence of the Word
A; : Append a Semi colon at the end of the line
gD :also takes you to the global variable declaration
<d><d> To delete a single line, type
d W (Caps) To Delete a Single Word :
(Z) (.) Scroll the Current Line as the Middle of the Line
(Z) (Enter) Makes the Current Lines the Top Line On Screen
Cc Remove Current Line
U Undo Change
Yyp duplicate a Line
yy or Y : copy Current Line
DD : Delete the Current Line
6j: GO Down 6 lines
D10:delete 10 lines from cursor

With Colon
:! < Command > Run a Commnad
:pwd Current Working Directory
set number display line numbers (not part of document)
:r <filename> Open another file and include the contents in the current file
:.<DOT> represents the Current Line
:$ Represents the Last Line
:r !df –hT Executing the Command and adding the output to the Current file
:shell Start A Shell
:sh Go to a Shell prompt ,do the work and enter exit to return back to file
:r! command Read in results of a command
:earlier 2m or earlier 2h (If vim was open two hours ago.)
u Undo ( u in normal mode or :u)
:redo Redo

Working with 2 or More Files
Vi sample1 sample2 Or vim -p Multiple Files
:n : Go to Next File
:rew : Go back To Previous File
:new <File Name> in one file : splits the Files
vi -o File1 File2 view 2 files at Same time
vi -p File1 File2 View files in Tab
:n Go to Next File
:rew Go back To Previous File

Tabs
Open File , Copy Contents
Now :tabnew <file Name>
Paste the Contents , Save the File
:tabn : Next Tab
:tabp : Previous Tab
qa : close all tabs at once
wqa : close all tabs after saving them
With CTRL
ctrl + shif t+ N scanning Tags in Xml Files
CTRL + ^ Flip Between 2 Files
Ctrl + r redo
Ctrl + U back 1/2 screen
Ctrl + D forward 1/2 screen
Ctrl + ~ switch between files
CTRL +o = first search line
CTRL + I = last line search

Mark : Mark Your Current Position, and move away for a while.You can then go back to the old cursor position by hitting a few keys
ma mark current position with letter 'a'
`a go back to mark a
'a put the cursor at the start of line a
:marks to see a list of marks made.

Search /foobar<Enter> search for foobar
n go to the next result
N go to the prev result
# search for the word under the cursor (back)
* search for the word under the cursor (fwd)
Browsing files with Vim
Open a directory with 'vim path/to/dir' or from within vim, :e path/to/dir. A file Browser opens up that one can use to browse, and make modifications. Use
Enter” to enter a directory, and j, k to move up and down.
The following should be helpful:
d - to make a directory
D - to delete one
R - to Rename

Tips
shift + vggx - delete all lines from curser to end of file
:e - loads the Updated File in VIM

If you want to do the same thing to a collection of lines, like cut, copy, sort, or format, you first need to select the text. Get out of insert mode, hit one of the options below, and then move up or down a few lines. You should see the selected text highlighted.

V - selects entire lines
v - selects range of text
ctrl-v - selects columns
gv - reselect block
After selecting the text, try d to delete, or y to copy, or :s/match/replace/, or :center, or !sort, or...

Here's one way to move selected text over a few spaces:

- select a chunk of code using capital V and the arrow keys (or j, k)
- type colon
- then type s/^/ /
- hit return
What you've done is replace the beginning of each selected line (the ^ symbol means "the beginning of the line") with spaces.

Add Output of the Command to File
AAS-A2 $ find . | vim -
Vim: Reading from stdin...

Some more tips include
CTRL + F : last Line
ggVg : select Lines in a file
1gg: takes the cursor  to first line
x: save and Quit from a file

Comments
In order to comment a Section of lines,
Put your cursor on the first # character, press CtrlV (or CtrlQ for gVim), and go down until the last commented line and press x, that will delete all the # characters vertically.


For commenting a block of text is almost the same: First, go to the first line you want to comment, press CtrlV, and select until the last line. Second, press ShiftI#Esc (then give it a second), and it will insert a # character on all selected lines. For the stripped-down version of vim shipped with debian/ubuntu by default, type : s/^/# in the second step instead.


More To Come ,Happy learning