Friday, February 25, 2011

Vim Usage: using clipboard

Vim is a very strong and powerful text processing software. People in Linux should hear this name although they may not know how to use it. However, when people is using vim, they may face a problem that is easy and common. But it is a strange question also, this is about clipboard of vim.

Let's do a simple experiment. Launch a browser and copy the text from a web page. Then, in vim, use "p" to paste the content as usual. Huh...you discover, there is nothing pasted here. Hoho...the reason is vim contains multiple clipboards and they are independent to system clipboard. Thus, when you copy the text from browser, it will save the content in the system clipboard. When you use "p" to paste the content in vim, vim is reading vim's clipboard.


Then, how to easy to copy and paste the content from system clipboard? Simple, you can use short-cut command "shift - insert" to achieve it although you need to move your hand far away. If you are using GVim, you can also use middle button. Okay, it is simple and now you learn the basic of vim clipboard.   

As you said before, vim contains multiple clipboards. The objective for this is for user to do some complicated text processing. Now, let's talk about ":reg". When you type this command in command mode, you can see a list of output which is familiar to you. Right, they are the content you copied. You can see that vim contains many clipboard and it is indexed by characters, numbers and symbols. All the clipboard's name starts with '"'. Wow~ can you see the content indexed by '"+'. This store the content that you have just copied from browser. You are right. '"+' is System clipboard.

At this time, you should be able to view content of all clipboards. Now, we are going to paste content from specific clipboard. We all know that "p" is the command for pasting. But if we need to paste from certain clipboard, "p" is not enough. To specify a clipboard, we need to tell vim explicitly using '"[clipboard index]'. For example, we want to paste the content from system clipboard, we can press '"+p' in command mode that means change to '"+' clipboard (system clipboard) and then paste. Now, you can see you pasted the content from the web page you copied.

Now, you know how to paste content from certain clipboard. Then how to copy content to certain clipboard, for smart user, I think you know the answer now. The step is "1) specify a clipboard" and then "2) copy the content". You know how to specify the clipboard. To copy content in vim, use "y". For example, you want to copy the content to system clipboard so that you can paste the content to other application. First, use '"+yy' to change the clipboard to system clipboard and then copy the line (for more usage of yank[y], please read in the help ":help y"). Then you can paste the content in other application or you can check if it is success by ":reg" command.

Remarks: Some people tell me that it cannot be used. You need to check whether your vim compiled with clipboard support. To check it, use command in terminal
$ vim --version
If you can find +clipboard or +xterm_clipboard, you can use it properly. Otherwise, you need to compile it again with clipboard support. In ubuntu, you can use
$ sudo apt-get install vim-gnome
to obtain a vim that support these two functions

No comments:

Post a Comment