OK, this has been something I’ve done more and more. With the MacOS, there is pbcopy that lets you pipe in anything into the Mac clipboard, and pbpaste takes it from there, and the more clumsy xcopy or xsel where you have to add options so it’s not as short as pbcopy and pbpaste. This is most useful when you want to say copy quickly the contents of a file into a graphical application.
The bigger trick though is if you just want a part of a file. In that case, iterm copy and paste don’t work that well for me. I’m not sure why, but it turns on Vim has a special register *
. If you remember the way that you copy something into a register is with "a2yy
for instance says into the register a copy 2 lines where y stands for yank since c
is taken for change.
Recall that copy and paste are pretty logical, to add something to a register, you use the quote notation to tell you what register, if there is no register, then you are using the “unnamed” register. So for instance yy
copies the line and p
will put it in a new place.
And if you want to use a register, then it is "ayy
puts the line into register a and then "ap
will take register A and put it there.
Note that you can add both motions (moving the cursor) so these work as an example of where any motion command works:
y1G
means yank from the current line to the first lineyG
means yank from the current to the last liney}
means yank from the current cursor position to the next paragraph
What makes vi so powerful is beside motion commands, there are text objects which describe the text in different units with a
means around and i
means inside:
yl
which yanks a Letteryaw
which yanks around a wordyas
which yanks to around a sentenceya"
yi(
which yanks within a paragraph
What’s all this then about Text Objects
This is a new concept that wasn’t in the original vi, so I haven’t used them very much, but here is how they work, but here is the syntax, so there are some atomic ideas for text object selection (this is different from motion because it doesn’t mean your cursor needs to be positioned at the beginning, with motions, you actually have to first move to the front and then have a command to get to the back so this is convenient when you are inside a text object) so first put in the a
or around i
for in and then. Note that this needs vim-sandwich turned off as it is a superset of that plugin, so if you have vim-sandwich not only can you select, but you can change things
l
letter, note that there is a shortcut where a and i are not necessaryw
word, if there is noa
ori
then it means from the cursor to the end of the word.W
a word that includes the leading and trailing white space where noa
or i means from the cursor to the ends
the sentence that ends in a period, exclamation point, or question markp
paragraph]
or[
a bracketed block(
,)
,b
a parenthesis block{
,}
orB
for a block around braces<
or>
ort
an HTML block with angle brackets or a tag around it"
text between double quotes'
text around single quotes- ` text around backquotes
Vim-Sandwich replacements are cool
These are particularly useful with vim-sandwich which lets you replace the surrounding stuff. So adding this all together you can:
sa
lets you sandwich add, so for instancesaiw(
means take thetextobject
and add around it the character to addsd
means take the text that is in between the character and delete it, sosdb
means a search for the text object block and deleting what’s in the middle such as sdap so run vim-sandwich delete around the paragraph.sr
mean replace the text object with the character sosrb"
means replace what is around the block with double quotes and srb”( means replace what is surrounded by quotes with parentheses