Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In Vim (7.2), there is a normal mode command r to replace a single character with another. For example, typing rX will replace the one character under the cursor with X and then return you to normal mode.

Is there a normal-mode command to insert a single character and then return to normal mode?

share|improve this question
    
Possible cross site duplicate: stackoverflow.com/questions/1557893/… –  Ciro Santilli Jan 23 at 14:12
add comment

2 Answers

up vote 2 down vote accepted

Mel_Burslan is correct that this feature does not natively exist, but creating a user-defined command is not really the way to go about creating it. I tinkered for a few minutes and came up with this:

:nmap <silent> ,s "=nr2char(getchar())<cr>P

Which uses some Vim trickery involving "putting" text from a register, in this case the "expression" register. The expression being plugged into the register is "nr2char(getchar())" which will return a single character string.

The reason I built the mapping this way is that getting user input "midway through" a mapping is tricky and can behave unpredictably; even this mapping will drop the cursor down to the status area while waiting for the user to type a character.

share|improve this answer
add comment

As far as I know, there is no such function in any widely distributed incarnation of vi editor but, vim has a facility to create custom commands. It has previously been discussed here: in this thread

You might be able to create your custom command doing what you wish to do.

share|improve this answer
    
Here's the command to map Ctrl-I to insert a single character: ":nmap <C-i> i_<esc>r" –  Johnny Apr 11 '13 at 19:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.