Adam Rubin
Adam Rubin

Reputation: 847

Vim "yank" does not seem to work

I'm fairly new to Vim. Tonight, I learned about the "yank" command, but when I try to use it in MacVim, it doesn't do anything. Neither Y nor y{motion} do anything. I tried with a default .vimrc to rule out any weird config issues.

Google-fu is failing me. This feels like a noobie issue. Am I missing something obvious?

Upvotes: 34

Views: 27388

Answers (6)

younsl
younsl

Reputation: 1

Clipboard integration in vim needs to be configured differently depending on the OS. You can add the following setting to your .vimrc file for effective clipboard sharing:

# ~/.vimrc
if has('mac')
  set clipboard=unnamed
elseif has('unix')
  set clipboard=unnamedplus
endif

After applying these settings and launching Vim, you can verify your current clipboard configuration by running the following command in Vim:

:set clipboard?

For macOS users, given the conditional structure in the vim configuration, the command should return clipboard=unnamed.

This configuration uses Vim's built-in has() function to detect the OS. It sets unnamed for macOS, unnamedplus for Unix/Linux systems, and defaults to unnamed for other systems. The has() function detects the OS quickly and accurately without executing external commands, making it superior to using the system() function in terms of performance and portability.

It's important to note that setting clipboard to unnamedplus on macOS Vim can cause basic copy/paste commands like yy or p to malfunction. This is because macOS doesn't support the + register. Therefore, it's crucial to use unnamed on macOS.

With this setting, Vim's registers are linked to the system clipboard. This means that copy and delete operations in Vim are automatically reflected in the system clipboard, and you can directly paste the system clipboard contents in Vim. This greatly simplifies text exchange between Vim and other applications.

Upvotes: 0

Weston Ganger
Weston Ganger

Reputation: 6732

If you have the setting set clipboard=unnamedplus in your .vimrc then this will not be working.

For OSX you have to use set clipboard=unnamed

For Linux you will probably need to use set clipboard=unnamedplus

Heres the snippet from my personal .vimrc

if system('uname -s') == "Darwin\n"
  set clipboard=unnamed "OSX
else
  set clipboard=unnamedplus "Linux
endif

Upvotes: 61

rod howard
rod howard

Reputation: 151

If your using Ubuntu or Mint the only solution that seemed to work for me was to uninstall vim and install the package "vim-gnome" instead. Then adding the line:

set clipboard=unnamedplus

to my .vimrc worked as expected.

Upvotes: 1

icedwater
icedwater

Reputation: 4887

yank by itself merely copies the line into a clipboard - you will need to paste it onto the next line or onto the Preceding one to use the copied line. To cut the line as well, use delete.

Upvotes: 28

D.Shawley
D.Shawley

Reputation: 59623

The yank command pulls text into a clipboard. For example yy simply yanks the current line into the common clipboard. You can "paste" the contents of the clipboard with p. You can also yank into named buffers using something like "ayw to yank the text from the current position to the end of the word into a buffer named a. The correspond put is "ap.

Upvotes: 4

Amadan
Amadan

Reputation: 198476

It does not do anything visible - just like Ctrl-C (Edit/Copy) in other editors. Try the command p (paste) after it - that's the equivalent of Ctrl-V - to put what was yanked into the document.

Upvotes: 5

Related Questions