A problem analysis of vim sticky post (pastetoggle)

Source: Internet
Author: User

This digest is from the network, the initial author is not clear, so no original link is left. If anyone knows, please tell them.

Vim as one of the best text editors, using vim to write documents, writing code is very pleasant things. Whenever you learn a new function of VIM, you will greatly improve your work efficiency. Some people use vim for decades, not fully mastering the function of Vim, which also shows the strength of vim. And this is not a good thing, as long as some learning, some improve.

Recently using Vim to write a blog, found in vim paste Python code, indentation is completely messed up. Carefully studied the following, the original is automatic indentation, so do the following settings:

: Set Noai Nosi

The automatic indent and smart indent are canceled so that the paste is not wrong. But in some vim is not good, or typesetting disorder.

Later found the better use of the settings:

: Set Paste

After entering paste mode, you can paste content in insert mode without any distortion. This is really ash often easy to use, can't help but look at the help, found that it did so much: TextWidth set to 0 Wrapmargin set to 0 set Noai sets Nosi Softtabstop set to 0 revins reset ruler reset Showmatch reset formatoptions Use null value

The following option values are unchanged, but are disabled: Lisp indentexpr cindent

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

But this is still relatively troublesome, each time to paste words, set paste, and then paste, and then set Nopaste. There is no more convenient to it. You might think, using a keyboard map, yes. We can set this::

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

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

: Set pastetoggle=<f11>

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

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

You might think of the vim register. Yes, use the VIM register "+p paste." There is no need to consider whether automatic indentation, whether paste mode, direct text transmission. :

"+p

To say vim registers, you need to copy and paste from the vim file.

In vim, to copy the current line, press yy in normal mode and press p where you want to paste. This is the reason vim saves the copied content to its own register. If YY is executed elsewhere, the new content will overwrite the contents of the original register. If you want to save the contents of the original register and add new content at the same time. At this point, we will add the label before YY. The label starts with a double quotation mark, followed by the label name, either the number 0-9 or 26 letters, then the copy operation, which saves the copy to the label register. Display all register contents by following command::

: Reg

Note Two special registers: "* and" +. The two registers are connected with the system, the former is related to the system selection buffer, the latter is associated with the system shearing board. They allow you to exchange data with other programs. Note:

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

$ sudo apt-get install Vim-gnome

Select the difference between the buffer and the system Clipboard. Let's continue to study. Select buffers and clipping boards

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

Select buffer is real-time, when the mouse or keyboard to select content, content already exists in the selection buffer, which may be the origin of the choice 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 with the following command::

$ sudo apt-get install Xclip

You can paste the contents of the selection buffer by using the middle mouse button or by typing Shift+insert. However, for some GUI programs, such as Gedit, you can only invoke the contents of the buffer by the middle mouse button, using Shift+insert, the contents of the Clipboard are called.

The Clipboard is similar to the Windows clipboard, and the contents are stored in the Clipboard after you select the text and then perform CTRL + C or select Copy in the menu.

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

$ xclip-out-sel Clipboard

The contents of the Clipboard, however, are CTRL + V. But in some cases, such as gnome-terminal, can not directly use ctrl+c,ctrl+v, this time to use SHIFT+CTRL+C,SHIFT+CTRL+V instead. paste in original format

Okay, with the selection of buffers and the Clipboard, here's the perfect solution for preserving formatting: Choose text content in vim normal mode press *p to paste the contents of the selection buffer in Scenario two: Copy file content in vim normal mode press + p to paste the Clipboard contents in

Then, if the content to be copied is also in the Vim editor, then how to copy it is more convenient. replication in Vim

Vim has a visual pattern (visual mode) in which you can select regions. You can type V into visual mode in normal mode, or personalize it, type V into row visual mode, or type CTRL + + into column visual mode. Then move the cursor to select the content. Note that the selection has been saved to the selection buffer in real time, but you can also type "+y to save this content to the Clipboard, or" Ay the content to a register labeled a. But know that only the first two of the contents can be used in other programs, and the contents of a registers can only be used within the VIM editor.

You can also copy them by using the mouse. The first thing to do here is to turn on mouse mode. :

: Set Mouse=a

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

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

: Set Mouse=v

In this case, you can also select Copy from the right-click menu to save to the Clipboard, except that you can copy it directly to the selection buffer, just as above. 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 to do, if you do not need line number, before copying, the first execution set Nonu to suppress line number display Bai.

In fact, it is not necessary, if you do not need to copy the line number, use the keyboard in visual mode to choose not to do.

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

So, in the end, we got the perfect solution to copy and paste between vim files, the transfer of file transfers using the Select buffer. vim copy and paste between files perfect solution

Add the following line to the ~/.VIMRC:

Set Mouse=v

Copy the content to the select buffer. With line numbers, use the mouse to select the content area. Do not line numbers, use the *yny copy n rows or Visual mode to select.

Paste the contents of the selection buffer into the vim file: In normal mode, press *p.

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.