Copying and pasting with Vim and YankRing
Yank and Pull
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).
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).
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.
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.
Infos
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.