Insert Single Character in Vim?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands
--
Chapters
00:00 Insert Single Character In Vim?
00:25 Answer 1 Score 4
00:43 Accepted Answer Score 9
01:27 Answer 3 Score 18
01:50 Answer 4 Score 3
02:14 Thank you
--
Full question
https://superuser.com/questions/581572/i...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#vim
#avk47
ANSWER 1
Score 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
ACCEPTED ANSWER
Score 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.
ANSWER 3
Score 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.
ANSWER 4
Score 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
.