Vim Copy and Paste Quest

Source: Internet
Author: User

Vim is one of the best text editors to use Vim to document, writing code is really a pleasant thing. Whenever you learn a new function of vim, it will greatly improve the efficiency. The use of vim for decades has not yet fully mastered the functionality of Vim, which also illustrates the power of vim. And this is not a good thing, as long as some study, there are improvements.

The recent use of vim to write a blog, found in vim after pasting Python code, indentation is completely chaotic. Careful study of the following, the original is automatically indented for the sake of, so do the following settings:

: Set Noai Nosi

Auto indent and Smart indent are canceled, so the paste will not go wrong. But in some vim does not work, or typesetting disorder.

Later found a better use of the settings:

: Set Paste

After entering paste mode, you can paste the content in insert mode without any distortion. This is really gray often use, can't help but look at the help, found that it did so many things:

    • TextWidth set to 0
    • Wrapmargin set to 0
    • Set Noai
    • Set Nosi
    • Softtabstop set to 0
    • Revins Reset
    • Ruler Reset
    • Showmatch Reset
    • Formatoptions using null values

The following option values are not changed, but are disabled:

    • Lisp
    • indentexpr
    • Cindent

No wonder before only set Noai and Nosi not, originally with so many factors related!

But this is still more troublesome, each time to paste, the first set paste, and then paste, and then set Nopaste. Is there any more convenient? You may have thought of it, using a keyboard mapping Yes, yes. We can set this::

: Map <F10>: Set paste<cr>:map <F11>: Set nopaste<cr>

So before pasting press F10 key to start paste mode, paste and press F11 cancel paste mode. In fact, paste has an option to toggle the paste switch, which is pastetoggle. It allows you to bind shortcut keys to activate/deactivate paste mode. Like what::

: Set pastetoggle=<f11>

This reduces the use of a shortcut key, which is more convenient.

But, is this the most convenient? Vimer The pursuit of efficiency is endless. Is there any better way to do it?

You may have thought of the VIM register. Yes, use the VIM register "+p paste." Do not consider whether automatic indentation, whether paste mode, direct text delivery! :

"+p

To say vim registers, from the vim file between the copy and paste speaking.

In vim, to copy the current line, press yy in normal mode and press p where you want to paste. This is why vim saved the copied content to its own register. If YY is executed elsewhere, the new content will overwrite the contents of the original register. What if you want to save the contents of the original register and add new content at the same time? This is the time to add tags before yy. The label starts with double quotes, followed by the label name, either the number 0-9, or 26 letters, and then the copy operation, which saves the copied content to the tag register. All register contents are displayed by the following command:

: Reg

Note Two special registers: "* and" +. These two registers are communicated with the system, the former relates the system selects the buffer, the latter relates the system Shearing board. They can be used to exchange data with other programs.

Note:

If there is no "* or" + Register in the Register list, it may be caused by the lack of a graphical interface to install VIM. Debian/ubuntu can be resolved by installing the vim-gnome.

$ sudo apt-get install Vim-gnome

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

Select buffer and Clipboard

Unlike the Windows,linux system, there are two shear plates: one is called the selection buffer (X11 selection) and the other is the shear plate (clipboard).

The selection buffer is real-time, and when you select content with the mouse or keyboard, the content already exists in the selection buffer, which is probably the origin of the selection buffer.

Use the following command to view the contents of the selection buffer:

$ xclip-out

If there is no Xclip command, Debian/ubuntu can be installed under the following command:

$ sudo apt-get install Xclip

You can use the middle mouse button or type Shift+insert to paste the contents of the selection buffer. However, for some GUI programs, such as Gedit, can only be called by the middle mouse button to select the contents of the buffer, the use of Shift+insert, the call is the content of the Clipboard.

The Clipboard is similar to the Clipboard in Windows, where the content is stored in the Clipboard after selecting the text, executing CTRL + C or selecting ' Copy ' in the menu.

Use the following command to view the contents of the Clipboard:

$ xclip-out-sel Clipboard

Instead, the contents of the Clipboard are CTRL + V. However, in some cases, such as gnome-terminal, can not directly use the CTRL+C,CTRL+V, this time will be replaced with Shift+ctrl+c,shift+ctrl+v.

Paste in original format

OK, here's how to choose a buffer and a clipboard, here's the perfect solution for preserving formatting paste:

    • Programme one:
    1. Select text content
    2. Vim Normal mode press *p to paste the contents of the selection buffer in
    • Scenario Two:
    1. Copy file contents
    2. Vim Normal Mode Press "+ p to paste the Clipboard contents in

At this point, if the content to be copied is also the content in the Vim editor, how is it more convenient to copy?

Replication in Vim

Vim has a visual mode that allows you to select regions in this mode. You can type V into visual mode in normal mode, or you can personalize it by typing V into the line visual mode, or by typing CTRL + v. into the column visual mode. You can then move the cursor to select the content. Note that the selection is saved in real time in the selection buffer, and of course you can also type "+y to save this content to the Clipboard, or" Ay "to save the content to a register labeled a. But you know, only the first two of the content can be used in other programs, and the contents of a register can only be used within the VIM editor.

You can also copy them with the mouse. Here you first turn on mouse mode. :

: Set Mouse=a

This allows you to copy to the selection buffer directly using the mouse selection area in normal mode. However, this situation cannot be copied to the Clipboard.

To copy content to the clipboard using your mouse, you need to set the following:

: Set Mouse=v

In this case, in addition to being able to copy to the selection buffer directly using the mouse selection area as above, you can also select "Copy" in the context menu to save to the Clipboard. But the new problem came out again. If the line number is displayed, the line number is also selected. You will think, this is good, if you do not need a line number, before copying, first execute the set Nonu to suppress the line number display chant.

In fact, there is no need to do so, if you do not need to copy the line number, use in the visual mode with the keyboard to choose it?

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

So, finally we got the perfect scheme for copying and pasting vim files, the transfer of files using the selection buffer.

The perfect solution for copying and pasting between vim files
    1. In ~/.VIMRC, add the following line:

      Set Mouse=v
    2. Copies the content to the selection buffer.

      • With line numbers, use the mouse to select the content area.
      • Do not line numbers, use "*yny" to copy n rows or select in Visual mode.
    3. Paste the contents of the selection buffer into the vim file: press *p in normal mode.

Add:

Set the default use of the select buffer Register in VIM "*:

Set clipboard = Unnamed

The data can be exchanged directly through the y,p and the system selection buffer.

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

Vim Copy and Paste Quest

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.