Vim copy and paste

Source: Internet
Author: User

Vim, as one of the best text editors, uses Vim to compile documents and writeCodeIt's really pleasant. Every time you learn a new feature of VIM, it will greatly improve productivity. Some people have been using Vim for decades and have not fully mastered the functions of VIM, which also shows the power of vim. But this is not a good thing. As long as you have some learning, you will have some improvement.

I recently used Vim to write a blog and found that after I pasted Python code in Vim, all the indentation became messy. After carefully studying the following, it turns out to be auto indent, so we made the following settings: 

 
: Set noai nosi

The automatic indentation and smart indentation are removed, so that the paste will not be wrong. But in some vim, the layout is still disordered.

Later, we found more useful settings:

 
: Set Paste

After entering paste mode, you can paste the content in insert mode without any deformation. This is really cool and easy to use. I can't help but read the help and find that it has done so many things:

    • Set textwidth to 0.
    • Wrapmargin is set to 0.
    • Set noai
    • Set nosi
    • Set softtabstop to 0.
    • Revins Reset
    • Ruler Reset
    • Reset showmatch
    • Use a null value for formatoptions

The following options remain unchanged, but are disabled:

    • LISP
    • Indentexpr
    • Cindent

No wonder we can't set noai and nosi. It was originally related to so many factors!

However, this is quite troublesome. If you want to paste it, set paste first, paste it, and then set nopaste. Is it more convenient? You may have thought of using keyboard ing, right. We can set this as follows ::

 
: Map <F10>: Set paste <CR>: Map <F11>: Set nopaste <CR>

In this way, press F10 to start the paste mode, and press F11 to cancel the paste mode. In fact, paste has an option to switch the paste switch, which is pastetoggle. You can bind a shortcut key to activate or cancel the paste mode. For example ::

 
: Set pastetoggle = <F11>

This reduces the usage of a shortcut key and makes it easier to use.

But is this the most convenient? Vimer's pursuit of efficiency is endless. Are there other good methods?

You may have thought of the vim register. Yes, use the vim register "+ P to paste it. You don't have to worry about automatic indent or paste mode. You can directly pass the original text! :

 
"+ P

To talk about the vim register, we should start with copying and pasting between Vim files.

In vim, to copy the current row, press YY in normal mode and P in the place to be pasted. This is why Vim saves the copied content to its own register. If YY is executed elsewhere, the new content will overwrite the content in the original register. What if I want to save the content in the original register and add new content at the same time? In this case, we need to add a label before yy. The label starts with double quotation marks, followed by the tag name, which can be a number ranging from 0 to 9, or 26 letters, followed by a copy operation, in this way, the copied content is saved to the label register. Run the following command to display all registers ::

 
: Reg

Note two special registers: "* and" +. These two registers are connected to the system. The former is associated with the System Selection buffer, and the latter is associated with the system clipboard. They can be used with otherProgramExchange data.

Note:

If the register list contains no "* or" + registers, it may be caused by the absence of the vim GUI. You can install vim-gnome In Debian/ubuntu.

$ Sudo apt-Get install vim-gnome

What is the difference between a buffer zone and a system clipboard? Let's continue our research.

Select the buffer and clipboard

Different from windows, there are two clipboard boards in Linux: one is called the selection buffer (X11 selection buffer), and the other is the clipboard (Clipboard ).

The selection of the buffer zone is real-time. When you use the mouse or keyboard to select the content, the content already exists in the selection buffer zone. This may be the way to select the buffer zone.

Run the following command to view the content of the selected Buffer ::

 
$ Xclip-out

If the xclip command is not available, run the following command to install Debian/Ubuntu ::

 
$ Sudo apt-Get install xclip

You can use the mouse to paste the content of the selected buffer by typing SHIFT + insert. However, for some gui programs, such as gedit, the content of the selection buffer can only be called by the middle mouse. If SHIFT + insert is used, the content of the clipboard is called.

The clipboard is similar to the clipboard in windows. After you select the text content, press Ctrl + C or select 'copy' in the menu to store the content in the clipboard.

Run the following command to view the clipboard content ::

 
$ Xclip-out-Sel clipboard

The content of the clipboard is Ctrl + v. However, in some cases, for example, gnome-terminal, you cannot directly use Ctrl + C, CTRL + V. In this case, Shift + Ctrl + C, Shift + Ctrl + V should be used instead.

Original format Paste

Now that you know how to select the buffer zone and clipboard, the following is a perfect solution for retaining format paste:

    • Solution 1:
    1. Select text content
    2. In Vim normal mode, press "* P" to paste the content in the selection buffer.
    • Solution 2:
    1. Copy File Content
    2. In Vim normal mode, press "+ P" to paste the clipboard content.

At this time, if the content to be copied is also the content in the vim Editor, how can we copy it more conveniently?

Copy in VIM

Vim has a visual mode in which you can select a region. You can type V in normal mode to enter the visual mode, or you can personalize it. Type V to enter the line visual mode, or press Ctrl + V to enter the column visual mode. Move the cursor to select the content. Note that the selected content has been saved in the selection buffer in real time. You can also type "+ Y" to save the content to the clipboard, or "ay" saves the content to the Register labeled as. But you must know that only the content in the first two can be used in other programs, and the content in register a can only be used in the vim editor.

You can also copy it with the mouse. The mouse mode must be enabled first. :

 
: Set mouse =

In normal mode, you can copy the selected area to the selected buffer zone with the mouse. In this case, the clipboard cannot be copied.

To copy the content to the clipboard with the mouse, you need to make the following settings ::

 
: Set mouse = V

In this case, in addition to copying the selected area to the selected buffer area, you can also choose copy from the context menu to save it to the clipboard. However, new problems have emerged. If the row number is displayed, the row number is also selected. You will think, this is easy to do. If you do not need a row number, execute set Nonu before copying to cancel the row number display.

In fact, this is not necessary. If you do not need to copy the row number, you can use the keyboard to select it in visual mode?

In addition, from the above discussion, it is not difficult to conclude that using the selected buffer zone is much more convenient than using the clipboard, which can save a lot of steps.

Therefore, we finally got the perfect solution for copying and pasting Vim files, and used the buffer zone for transferring files.

Perfect solution for copying and pasting Vim files
    1. In ~ /. Add the following line to vimrc ::

      Set mouse = V
    2. Copy the content to the selection buffer.

      • When a line number is included, use the mouse to select the content area.
      • Do not use line numbers. Use "* yny to copy n rows or select in visual mode.
    3. Paste the content in the selection buffer to the vim file: In normal mode, press "* P.

Supplement:

Set the default buffer register "*:

 
Set clipboard = unnamed

Then you can exchange data directly through Y, P and the System Selection buffer.

(Original address:
Http://blog.ossxp.com/2010/12/2190)

Related Article

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.