Vim
Text Editors
Programming
Command Line Tools
Coding Tips

How to copy to clipboard in Vim?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Copying text to the clipboard in Vim, the powerful text editor, can enhance your productivity and streamline your workflow. Whether you're using Vim in a Unix-like environment or on Windows, you can perform clipboard operations seamlessly once you understand Vim's capabilities and settings.

Understanding the Vim Clipboard

Vim treats the clipboard as just another register. In Vim, a register is a storage location where you can store text. To copy text to the clipboard, you use Vim's yank command, typically y, which places the selected text into a register. By default, yanked text goes into the unnamed register. However, to interact with the system clipboard, you can use the "+ or "* registers:

  • "+ refers to the system clipboard that interacts with other applications.
  • "* refers to the primary buffer (used mainly in X11/Linux environments).

Configuring Vim for Clipboard Access

Before you begin copying text to the clipboard, ensure your version of Vim includes clipboard support. Run the command:

bash
vim --version

Look for +clipboard or +xterm_clipboard in the output. If you see -clipboard or -xterm_clipboard, it means your version does not support clipboard operations, and you might need to install a version of Vim compiled with clipboard support, such as gvim or vim-gtk.

Basic Clipboard Operations

To copy text to the clipboard, you move to the desired text and use the yank command followed by the appropriate register. Here is how you do it:

  • Yank (copy) a line: "*yy or "+yy
  • Yank a word: position the cursor at the beginning of the word and type "*yw or "+yw
  • Yank from cursor to end of line: "*y$ or "+y$

Practical Examples

Here are more specific examples to illustrate the commands:

  1. Copy a whole line:
    • Move to the line you want to copy.
    • Type: "+yy
  2. Copy multiple lines (for example, 5 lines):
    • Move to the first line of the block you want to copy.
    • Type: "+y5y
  3. Copy specific text:
    • Enter visual mode by pressing v.
    • Use arrow keys or hjkl keys to select text.
    • After highlighting, type: "+y
  4. Using the Clipboard in Insert Mode:
    • You can also paste the clipboard contents directly into insert mode. Navigate to where you want to insert the clipboard contents and type: Ctrl-r +

Automated Clipboard Settings

For ease of use, you can configure Vim to automatically use the clipboard for all yank, delete, change, and put operations. Add the following to your .vimrc file:

vim
set clipboard=unnamedplus

This setting tells Vim to use the "+ register as the default for all operations, allowing seamless integration with the system clipboard without specifying the "+ every time.

Summary Table

CommandAction
"*yyYanks the current line into the primary X11 selection
"*ywYanks the current word into the primary X11 selection
"*y$Yanks from the cursor to the end of the line into the primary X11 selection
"+yyYanks the current line into the system clipboard
"+ywYanks the current word into the system clipboard
"+y$Yanks from the cursor to the end of the line into the system clipboard

Advanced Tips

  • Integrate Clipboard Usage in Macros: Since registers can be used in recorded macros, use the "+ register in your macros to interact directly with the system clipboard.
  • Preserve Clipboard Contents: To avoid replacing the clipboard content on each paste operation, use the :let command to save and restore the clipboard:
vim
  :let old_clipboard_contents = @+
  [perform operations]
  :let @+ = old_clipboard_contents

By understanding and effectively utilizing the clipboard in Vim, you increase flexibility in your editing and can easily integrate text movement between Vim and other applications.


Course illustration
Course illustration

All Rights Reserved.