Copying and pasting with Vim and YankRing

Yank and Pull

Article from Issue 167/2014
Author(s):

With the YankRing plugin, Vim's yank and pull features become even more powerful.

Bruce Byfield

Bruce Byfield is a computer journalist and a freelance writer and editor specializing in free and open source software. In addition to his writing projects, he also teaches live and e-learning courses. In his spare time, Bruce writes about Northwest coast art. You can read more of his work at http://brucebyfield.wordpress.com

You can tell that Vim is an old text editor by its use of "yank" for "copy" and "pull" for "paste." However, never let the obsolete terminology fool you – in many ways, Vim's yank and pull are still more sophisticated than anything you can find on most modern desktops. Add the YankRing plugin [1], and Vim's yank and pull capacities become even more powerful.

For some users, unenhanced Vim may be enough for their needs. Anything pulled or deleted is stored, the most recent first, in a register – Vim's name for the buffer that serves as a clipboard. A register can contain up to nine items, which you can view with the command :reg or :register. If one register is not enough, you can have up to 26, each identified by a single lowercase letter.

A register is specified by prefacing a command with its name, so that ayw copies a word and puts it in register a.

Pressing the Esc key and entering the command yw copies the word immediately after the cursor to the default register; similarly, yy copies the line after the cursor, and dd deletes and copies the current line. To yank or delete multiple words or lines, preface a command with the number of words or lines. For example, 2yw copies the next two words after the cursor. The only feedback that any of these command returns is a brief summary of the number of lines affected when multiple lines are involved.

In much the same way, the command  p pastes the latest item in the register after the cursor, and P pastes it before the cursor. To pull another of the eight items in the register, specify its number before the command; for example, 3P pastes the third latest item in the register before the cursor. Unless you have a perfect memory, you will probably need :reg or :register to see a list of items currently in all the registers (Figure 1).

Figure 1: In default Vim, the register serves as a clipboard for yanking and pulling (copying and pasting).

In all these circumstances, the default is the first register. However, you can use another register by prefixing the command with another register's name, so that bp pulls a command from register b.

In visual mode, you can highlight a block of text by indicating its starting point with a v and its endpoint with a V. You can then yank or pull the highlighted block by using the same commands that you would when selecting text by cursor position.

Even more elaborately, in visual mode [2], you can add a marker anywhere in your text using m plus a lowercase letter. Once the marker is created, you can return to it by typing its name. More to the point, you can highlight a text block by referring to two markers – for example, the command 'b'c highlights all text between marker b and marker c, which can then be yanked or pulled like any other highlighted block. You can also do the same thing with the current mouse position and a single marker, so that y'a highlights everything between the cursor and marker a.

Installing and Configuring YankRing

YankRing is a plugin that adds to, enhances, and simplifies Vim's default yank and pull features. By analogy to an Emacs feature called the kill ring, it refers to its register as a yankring. Despite the different names, the analogies between unenhanced Vim and YankRing are generally obvious.

YankRing's backward compatibility is strong. For example, although you can still use additional registers, given that the yankring by default holds 100 elements to default Vim's nine, you are unlikely to need them. Similarly, you can use the same commands as in unenhanced Vim, although you will probably prefer to use YankRing's own commands. You can even run YankRing with or without its visual window.

If you are using Vim without a plugin manager, the easiest way to install YankRing is to install Git then run the command:

git clone git://github.com/vim-scripts/YankRing.vim ~/.vim/plugins

By contrast, if you are using the Pathogen plugin manager, run

git clone git://github.com/vim-scripts/YankRing.vim ~/.vim/bundle/yankring

Both these commands copy the files in the repository for YankRing to your hard drive, making them immediately ready for use (Figure 2).

Figure 2: Copying YankRing files.

For many users, YankRing's defaults are all they will need. However, in the latest release (version 17), you can add 28 different fields [3] to change YankRing's behavior, many of which set the appearance of the yankring window. To edit these fields, use the following structure:

