VI copy and paste command --

Source: Internet
Author: User
Tags add numbers

The VI editor has three modes: Command mode, input mode, and last line mode. It is very important to master these three modes:

1. command mode: After VI is started, the command mode is entered by default. From this mode, you can switch to the other two modes by using commands, in any mode, you can press the [ESC] key to return to the command mode. Enter the subtitle "I" in command mode to enter the VI input mode to edit the file.

2. input mode: In this mode, you can edit, modify, and enter the file. In the last line of the editor, "-- insert --" indicates that vi enters the input mode. When modifying input and other operations, we need to save the file. At this time, we need to first return to the command mode and save it in the last line mode.

3. Last line mode: Enter ":" In command mode to enter this mode. There are many useful commands in last line mode.

 

4. Edit operations
Enter input mode command
I insert command a additional command o open command C modify command
R replace command s replace command ESC exit command

Operation in input mode
Home cursor to beginning of Line
End cursor to end of line
Page up and page down
Delete: Delete the character at the cursor position

Delete operation (used in command mode)
X delete a single character at the cursor
Dd deletes the row where the cursor is located.
DW deletes all characters from the current character to the end of a word including Spaces
# X for example, 3x Delete the three characters to the right of the cursor
# Dd For example, 3DD deletes three lines of text from the current row

Cancel operation
U command to cancel the last operation, you can use multiple times to restore the original operation
U cancel all operations
CTRL + R can be used to restore operations using the U command

Copy operation
Run the YY command to copy the entire line to the VI buffer.
YW copies the content from the current cursor position to the end of the word to the VI cache, which is equivalent to copying a word.
Y $ copy the cursor to the cache area at the end of the row
Y ^ copy the cursor to the cache area from the beginning of the line
# Yy. For example, 5yy means copying 5 rows.
# YW: for example, 2yw means copying two words.

If you want to copy the content between Row M and row N, you can enter m in the last row mode. For example, NY: 3, 5y copies the content from row 3 to row 5 to the cache area.

5. search and replace
The search and replacement functions of VI are mainly completed in the last line mode:

Top-down search
/The character seek to be searched./indicates that the search starts from the position where the cursor is located, for example:/work

Bottom-up search
? For example,/work

Replace
: S/old/new replace the old
: S/old/new/g replace all existing old values in the row with new.
: #,# S/old/new/g replace the old value from row # To row # With new
: % S/old/new/g replace the old

If the replacement range is large, add a C command at the end of all commands to force each replacement to be confirmed by the user. For example: s/old/new/C or S/old/new/GC

6. Restore Files
When editing a file, VI will generate a temporary file that starts with. And ends with. SWP. Normally, the file is automatically deleted. If the file is unexpectedly exited, such as a sudden power failure, the file will not be deleted. we can select the command to process it the next time we edit it:

O read-only access without changing the File Content
E. Continue to edit the file and do not restore the content saved in the. SWP file.
R restores the content of the file not saved after the last Edit
Q: Exit vi
D. Delete the. SWP file.
Or use the Vi-r file name to restore unsaved content.

In the GUI:
(1) You can press I to enter the insert mode.
(2) drag the cursor to select the content to be pasted and press the left mouse button to copy the selected block to the buffer zone.
(3) move the cursor to the position to be pasted, and press the middle mouse button (you can press both the left and right buttons) to paste the buffer content.

In a plain text terminal:
(1) Select the text block and use V to enter the visual mode. move the cursor key to select the content.
(2) copy the selected block to the buffer, use y; copy the entire row, and use YY
(3) Cut the selected block to the buffer, use d; cut the entire row with dd
(4) paste the content in the buffer, and use P

Open the second file in the same editing window and use: SP [filename]
Switch between multiple editing files and press Ctrl + W.

Add a number to the front of the command to indicate the number of repetitions, and add a letter to indicate the name of the buffer used.
For help, use: Help [content or command].

 

Set Tab to 4 and automatically convert to space in VI

 

: Set tabstop = 4 "Force tabs to be displayed/expanded to 4 spaces (instead of default 8 ).
: Set softtabstop = 4 "make Vim treat <tab> key as 4 spaces, but respect hard tabs.
: "I don't think this one will do what you want.
: Set expandtab "turn tab keypresses into spaces. Sounds like this is happening to you.
"You can still insert real tabs as [CTRL]-V [Tab].
: Set noexpandtab "Leave tab keys as real tabs (ASCII 9 character ).
: 1, $ retab! "Convert all tabs to space or ASCII-9 (per" expandtab "),
"On lines too to_end-of-file.
: Set shiftwidth = 4 "when auto-indenting, indent by this much.
"(Use spaces/tabs per" expandtab ".)
: Help tabstop "find out more about this stuff.
: Help vimrc "find out more about. vimrc/_ vimrc :-)

 

