Friday, January 20, 2012

Editing Text Files With Vim Editor


Here are some of the fundamental things you'll need to do when you edit a document in vi.

Starting vi

To start vi, just type the command
vi
to the UNIX shell. If you want vi to start with a file already loaded into a buffer, type
vi <filename>
where <filename> is the name of the file you want to edit.

Quitting vi

To exit vi and return to the UNIX shell, type :q. If you have made changes to the buffer since the last time you saved it to disk, vi will say "No write since last change (":quit!" overrides)". Type :q! to exit without saving or :wq to exit with saving.

Getting help

vi does not have an on-line help system.You can temporarily exit to the shell and see the man page by typing the command :!man vi.
Vim editor has 3 mode:
Command mode: Cursor movement, change, delete, yank, put, search
Insert mode: type in new text, return to command mode with <ESC>
Ex mode: configuring, exiting, saving, search and repalce

Cursor motion

In addition to basic cursor motion, vi provides some other handy cursor motion functions:
  • (number zero) or ^ (Shift-6) moves the cursor to the start of the current line.
  • $ moves the cursor to the end of the current line.
  • w moves the cursor forward to the next word.
  • b moves the cursor back to the previous word.
  • 1G moves the cursor to the start of the buffer. *
  • G moves the cursor to the end of the buffer. *
  • #G moves to the line where # is the line number.
  • Control-F moves the cursor forward one screen.*
  • Control-B moves the cursor backward one screen.*
  • Control-D moves the cursor forward one half screen.*
  • Control-U moves the cursor backward one half screen.*
Here are some ways to search for a text string which will reposition the cursor. Wild cards include . (period) to match a single character and * to match any string:
  • /x searches forward for string x.*
  • searches forward for next occurence of found text.*
  • ?x searches backward for string x.*
  • N searches backward for previous occurence of found text.*
Command marked with asterisk (*) can also be used with the more utility to view files.

Inserting and deleting text

Deleting text is easy but neither the Backspace nor Delete key will delete a character. Here are some ways to delete text:
  • x deletes forward one character.
  • deletes backward one character.
  • or d$ deletes from the point to the end of the line.
  • dw deletes forward one word.
  • dd deletes one line.
  • #dd deletes multiple lines where # is the number of lines to delete

Cutting and pasting text regions

Changing text

Here are some ways to replace text using c (change) and r (replace) commands:
  • cw allows the current word to be replaced until terminated with Escape.
  • cc allows the current line to be replaced until terminated with Escape.
  • C or c$ allows the rest of the line to be replaced until terminated with Escape.
  • r allows the current character to be replaced by the next character that is typed.
  • allows all text to be replaced until terminated with Escape.

Undoing changes

It is possible to undo the change you have made to a file by entering the command u.

0 nhận xét:

Post a Comment