let g: [FIELD] = [SETTING]

Many of these fields set the appearance of the window in which the yankring displays its contents, but others are displayed in Table 1. Those with a default of 1 (except the element length) are toggles that you can turn off by changing the setting to 0.

Table 1

YankRing Fields

Field

Default

Description

yankring_max_history

100

The number of windows displayed in the YankRing window

yankring_min_element_length

2

The minimum length of an element. Prevents single-letter deletions from cluttering up the window

yankring_max_element_length

1

The maximum length (MB) of an element

yankring_enabled

1

YankRing is enabled when Vim starts

yankring_persist

1

Saves YankRing contents between sessions

yankring_ignore_duplicate

1

Does not store duplicate elements

yankring_window_auto_close

1

Closes the window automatically when an element is selected

Using the YankRing Window

After installation, YankRing is ready to use by default when you start Vim. If you want to turn it off, run :YRToggle once. Running :YRToggle a second time turns YankRing on again. You can read a general introduction by running :h yankring.txt, and you can view multiple examples with :h yankring-tutorial.

YankRing runs with or without its window, but without the window, much of the purpose of installing the plugin is lost. Within the window, you can select an element, then run a command to pull or edit it.

To start the YankRing window, enter the command :YRShow. The window closes when you select an element to pull or enter the command q. At any time, you can type  ? to view a summary of available keystroke commands.

As in unenhanced Vim, the default commands in the window are p for inserting text after the cursor and P for placing it before. However, YankRing offers several other commands, including replacements for p and P. Additionally, if you are in visual mode, with several lines selected, entering r pulls the lines in reverse order. Pressing d in command mode removes the currently highlighted element from the yankring or, in visual mode, removes everything selected. If you need to find an element in the yankring, type s, enter a text string, and optionally use regular expressions to locate it (Figure 3). After you have searched or deleted, you can use u to update the window's listing – relisting elements.

Figure 3: You can search the yankring using regular expressions.

YankRing also includes commands entered from within Vim. Using :YRGetElem [ELEMENT] highlights the element specified within the yankring, whereas :YRPop [ELEMENT] removes the element specified. With :YRGet Multiple [ELEMENT], you can select for deletion the specified element and all elements below it in the yankring. Alternatively, YRPop [ELEMENT] deletes an element, and YRPopl [ELEMENT],[NUMBER] deletes the designated number of elements, starting with the one specified.

Possibly, YankRing's most efficient feature is the ability to select a range of items. While you are in visual mode, YRYankRange either adds all selected text to the yankring or, if a range prefaces the command (e.g., :5,10YRYankRange), adds all the lines specified, inclusively. YRDelete has the same structure but removes the range of lines instead.

Command-Line Envy

In many ways, YankRing is a model of what a Vim plugin should be. It has both backward compatibility to reduce the time to learn it and increased options to extend its efficiency. Best of all, although unenhanced Vim gives users only a glimpse of its register and almost no control over it, the yankring offers users full control with only a handful of commands.

By themselves, Vim's own yank and pull features are equaled on the desktop only by KDE's Klipper [4]. However, when they are augmented by YankRing, yank and pull have features that are unimagined on the desktop. The next time someone dismisses the command line as primitive, ask them if their copy and paste features can do half of what YankRing's yank and pull can do, and watch them grow envious.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Between the Lines

    Readline provides you with a rich set of tools for working with text and moving around quickly and efficiently on the command line.

  • Programming Snapshot – Python Scripts

    Mike Schilli takes on the almost 2,000-year-old Chinese Rings puzzle. Instead of just jingling rings, he tries to find a solution with logical operators.

  • Ranger

    Ranger offers a wealth of commands – many with alternatives – and less reliance on a mouse.

  • VIM TRICKS

    You don’t have to use the vim text editor as a latter day vi. These simple tricks will save you time and keystrokes.

  • Command Line: Vim Search Commands

    We show you how to build on the basic functionality of Vim by introducing some useful search commands.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News