I use Vim for pretty much everything these days. So I’ve decided to start a “tips” category for my favorite Vim tricks. Here’s a question for you: how can you paste into the command buffer? And more importantly, how can you copy the command buffer to the clipboard?
First the easy part… Copying from the clipboard to the command line is as simple as typing SHIFT-INSERT. Donzo! Just make sure to hit the colon (:) first to get you into command mode.
But the opposite is much more daunting. It’s happened to me a thousand times. I find the perfect search and replace regular expression through trial and error and now it’s stuck on the command line. And I need it for a shell script! How do I extract it? Copy and paste won’t work! Instead I type the following on to the command line:
let @*=@:
Voila! Your last command has now been copied to the clipboard, suitable for adding to your shell script. If you’ve run a few commands since your last search and it’s the regular expression you need, you can instead use:
let @*=@/
That command instead copy the last search regardless of how long ago it occurred in the history.
Happy Vimming!
