How to Copy Text in Vim (to the Clipboard)

Copy Text in Vim (to the Clipboard)

by | Jan 15, 2024 | Useful Tech

Looking to get text out of Vim (on the command line) and into another app? Find out here how to copy text from Vim to the system clipboard on Mac, Linux, and Windows.

First, Note the Consistent User Interface

Note the consistent user interface and error reportage,” writes Patrick J. LoPresti on a presumably hot July day in 1992 about the Unix line editor ed and a typical first session with it:

golem$ ed

?
help
?
?
?
quit
?
exit
?
bye
?
hello?
?
eat flaming death
?
^C
?
^C
?
^D
?

Today, of course, the same joke is made about Vim (which, by way of vi is a descendent relative of ed).

Presuming you know how to exit Vim, let’s try to get text out of it instead of ourselves. (The command below to copy lines using a command line tool works just the same in ed, the standard text editor.)

How to Copy Text in Vim (to the Clipboard)

Using the Clipboard Register

Time needed: 1 minute

To copy text from Vim to the system-wide clipboard on Mac, Windows, and Linux:

  1. Press V for visual mode.

    “V” doesn’t enter visual mode: Press Esc first to make sure you exit insert mode in Vim and are in normal mode.
    No need to be visual: You need not use visual mode for copying to the clipboard in Vim, of course; all other ways of specifying what you want to copy/yank work as well (say, y3w for copying three words).

  2. Use movement keys to highlight just the text you mean to copy.

    Vim shortcuts: Press J for down, K for up, H for left, L for right, W to jump a word forward, B to jump back word-wise, Shift G for the last line, GG for the first line, etc.

  3. Now type "*y.

    Here’s why: Vim has multiple internal clipboards (called registers), some of them special. To access any clipboard, you prefix the register with " (quotation mark), and the system clipboard has the special name * (star). To copy text to a register, you finally use y (for “yank”).
    No * register: Note that the special * register only exists if Vim has been compiled with clipboard support. To check your version, run :version in normal mode and look for +clipbard (enabled) or -clipboard (disabled).
    Copy text to the system clipboard using the Vim clipboard register

Also: How to Paste Text in Vim (from the Clipboard)

Using a Command Line Tool

If copying using Vim’s register for the system clipboard fails, you can restart to sending the text to a command-line tool:

  1. Press Shift V for visual line mode.
    “Shift V” doesn’t enter visual mode: Press Esc first to make sure you are in normal mode.
    Doesn’t have to be visual: You need not use visual mode for copying in Vim, of course (see below); for an approachable way (if maybe not be fastest) to copy text in Vim and Neovim, visual mode shall serve the purpose here, though.
    Always lines: Note that Vim lets you copy only entire lines; while you can use visual mode to highlight part of a line, Vim will still send the entire line of text to the clipboard. See below for copying part of a line.
  2. Use motion keys to highlight just the lines you want to copy.
  3. Type :.
  4. Now type the command for your operating system to copy text from Vim to the clipboard following :'<,'> already entered for you.
    macOS: Append :w !pbcopy to the command line (using pbcopy to copy from Terminal to the clipboard).
    Windows: Type :w !clip.
    Linux: Write :w !xclip -selection clipboard.
    Example: In Mac Terminal, the Vim command line will read '<,'> :w !pbcopy.
  5. Press Enter.
    Copy text from Vim to the Mac clipboard

How to Copy All Text in Vim and Neovim

To copy all text in a file with Vim or Neovim:

  1. Exit insert mode, if needed, for normal mode.
  2. Press : for command mode.
  3. Type %.
    Here’s why: This applies the following command to the whole document.
    Alternative: You can also type 0,$, which selects the lines starting with and including the first line (0) up to and also including the last line ($).
  4. Now type the desired copying command.
    To the clipboard: For the system clipboard, append "*y, of course.
    To a named register: Use "ay to copy to the a Vim or Neovim register, for example.
    Example: :%"*y will copy the full buffer to the system clipboard.
  5. press Enter.

Using Visual Mode

To copy all with Vim or Neovim visual mode:

  1. Press G G (the G key two times) to go to the first line.
  2. Now press Shift V in normal mode to enter visual mode.
  3. Press Shift G now to jump to the last line.
  4. Type : followed by the desired copying command.
    Example: :'<,'>"By will copy all to the B register (while '<,'> stands for the extent of the visual selection, which is the whole file here).

How to Copy Text in Vim (and to the Clipboard): FAQ

Can I copy from Vim without visual mode?

Yes.

You can use any selection command that lets you yank to the * register to copy to the clipboard.

Example: Use :%y * to copy the whole document to the clipboard.

Alternatively, to copy lines from Vim to the system clipboard without highlighting them first using a command-line tool:

  1. Press : (colon) for the command line.
  2. Type first_line,last_linew ! followed by the operating system’s copying command (see above).
    Lines: Replace first_line with the number of the first line to copy, last_line with the last line; (set up Vim to show line numbers to identify them, if only temporarily).
    Last line: You can use $ for the last line.
    All lines: % stands for the whole document; see above;
    Example: Make the command read :3,7w !clip to copy lines 3 to 7 on the Windows command line.
    Whole document: Use :w !xclip -selection clipboard to copy the entire file, for instance, on Linux.

How can I copy part of a line if the clipboard register does not work?

To copy part of a line if the clipboard register fails to copy to the system’s clipboard, it is usually easiest (and hence best) to rely on the Terminal’s copying command:

  1. Highlight the text you want to copy using the mouse.
    Within a line: It is best to highlight only part of a line or extraneous characters (such as line numbers) will be copied as well and make using the text more difficult.
  2. Press Ctrl C or Command C to copy or right-click and select Copy from the context menu.

How can I copy in graphical Vim?

In Vim using the operating system’s graphical interface (instead of the command line), you can highlight text and directly copy using the menu (Edit | Copy) or, though not universally, the standard copying command (Ctrl C or Command C, typically) or use the system clipboard register as above.

(How to copy text to the clipboard tested with Vim 9.0 and Neovim 0.9.5 on macOS Sonoma 14.2 and Ventura 13.4, Windows 11 Version 22H2 and Ubuntu 22; updated January 2024)

Home » Useful Tech » How to Copy Text in Vim (to the Clipboard)