News
Latest
Support Area
Download
Help and Advice
Links
Home
About us
Contact us
Copyright
Privacy Policy


Mastering VI

|
Vi is a complex editor that is supplied with almost every linux distribution.

typing "vi" at your prompt should run the editor, if not consult you package manager.

using vi followed by a filename will edit that file, or create it if iit doesn't exsist

Basic commands

When editoring a file you can navigate around using the keys "H","J","K" and "L". alternatively thr curser keys should also provide the same function. while navigating around the file you will be in a normal mode, this will allow you to navigate around the file without modifing it. using the "i" character will put you in an insert mode. This will allow you to insert and type freely within the document. Using the "Escape" key will always return you to normal mode.

To quit the application while in normal mode use ":q", to write the file use ":w". These commands can the be chained together to perform complex tasks within the editor, for example ":wq" will write the file, then quit. vi will store a large undo buffer so you will be able to use "u" in normal mode to undo any changes. The "!" is unconditional and can be used to overwrite any queries the system may have. "q!" will quit regardless of changes. "w!" will write the file regardless of errors, unless the system realises you don't own the file being edited and you don't have write permission.

Navigation.

Page up and down will work as expected, the"$" character will go to the end of line, the "^" will move to the start of the line. "w" will move to the next word, while"e" will move to the end of the next word.

Amending.

The delete command in normal mode is "d" using it twice together will delete the current line, however this command can be used with the others mentioned to form werful complex commands. "d$" will delete from your current position to the end of the line. "5dd" will delete five lines, "5dw" will deletee the next five words and so on.

"a" is the append mode which works in the same way as insert. Similarly "R" is replace mode.

Copy and Paste.

The copy command is "yy" this will copy the current line where as "100yy" will copy the current line followed by the next ninetynine lines "p" will paste the line in the copy buffer at the present location. using t is with numbers, eg "10p" will in this example paste in the copy buffer ten times.

Search and Replace.

Searching through a document and replacing text can be complecated using regular expressions, but remebering that commands in vi can be chained together will help in your understanding. The "/" command is used for search, followed by your search criteria, using the forward slash on its own will continue the search after it has matched your criteria.

To replace one instance of a word you can use the following.

"s/word1/word2/"

This will search for an instance of word1 and replace it with word2. adding a "g" to the end of the replace command above will replace all instances of "word1" with "word2" on the same line. Using "%" will replace all instances within the document. or you can specify line numbers eg.

":14,19s/word1/word2/g"

This will replace multiple instances of "word1" with "word2" between tge lines of 14 and 19. These numbers can equally be replaced with some of the commands previously. The "c" after the "g" at the end of the line will prompt you to chage or not.

Special charaters in a search string might have to be escaped before it will search correctly. this is done using the "\" character eg.

":1,$s/\/path\/to\/file/\/another\/path\/to\/file/g

This will replace from the first line to the end of the document all references of "/path/to/file" with "/another/path/to/file"

Commands

Using vi you can also run commands and the output will be pasted into the editor. Use ":!" followed by the command eg.

":!ls"

Will paste the contents of your working directory into the editor.

Quick Cheats.

h move left
j move down
k move up
l move right
i insert mode
y yank
p paste
d delete
R replace
u undo
:q quit
:w write
/ search downwards
? search upwards
^ goto start of line
$ goto end of line

|
© 2024 Localhost. All rights reserved. Terms of Use and Disclaimer
Designed by My Hosts.com
   Last updated March 21 2017.