40

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?

3

5 Answers 5

9

MelBurslan 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.

1
  • 1
    How does one use this? And is this repeatable? Commented Aug 27, 2020 at 17:55
18

Thanks to Johnny for giving us this terrific answer in the comments below:

":nmap <C-i> i_<Esc>r"

That maps Control + i to insert a single character, and it does it very concisely.

In your vimrc file, this will look like:

nnoremap <C-i> i_<Esc>r

I changed my mapping to use space, and you can change yours to your preferred key(s):

nnoremap <Space> i_<Esc>r
2
  • 3
    This is the most concise and simple answer. Commented Sep 24, 2017 at 21:06
  • This is simple and makes sense, but for whatever reason it doesn't work as intended when I put it in my .vimrc. When I do it "by hand" (type i_<Esc>r) it works, but when used by the mapping it leaves me in normal mode, with the cursor on the _. It's probably a question of my vim config, but for the record I'm on neovim 0.9.1 on OSX
    – nietaki
    Commented Jul 7, 2023 at 10:57
4

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.

1
  • 3
    Here's the command to map Ctrl-I to insert a single character: ":nmap <C-i> i_<esc>r"
    – Johnny
    Commented Apr 11, 2013 at 19:40
3

A simple way to do this is using the Cut x and Put p commands. Say that * is the character you want to insert. Insert it using i * Esc. Then hit x. That will cut the character. Now, you can hit p to put the single character. If you need to insert that character 300 times, you can type: 300p.

1
  • 9
    Did you just say that having to press a total of 5 keys is a 'simple way' of typing a character?
    – piezol
    Commented May 28, 2020 at 15:05
2

The solution is great. But given "e" move you to the end of a word, I like this better with one right move "l" as here

:nmap <C-i> li_<Esc>r

or in .vimrc

nnoremap <C-i> li_<Esc>r

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .