[Linux-vi] The simple set of VI command

Source: Internet
Author: User

Source:https://www.cs.colostate.edu/helpdocs/vi.html

What is VI?

The default editor, which comes with the UNIX operating system is called VI (visual Editor). [Alternate editors for UNIX environments include Pico and Emacs, a product of GNU.]
The UNIX vi Editor is a full screen editor and have both modes of operation:
Command mode commands which cause action to is taken on the file, and
Insert mode in which entered text was inserted into the file.
In the command mode, every character typed was a command that does something to the text file being edited; A character typed in the command mode may even cause the VI editor to enter the insert mode. In the insert mode, every character typed are added to the text in the file; Pressing the <Esc> (Escape) key turns off the Insert mode.
While there was a number of VI commands, just a handful of these is usually sufficient for beginning VI users. To assist such users, this Web page contains a sampling of basic VI commands. The most basic and useful commands is marked with a asterisk (* or star) in the tables below. With practice, these commands should become automatic.
Note:both UNIX and VI are case-sensitive. Be sure the not-use-a capital-letter-in-place of a lowercase-letter; The results is not being a what expect.
To Get in and out of VI

To Start VI

To use VI on a file, type in VI filename. If the file named filename exists, then the first page (or screen) of the file would be displayed; If the file does not exist, then an empty file and a screen is created into which to enter text.
* VI filename edit filename starting at line 1
vi-r filename recover filename that is being edited when system crashed
To Exit VI

Usually the new or modified file is saved if you leave VI. However, it's also possible to quit VI without saving the file.
note:the cursor moves to bottom of the screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.
*:x<return> quit VI, writing out modified file to file named in original invocation
:wq<return> quit VI, writing out modified file to file named in original invocation
:q<return> quit (or exit) VI
*:q!<return> quit VI Even though latest changes has not been saved for this VI call
Moving the Cursor

Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the VI Editor screen (or window). You must use the key commands listed below. On some UNIX platforms, the arrow keys is used as well; However, since VI is designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produ Ce strange effects in VI and should is avoided.
If you go back and forth between a PC environment and a UNIX environment, you may find the this dissimilarity in methods For cursor movement are the most frustrating difference between the other.
In the table below, the symbol ^ before a letter means the <Ctrl> key should is held down while the letter key is pressed.
* J or <Return>
[or Down-arrow] move cursor down one line
* k [or Up-arrow] move cursor up one line
* H or <Backspace>
[or Left-arrow] move cursor left one character
* L or <Space>
[or Right-arrow] move cursor Right one character
* 0 (zero) move cursor to start of current line (the one with the cursor)
* $ move cursor to end of current line
W move cursor to beginning of next word
b Move cursor back to beginning of preceding word
:0<return> or 1G move cursor to first line in file
:n<return> or NG move cursor to line n
:$<return> or G move cursor to last line in file
Screen manipulation

The following commands allow the VI Editor screens (or window) to move up or down several lines and to be refreshed.
^f move forward one screen
^b Move backward One screen
^d Move down (forward) one half screen
^u Move up (back) one half screen
^l redraws the screen
^r redraws the screen, removing deleted lines
Adding, changing, and Deleting Text

Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.
Perhaps the most important command is the one, the allows you-to-back, and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back to more than one step.
* U UNDO WHATEVER you JUST did; A simple toggle
The main purpose of an editor are to create, add, or modify text for a file.
Inserting or Adding Text

The following commands allow you to insert and add text. Each of these commands puts, the VI editor into insert mode; Thus, the <Esc> key must is pressed to terminate the entry of text and to put the VI editor back into command mode.
* I insert text before cursor, until <Esc> hit
I Insert text at beginning of until <Esc> hits
* A append text after cursor, until <Esc> hits
A append text to end of the current line, until <Esc> hits
* o Open and put text in a new line below current line, until <Esc> hits
* O Open and put text in a new line above current line, until <Esc> hits
changing Text

The following commands allow you to modify text.
* R Replace single character under cursor (no <Esc> needed)
R replace characters, starting with current cursor position, until <Esc> hits
CW Change the current word with new text,
Starting with the character under cursor, until <Esc> hits
CNw change N Words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
C Change (replace) the "characters in", until <Esc> hit
CC Change (replace) the entire, stopping when <Esc> was hit
NCC or cNc change (replace) the next N lines, starting with the
Stopping when <Esc> was hit
Deleting Text

The following commands allow you to delete text.
* x Delete single character under cursor
Nx Delete N characters, starting with character under cursor
DW Delete the single word beginning with character under cursor
DNw delete N words beginning with character under cursor;
e.g., d5w deletes 5 words
D Delete the remainder of the line, starting with current cursor position
* DD Delete entire current line
Ndd or dNd Delete N lines, beginning with the current line;
e.g., 5DD deletes 5 lines
Cutting and Pasting Text

The following commands allow you to copy and paste text.
YY copy (Yank, cut) the current line into the buffer
Nyy or yny copy (Yank, cut) the next N lines, including the current line, into the buffer
P put (paste) the line (s) in the buffer to the text after the
Other Commands

Searching Text

A common occurrence in text editing are to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.
/string search forward for occurrence of string in text
? string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction
Determining Line Numbers

Being able to determine the line number of the "current" or the "total" number of lines in the "file Being edited" is Somet IMEs useful.
:. = Returns line number of bottom in the screen
: = Returns the total number of lines at bottom
^G provides the current line number, along and the total number of lines,
The file at the bottom
Saving and Reading Files

These commands permit you to input and output files other than the named file with which you are currently working.

: R filename<return> Read file named filename and insert after current line
(The line with the cursor)
:w<return> write contents to file named on original VI call
: W newfile<return> Write contents to a new file named NewFile
: 12,35w smallfile<return> Write the contents of the lines numbered the through to a new file named Smallfile
: w! Prevfile<return> write contents over a pre-existing file named Prevfile

[Linux-vi] The simple set of VI command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.