Sunday, November 30, 2014

Advanced Vim: Recording and Multiple Clipboards

Once you have a basic understanding of Vim, you'll want to learn about some of Vim's more advanced functionality.


Recording allows you to capture a series of operations, assign them to a key, and play them back on demand. By default recording is allocated to the q key in normal mode, and is quite the involved process. Nothing you won't be able to master, however. Just like normal and insert mode, "recording" is itself a mode. In order to begin recording commands, you have to press q, followed by a lowercase key, carry out your steps and then end the recording by pressing q again. So let's say we wanted to record a series of steps where we surround a line with <h2> tags:

# let's say your text file contains the following line:
This is the title

# in normal mode, move your cursor on the same
# ...line as this text and press the following

q # prepare recording
a # open up the a buffer
^ # move to the beginning of the line
i # enter insert mode
<h2> # type opening tag
ESC # hit escape button to return to normal mode
A # press "A" to jump to end of the line in insert mode
</h2> # type the closing tag
ESC # hit escape button to return to normal mode
q # press q to stop recording

Playing Back a Recording

Now anytime you want to wrap a line of text inside h2 tags, just jump to that line and type @a. This will command Vim to play back the recording stored in the a buffer.

Registers a.k.a. Multiple Clipboards

You'll discover early on using Vim that every delete command executed by x or dd is really a cut onto a clipboard. That cut content can later be retrieved by pressing p or P in normal mode. Another way to copying content to the keyboard involves pressing yy. The first y stands for yank and the second y is the motion, which in this case is the entire line. Alternatively you can press a capital Y for the same effect. Note any motion can follow the yank command:

  • yy will yank the entire line.
  • yiw will yank the inner word
  • yt. will yank until a period is found
  • yf. will yank until a period, including the period
  • yi" will yank everything inside quotes, give the cursor is between those quotes

In Vim, clipboards are referred to as registers and there are multiple at your disposal. And just like recording, to access these registers, you need to type in a prefix. Where recording's prefix is q followed by a lowercase key, copying requires you to press " followed by a lowercase key.

Anytime you need to view a list of your registers, enter normal mode and type :reg.

# let's say you have the following text
Hello World

# place your cursor over Hello

"aiw # type these to copy Hello to the register
w # skip to next word
"biw # copy "World" to b register
o # press "o" to jump to next line
ESC # hit escape button to return to normal mode
"ap # type these to paste Hello
o # press "o" to jump to next line
ESC # hit escape button to return to normal mode
"bp # type these to paste World

Mac OS X Clipboard Sharing

This will allow you to share the clipboard across multiple Vim sessions on a Mac OS X.

# install macvim first
brew cask install macvim

# add this alias to your ~/.bashrc
alias vi='/opt/homebrew-cask/Caskroom/macvim/7.4-76/MacVim-snapshot-76/'

# reload from the command line
source ~/.bashrc

# make sure +clipboard is in the output
vi --version

No comments:

Post a Comment