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:
y1Gmeans yank from the current line to the first lineyGmeans 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:
ylwhich yanks a Letteryawwhich yanks around a wordyaswhich 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
lletter, note that there is a shortcut where a and i are not necessarywword, if there is noaorithen it means from the cursor to the end of the word.Wa word that includes the leading and trailing white space where noaor i means from the cursor to the endsthe sentence that ends in a period, exclamation point, or question markpparagraph]or[a bracketed block(,),ba parenthesis block{,}orBfor a block around braces<or>ortan 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:
salets you sandwich add, so for instancesaiw(means take thetextobjectand add around it the character to addsdmeans take the text that is in between the character and delete it, sosdbmeans 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.srmean 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






