inteliture.com
Google

Saturday, July 14, 2007

Learn and use the vi linux editor

Introduction to vi.

VI displays a full screen window into the file you are editing. The contents of this window can be changed quickly and easily within vi. While editing, visual feedback is provided (the name vi itself is short for visual).

vi is one of three standard editors on the Linux system, the other two being:

  • ed - This is a line editor. While powerful in its own right, it is tedious to use.
  • ex - This is another line editor, but with the same powers and commands as vi: the names vi and ex - identify a particular user interface rather than any underlying functional difference.

In both ex and ed, visual updating of the terminal screen is limited, and commands are entered on a command line. vi, on the other hand, is a screen-oriented editor designed so that what you see on the screen corresponds exactly and immediately to the contents of the file you are editing.

Linux offers further editors, such as joe, pico and emacs (although the latter has been available for some years before Linux was created).

Its powerful and flexibile and it is an industry standard that is widely available, not only on Linux and Unix, but also on other operating systems.

Common commands.

To insert new text

esc + i ( You have to press 'escape' key then 'i')

To save file

esc + : + w (Press 'escape' key then 'colon' and finally 'w')

To save file with file name (save as)

esc + : + w "filename"

To quit the vi editor

esc + : + q

To quit without saving

esc + : + q!

To save and quit vi editor

esc + : + wq

To search for specified word in forward direction

esc + /word (Press 'escape' key, type /word-to-find, for e.g. to find word 'shri', type as
/shri)

To continue with search

n

To search for specified word in backward direction

esc + ?word (Press 'escape' key, type word-to-find)

To copy the line where cursor is located

esc + yy

To paste the text just deleted or copied at the cursor

esc + p

To delete entire line where cursor is located

esc + dd

To delete word from cursor position

esc + dw

To Find all occurrence of given word and Replace then globally without confirmation

esc + :$s/word-to-find/word-to-replace/g

For. ex. :$s/mumbai/pune/g
Here word "mumbai" is replace with "pune"

To Find all occurrence of given word and Replace then globally with confirmation

esc + :$s/word-to-find/word-to-replace/cg

To run shell command like ls, cp or date etc within vi

esc + :!shell-command

For ex. :!pwd

No comments: