Vim logo vim online Vim Book Ad

basic Tip #348: Quickly insert a single word

 tip karma   Rating 1/6, Viewed by 1135 

created:   October 19, 2002 15:43      complexity:   basic
author:   spudnic      as of Vim:   5.7

I've had something like this for a single character for some time, and this may be something that everyone is already doing, but it's really convenient for me.  These two mappings make it quick and easy to insert a word.

nmap <buffer> <silent> ,w :exec ":imap \<space\> \<space\>\<esc\>,BB"<cr>i
nmap <buffer> <silent> ,BB :exec ":iunmap \<space\>"<cr>

Given the sentence:

The quick fox.

To add the word "brown" you would put your curser on the f in fox and type ",w" (or whatever you decide is a good mapping).  Type the word brown.  As soon as you hit space you are out of insert mode.

If there's an easier way to do this, please feel free to comment.

Thanks.

 rate this tip  Life Changing Helpful Unfulfilling 

<<Format paragraph without changing the cursor position | Format your xml document using xmllint >>

Additional Notes

[email protected], October 23, 2002 8:33
Unless I missed something, this doesn't save any keystrokes. Without, I would use two keystrokes "i" to enter insert mode and ESC to exit. Here I still would still need two keystokes "," and "w".
Anonymous, October 23, 2002 11:21
Size isn't everything!  This seems easier.  Finding the ESC is harder than , . and with this you're
entering ,w together rather than a key before your word and another key at the end.  So you start this with 2 characters, but it ends itself automatically, which seems easier and more fluid to me.
[email protected], October 30, 2002 1:50
I am no vim wizard, so this might be offensively ridiculous. Anyway, having used
"standard" vi for a number of years before stumbling on vim, I have mapped my
arrow keys so that I leave insert mode as soon as I try to move anywhere with
them. I simply found it annoying to remain in insert mode after "being done" at
one place. The effect of this mapping on the problem described is that after having
added a word or two in insert mode, I rarely have to think about actively exiting
insert mode. That is taken care of by the arrow key mappings. I have to admit
that I cannot recall exactly what I do, or rather, how painful I find it, to punch that
escape key in the cases I plan to move away from the spot where the word was
inserted in greater strides than with arrow keys.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo