1

I have discovered that it is possible in insert mode to execute a single command and return to insert mode by using Ctrl-O.

What about the possibility to insert a single word while in normal mode ?

I could, of course, switch to insert mode, write the word, escape and go back to normal mode.

But I wondered if it was possible to map a sequence (e.g. Ctrl-K) allowing without leaving normal mode to quickly add a single word at the cursor position ?

I tried mapping a function with a prompt (cf. question 11567596 - Ingo Karkat's answer - and Wikia tip 1616) but failed : the word was appended at the end of the line...

Community
  • 1
  • 1
ThG
  • 2,361
  • 4
  • 22
  • 33

2 Answers2

3

My best advice is to embrace insert mode; it's the vi way TM.

Seriously, I also had the idea of creating mappings that allow me to enter a word / sentence / whatever, and then automatically return to normal mode. But...

  1. Whether you use the input() from romainl's answer, or a more elaborate approach (e.g. with a CursorMovedI hook), you have to press Enter or another key to indicate that you're done. That's no better than pressing Esc to leave insert mode the normal way!

  2. You just make things more complex: you spend valuable (short) key sequences for it, you have to spend brain cycles to decide between both approaches, your muscle memory must learn to ways to enter text, and these specials aren't available in other vi-modes (e.g. in Bash).

So, I think it's just not worth it.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I took this as a training exercise. I actually copmpletely agree with you. – romainl Sep 19 '12 at 18:41
  • @Ingo Karkat : I know both of you are right, of course (and I hope this is not a case of not "grokking vi"...). But 1) I wanted to try it as a proof of concept (I hope this is the adequate term) 2) I have started using VimTouch on Android (Galaxy Nexus with JB) and whatever allows me not to move to and fro from the normal mode in a cramped screen is welcome 3) I only write plain text and will use that - as normal mode is intended to - for fine-trimming my texts. So your answers were both wise... and helpful. – ThG Sep 19 '12 at 19:39
2

This simple function works, here:

function! InsertWord()
  let l:user_word = input("Type something then hit ENTER: ")
  execute "normal i".l:user_word
endfunction

command! InsertWord call InsertWord()

nnoremap <C-k> :InsertWord<CR>

The whole thing could probably be shortened to a one-liner, though, but I like it like that.

romainl
  • 186,200
  • 21
  • 280
  • 313