vi / vim editor: Can I paste text inside a colon command?
October 21, 2008 12:19 PM   Subscribe

vi / vim editor: Can I paste text inside a colon command?

(Long-time emacs user here, having a great time learning vim) I often want to copy some text and then use it as the match portion of a substitution command, rather than re-typing it. I've looked over the FAQs, the tips, etc. and didn't see this addressed. Is it possible? Or is there some other vi-specific idiom for doing the same thing?

Bonus question: As I'm still getting my head around the vim terminology and resources, what/where could I have searched to find this answer myself?
posted by alb to Computers & Internet (6 answers total) 1 user marked this as a favorite
 
Best answer: Well, the first part is to know the terminology. The mode you enter when you type ":" or search, etc, is either Ex mode or Command mode. For this question, they're the same. So if you go to the docs on command mode, you see this:
CTRL-R {0-9a-z"%#:-=.}					*c_CTRL-R* *c_<C-R>*		Insert the contents of a numbered or named register.  Between		typing CTRL-R and the second character '"'' will be displayed		to indicate that you are expected to enter the name of a		register.		The text is inserted as if you typed it, but mappings and		abbreviations are not used.  Command-line completion through		'wildchar' is not triggered though.  And characters that end		the command line are inserted literally (<Esc>, <CR>, <NL>,		<C-C>).  A <BS> or CTRL-W could still end the command line		though, and remaining characters will then be interpreted in		another mode, which might not be what you intended.		Special registers:			'"''	the unnamed register, containing the text of				the last delete or yank			'%'	the current file name			'#'	the alternate file name			'*'	the clipboard contents (X11: primary selection)			'+'	the clipboard contents			'/'	the last search pattern			':'	the last command-line			'-'	the last small (less than a line) delete			'.'	the last inserted text							*c_CTRL-R_=*			'='	the expression register: you are prompted to				enter an expression (see |expression|)				(doesn't work at the expression prompt; some				things such as changing the buffer or current				window are not allowed to avoid side effects)		See |registers| about registers.  {not in Vi}		Implementation detail: When using the |expression| register		and invoking setcmdpos(), this sets the position before		inserting the resulting string.  Use CTRL-R CTRL-R to set the
So the short answer is, if you want to paste the last line yanked or deleted, then do CTRL-R-".
posted by sbutler at 12:40 PM on October 21, 2008 [2 favorites]


And in case you didn't know how to copy text: use 'v' to enter the visual mode, highlight the region, and then 'y' to yank it. That puts it in the default register, which gets overwritten all the time. To put it in another register, prefix the 'y' with '"'. For example, to place in the 'a' register, you would type: "ay. Then to paste while in ex mode, it's CTRL-R-a.

I love vi/vim because you can find it everywhere. But there's also a secret-society-handshake aspect to it :)

posted by sbutler at 12:50 PM on October 21, 2008


Arg... that's supposed to be: To put it in another register, prefix the 'y' with '"<letter>'
posted by sbutler at 12:53 PM on October 21, 2008


Response by poster: Great! Thanks for answering both parts of my question in one go. I was searching for answers with the term "command mode", but then using "paste" instead of "insert" (and "register" apparently would have helped, too)
posted by alb at 12:54 PM on October 21, 2008


You can also call up the "command line window", which you can edit like it was a normal buffer, or invoke items in it by hitting enter in normal or insert mode. In the command line window, you can use "p" to paste, just like when editing files. Read :help q: for more info.
posted by jepler at 2:15 PM on October 21, 2008


if you're using vim, vimtutor is a great resource (on *nix just run vimtutor, in windows, check the start menu) quick intro to the terminology and methodology, as well as the must-know basics to be effective.
posted by namewithoutwords at 5:33 PM on October 21, 2008


« Older Oaxacan Romance   |   Charter Flights from Dallas de Las Vegas Newer »
This thread is closed to new comments.