Copy and paste basic methods in VI

Let's make such a small attempt first. Open the browser, copy a text segment from the webpage, open vim, and paste it with "P". You will find that, the reason is that, unlike windows, VIM has multiple clipboard and is completely independent from the system clipboard, so when you copy the text in the browser, the text is saved in the system clipboard. When you paste it with "P", in fact, he reads the vim clipboard.
So how can we conveniently copy the system clipboard content? Simple! You can use another shortcut "Shift + Insert". If you are using gvim, you can also use the middle mouse button to play the same role. Is it easy?
OK! Let's get started!
As we have mentioned earlier, VIM has multiple clipboard to facilitate copying and pasting when handling complicated characters. Run the ": Reg" command (Press ESC to exit the editing mode, press: To enter the command mode, and then enter Reg and press Enter? Output result on the screen! Are you familiar with this! All the content you copied before! At this time, you may also find that there are so many Vim clipboard, which are separated by numbers and symbols one by one. Here is an example of serial number -- "" 5, have you seen it? It starts. And! Have you seen the clipboard numbered "+? Is the text you copied in the browser just now? Haha ...... That's right! The system clipboard number is "+.
Now that you know how to view all the previous clipboard content, we try to copy the content of a specific clipboard to the file we are editing. We know that the VI paste command is "P", but if you want to paste a specific clipboard, it will not work if you just use P. What should we do? Simple! Can I add the clipboard number? Press ESC to exit the editing mode, and then press "+ P" in sequence? No! "And + how to press? Shift !), Okay! Check whether the text copied in the browser has been pasted into the document? You know how to copy the content in the system clipboard into vim. So how can we copy the characters in VIM to other places? It's easy. We just need to copy the text to the clipboard? ("+ The clipboard is a system clipboard. Forgot ?) In vim, the copy command is Y (Press Y in non-edit mode). Copy the text to the specified clipboard, you only need to add the ID of the clipboard. For example, we want to copy a piece of text in vim and paste it into the address bar of the browser. Step 1: select the text to be copied (in non-editing mode ), then press "+ Y" on the keyboard. OK! It has been copied. Now run the: Reg command to see if it is in the "+ Clipboard? So far, you have mastered the basic usage of VIM clipboard. Of course, the vim clipboard is still very powerful and cannot be described in this article. Please continue to explore it. Introduction to copying and pasting multiple files in Vi!

When editing Vi, you often want to copy one or more lines in file a to file B.
(Maybe many people don't know what to do.) What should I do?
The following are some methods:
1. If you have multiple machines connected to the Internet, you can. NetOn the term virtual terminal, telnet to the host to modify and copy file a, and then open file B and paste it!
2. If it is a single machine, use it of course.UNIXWhat about the built-in editing function?
First, we will briefly introduce some commands in VI Editing:
(A). YW saves the current word to the unnamed buffer zone. "X" can be added to the front to indicate the name of X.
The buffer zone (X is a-z). You can add numbers to indicate the number of words stored in the buffer. In the future, you can use P or P.
Retrieve
YY saves the current row to the unknown buffer. For usage, see YW

(B). The command is as follows: A-Z is a buffer, and can also be removed from the buffer specified by number 1-9.
The namebuffer can be used to store several text segments, making it easy to access, move, or rearrange text segments in the future. These buffers are named with letters A to Z! To access one of these buffers, use double quotation marks
For example, to insert a row into a buffer, you can type:
"Ayy
To put the text back in the file, type "AP


(C). M:
The M letter marks the current row with a letter. In the future, use the 'letter to move the cursor to the current row,
(Refer to the 'COMMAND)

Column: 1. Add four rows in file a to file B.

The command is as follows: "ay4 +
: Ex B (to file B)
Then, "AP
2. Copy multiple rows in a to file B.
Mark mk at the beginning
Then use "ay 'K at the end (the cursor automatically returns to the beginning, and your content has been placed in the buffer)
A Indicates Buffer.
Then, use ex to escape to your file B, and then paste it with the "AP command ".

3. If you copy and paste your file multiple times, see the following:
A Indicates Buffer A. Can you continue to use buffer B?
There are so many buffers, A-Z and 1-9.
The buffer is likeWindowsThe same as the pan-cut version, there are many, you can copy what you want, and then paste it, but you just need to change a when you use "ay, if you want to copy 462 rows, use "ay, copy 536 rows with"

 

Source: http://www.cnblogs.com/lansh/archive/2010/08/19/1803378.html

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.