linux vi

(Reference: http://www.linfo.org/vi/edit.html)

1.How to copy and paste text with vi or vim

-----------------------------------------------

The ability to duplicate text in an editor can be handy. vi and vim have several useful copy and paste commands.

(gg will go the first line, G will go to the last line)

The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:

Y 2Y
10Y
yG

To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:

P p

It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:

yw y$

The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.

Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.

----------------------------------------------------------------------------------------

Once you have typed some text, you can use the four basic commands for moving the cursor around the text. These commands enable you to go to any desired location in order to modify the text, including making insertions and deletions. The four basic cursor positioning commands are:

h    move cursor one character to left 
j    move cursor one line down 
k    move cursor one line up 
l    move cursor one character to right

Each of these commands can be either used by itself or modified by typing an integer in front of it to indicate the number of characters or lines to move. For example, typing (in command mode, of course)

3j

will move the cursor down three lines. Or typing 2h will move it two characters to the left.

These commands can be repeated by holding the key down. If you attempt an impossible movement, such as pressing k when the cursor is on the top line, the screen might flash or a beeping sound might be made (depending on how your computer is set up).

The cursor can be moved directly to any desired line by using the G command preceded by the line number. For example, typing

5G

moves the cursor to the fifth line from the top of the text. Just typing G without any number moves the cursor to the final line of text.


The x (lower case "x") command deletes the character immediately under (i,e., covered by) the cursor. To delete any desired character, just switch to the command mode (if you are not already there) and then use an appropriate combination of the hjk and l commands (of course, one at a time) to move the cursor to that character. Then type x and the character is deleted.

By pressing x continuously instead of just hitting it once, the cursor continuously moves to the right and each character under it is successively deleted.

The X (upper case "X") command is similar except that it deletes the character to the left of the cursor rather than the character under it.

There are several additional commands for deleting text. The D (upper case "D") command removes the text on the current line from the character under the cursor to the end of the line.

The d (lower case "d") command is very flexible because it can be modified to delete any number of characters, words or lines. Typing d by itself will not do anything, but typing dw causes the character the cursor is resting on and the remaining characters to the right of it in the same word to be deleted. (The "w" stands for "word.")

Typing 2dw causes the character under the cursor, the remaining characters to the right of it in the same word and all of the characters in the next word to be deleted. For example, typing 2dw with the cursor on the "a" of the string "pineapple plantation" causes the string "apple plantation" to be deleted.

As another example, typing 3dw with the cursor on the "j" of the string "the bluejay flew south" causes the string "jay flew south" to be deleted. That is, "jay" and two words to the right of it are deleted.

Deleting an entire line can be accomplished with the dd command. This command can also be used to delete multiple lines by preceding it with an integer representing the number of lines to be removed. For example, typing 2dd will delete two consecutive lines beginning with the current line.

With some terminals, deletion of a line causes it to be replaced on the screen with an "@" character. This character merely represents an empty line and is not inserted into the text. Its purpose is to relieve the processor from having to redraw the screen (i.e., change the whole screen). This character can be removed if desired by typing r (or l on some terminals) while holding down the CTRL key.

The change command c (lower case "c") differs from the delete command in that it not only deletes a section of text but also activates insert mode to allow you to type in replacement text. After you have completed typing in the replacement text, be sure to press ESC to return to the command mode.

As is the case with d, the c command is not used by itself but is only used in combination with another letter after it and an optional integer before it.

For example, the command cw (which stands for "change word") deletes the characters in the current word under and to the right of the cursor and then switches vi to the insert mode so that you can enter text to replace the deleted characters. The number of new characters typed in can be the same as, fewer or more than the number deleted.

The amount of text to be changed can be increased by preceding the command with a number. For instance, typing 2cw will additionally remove the next word for replacement with whatever is typed in. The space between the words is not preserved.

The d and c commands can also be modified by other characters in addition to "w." For example they can be used with "b," which stands for "back." Thus, typing 3bd will delete the characters to the left of the cursor in the current word together with the two words to the left of the current word.

The cc command erases the current line, leaving it blank and awaiting replacement text. Preceding this command with an integer will delete that number of lines, beginning with the current line. For example, typing 5ccwill allow you to change five consecutive lines starting with the current line.

Another change command, R, differs from the c commands in that it does not initially delete anything. Rather, it activates insert mode and lets you replace the characters under the cursor one at a time with characters that you type in.

vi supports several types of transposition. Transposing the order of two adjacent characters is easy with the xp command. Just place the cursor on the left-most of the two characters, type x to erase the left character and then type p for the deleted character to be put to the right of the cursor.

Two adjacent words can be transposed with the deep command. To use it, position the cursor in the space just to the left of the word on the left and type deep. Two adjacent lines can be transposed with the ddpcommand by placing the cursor on the upper line and typing ddp.

It is also a simple matter to change the case of a letter. When the cursor is over the desired letter, hit the "~" (tilde) key. This will change a capital letter to a small letter and visa versa.

The J (upper case "J") command is used to join the next line to the current line. The opposite operation, splitting a line, is accomplished in insert mode by merely positioning the cursor over what will be the first character of the new line and then hitting ENTER.

vi also has an undo capability. The u (lower case "u") command is used to reverse the effects of an already issued command that has changed the buffer, but which is not yet written to disk. U (upper case "U") undoes all of the changes that have been made to the current line during your current visit to it.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值