Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

vim - How to copy/paste text from vi to different applications

Is it possible to copy/paste text without using :vs? If I have two vi windows open, I can copy/paste text with a mouse. How can I do it with a keyboard?

I found two existing questions that are similar to this, but neither one answers my question.
how to copy codes in vi to clipboard
Copy and paste content from one file to another file in VI

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm sure there are many ways, but I do it using marks and registers.

Marks

You can place a mark anywhere in a file using m followed by the name of the mark you want to use.

You can use any letter between a and z (capital and lowercase) to name your marks.

You can go to the line that contains a mark with the ' key.

For example, mx marks a line with mark x and 'x moves the cursor to the line containing mark x.

You can go to the exact location of a mark using the backtick key: `

To yank from the current cursor location to the line containing mark x, for example, you would enter y'x

Registers

In order to use the clipboard, you need to use registers, which represent places you can store the text you yank.

Just like you can use different marks for each character, you can name the registers you yank text to.

You refer to a register by using the " key when yanking/putting.

For example "ay'x would yank the text between the cursor and the line containing x to register a.

The clipboard is represented by a special register: either * or + depending on your environment.

To yank the text between the cursor and the line containing mark x to the clipboard, enter the following: "+y'x

This says: use buffer + (the clipboard) to store the text between the cursor and the line containing mark x.

Once you do this, your text will be in the clipboard. You can use CONTROL-V to paste it into other apps.

NOTE: In some environments, the clipboard is represented by the buffer named *.


This may sound overwhelming, but once you get used to it, it's VERY powerful.

I use this hundreds of times every day.

If you're editing a file that has several key points of interest, you can mark each part of the file with different marks and quickly move your cursor between the code you need to edit.

Likewise, if you have several pieces of text that you need to repeatedly copy, you can store each one in a different register to make your pasting more efficient.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...