In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to achieve copy and paste in Vim. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Recently, I used Vim to write a blog and found that after pasting Python code in Vim, the indentation was all messed up. After studying the following carefully, it turns out that it is because of automatic indentation, so make the following settings:
Set noai nosi eliminates automatic indentation and smart indentation so that pasting doesn't make mistakes. But not in some vim, or typesetting disorder.
Later, a better setting was found:
After set paste enters paste mode, you can paste the content in insert mode without any distortion. This is so easy to use that I can't help but look at the help and find that it has done so much:
Textwidth is set to 0
Wrapmargin is set to 0
Set noai
Set nosi
Softtabstop is set to 0
Revins reset
Ruler reset
Showmatch reset
Formatoptions uses null values
The following option values remain unchanged, but are disabled:
Lisp
Indentexpr
Cindent
No wonder only noai and nosi can't be set up before, it has to do with so many factors!
But this is still quite troublesome, every time to paste, first set paste, and then paste, and then set nopaste. Do you have anything more convenient? You may have thought of using keyboard mapping, yes. We can set it as follows:
: map: set paste
Map: set nopaste so press F10 before pasting to start paste mode, and press F11 to cancel paste mode after pasting. In fact, paste has an option to toggle the paste switch, which is pastetoggle. It allows you to bind shortcut keys to activate / disable paste mode. For example:
Set pastetoggle= reduces the use of a shortcut key and is more convenient to use.
But is this the most convenient? Vimer's pursuit of efficiency is never-ending. Is there any better way?
You may have thought of it, the vim register. Yes, use the vim register "+ p" to paste. There is no need to consider whether it is automatically indented, whether paste mode, direct text transfer!
"+ p if you want to talk about vim registers, you should start with copy and paste between vim files.
In Vim, to copy the current line, press yy in normal mode and p where you want to paste. This is because vim saves the copy 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 while adding new ones? At this point, it's time to add tags before yy. The tag starts with double quotes, followed by the tag name, which can be the number 0-9 or 26 letters, and then the copy operation, which saves the copy to the tag register. Display the contents of all registers with the following command:
Reg pays attention to two special registers: "*" and "+". The two registers are connected to the system, the former associated with the system selection buffer and the latter with the system clipboard. Through them, you can exchange data with other programs.
Note:
If there is no "* or" + register in the register list, it may be due to the lack of a graphical interface for installing vim. It can be solved by installing vim-gnome under Debian/Ubuntu.
What is the difference between the $sudo apt-get install vim-gnome selection buffer and the system clipboard? Let's get on with our research.
Select buffer and clipboard
Unlike the Windows,Linux system, there are two clipboards: one is called the selection buffer (X11 selection buffer), and the other is the clipboard (clipboard).
The selection buffer is real-time, and when you use the mouse or keyboard to select content, the content already exists in the selection buffer, which may be the origin of the selection buffer.
Use the following command to view the contents of the selection buffer:
If there is no xclip command for $xclip-out, you can install it under Debian/Ubuntu with the following command:
Sudo apt-get install xclip can use the middle mouse button or type Shift+Insert to paste the contents of the selection buffer. But for some GUI programs, such as gedit, you can only call the contents of the selection buffer through the middle mouse button, and if you use Shift+Insert, you are calling the contents of the clipboard.
The clipboard is similar to Windows's clipboard. After selecting the text, execute Ctrl + c or select "copy" from the menu, then the content is stored in the clipboard.
Use the following command to view the contents of the clipboard:
$xclip-out-sel clipboard and the contents of the clipboard are Ctrl+v. But in some cases, such as gnome-terminal, you can't use Ctrl+c,Ctrl+v directly, so use Shift+Ctrl+c,Shift+Ctrl+v instead.
Paste in original format
Now that you've learned about the selection buffer and clipboard, here's the perfect solution for preserving format pasting:
Option 1:
Select text content
Press "* p" to paste the contents of the selection buffer in vim normal mode.
Option 2:
Copy the contents of the file
Press "+ p" to paste the clipboard contents in vim normal mode
At this point, if the content you want to copy is also in the vim editor, how can it be more convenient to copy?
Replication in vim
Vim has a visual mode (Visual Mode) in which you can select regions. You can type v in normal mode to enter visual mode, or you can personalize it a bit, type V to enter row visual mode, or type Ctrl+v to enter column visual mode. At this point, move the cursor to select content. Note that the selection has been saved in real time in the selection buffer, of course, you can also type "+ y" to save this content to the clipboard, or "ay to save the content to the register labeled a. You should know, however, that only the contents of the first two can be used in other programs, while the contents of the a register can only be used within the vim editor.
It can also be copied with the mouse. The first thing to do here is to turn on mouse mode. :
Set mouse=a so that you can directly use the mouse to copy the selection area to the selection buffer in normal mode. However, it cannot be copied to the clipboard in this case.
To copy content to the clipboard using the mouse, you need to make the following settings:
: set mouse=v in this case, in addition to directly copying the mouse selection area to the selection buffer as above, you can also select "copy" from the right-click menu to save to the clipboard. But a new problem arises. If the line number is displayed, the line number will also be selected. You will think, this is easy to do, if you do not need a line number, execute set nonu before copying to cancel the line number display.
In fact, this is not necessary, if you do not need to copy the line number, can you just use the keyboard to select it in visual mode?
And, from the above discussion, it is not difficult to see that using the selection buffer is much more convenient than using the clipboard and can save a lot of steps.
So, in the end, we got the perfect solution for copying and pasting between vim files, and the transfer of files uses a selection buffer.
Perfect solution of copying and pasting between vim files
Add the following line to ~ / .vimrc:
Set mouse=v copies the contents to the selection buffer.
When with a line number, use the mouse to select the content area.
Instead of line numbers, use "* yny to copy n lines or select in visual mode."
Paste the contents of the selection buffer into the vim file: press "* p" in normal mode.
Add:
Set the selection buffer register to be used by default in vim "*:
Set clipboard = unnamed can exchange data directly through yjournal p and system selection buffer.
On "how to copy and paste Vim" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.