Skip to main content

VI Commands - Useful tips & tricks

Opening a file
# vi filename

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 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).

Oops

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"

Examples:

:1,$:s/dog/cat/g Substitute 'cat' for 'dog', every instance
for the entire file - lines 1 to $ (end of file)

:23,25:/frog/bird/ Substitute 'bird' for 'frog' on lines
23 through 25. Only the first instance
on each line is substituted.


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 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




Digg this!Add to del.icio.us!Add to Techorati!isquare!friendfeedReddit!Add to Yahoo!
Print Page
Feedbacks: We appreciate feedbacks and suggestions about our website mailto:bala@techgyaan.org?subject=Comments%20from%20TechGyaan%20Website

Comments

Popular posts from this blog

Download of the Day!

Amarok 2.0.2 has released Amarok is an open source alternative music player for iTunes. Amarok supports Linux, Unix, MacOSX and windows Operating system. It is smiliar to iTunes just drag & drop the songs to play list. Amarok also lets you listen to internet radio ( mp3 streaming ) including music from last.fm . Features Automatic cover Lyrics download Dynamic playlists Visualizations and Podcasting. These are only some of the great new features of Amarok 2. Give it a try! Download here An least but not last - Amarok will also synchronize your music to your iPod. For the more technical users a scripting interface allows you to extend the functionality of Amarok. Print Page Feedbacks: We appreciate feedbacks and suggestions about our website info@techgyaan.org

How to fix Solaris "/lib/svc/method/net-physical "network settings is misconfigured”

Yesterday I came across an issue with the server being unable to access it switched to the  maintenance mode with some a strange message; “ How to fix Solaris "/lib/svc/method/net-physical "network settings is misconfigured”. Dec  9 10:05:59 techgyaansvr svc.startd[7]: [ID 652011 daemon.warning] svc:/network/physical:default: Method "/lib/svc/method/net-physical" failed with exit status 96. Dec  9 10:05:59 techgyaansvr svc.startd[7]: [ID 748625 daemon.error] network/physical:default misconfigured: transitioned to maintenance (see 'svcs -xv' for details) Actual message is that the IP address already exists, but is not configured. We need to clear the network & flush the settings to make it accessible Resolution: Check the service which is disabled #svcs –xv [this command will show you the services which is disabled by system] Take the network interface offline #ifconfig eri0 down [this command will bring the interface down] #ifconfig eri0 unplumb [this co...

Increase your RAM? free of cost…Really Worth It!!!!

A Very useful tip . Please try this and use RAM efficiently. Now this is called a tip of the year! While working with the Task Manager I observed the following. You can also try it out. 1.Start any application, say Word. Open some large documents. 2.Now start the Task Manager processor tab and sort the list in descending order on Memory Usage. You will notice that Winword.exe will be somewhere at the top, using multiple MBs of memory. Note down the number. 3.Now switch to Word and simply minimise it. (Do not use the Minimize All option of the task bar). 4. Now go back to the Task Manager and see where Winword.exe is listed. Most probably you will not find it at the top. You will typically have to Scroll to the bottom of the list to find Word. Now check out the amount of RAM it is using. Compare it with the original. Surprised? The memory utilisation has reduced by a huge amount. 5.So where is the tip of the year? Simple? Minimise each application that you are currently not working on ...