I know that in insert mode CTRL-Y can insert the character above the cursor.

Is there a way of inserting the word above the cursor in insert mode ?


With a help of vimscript you might have following:

func! InsertWord()
    " get previous line number
    let prevline = line('.')-1
    " get the line from current position to the end
    let rest_of_line = strcharpart(getline(prevline), virtcol('.')-1)
    " if cursor was on a space, return 1 <C-y>
    if rest_of_line[0] == ' '
        let cnt = 1
        " otherwise count number of chars till the space and return that number of <C-y>s
        let end_of_word = substitute(rest_of_line, '^\(\S\+\).*', '\1', '')
        let cnt = strchars(end_of_word)

    return repeat("\<C-y>", cnt)

inoremap <expr> <c-b> InsertWord()

enter image description here

  • 1
    I think this might fail with multi-width characters (unicode)
    – Mass
    Sep 8 '20 at 16:05
  • 1
    @Mass, true, changed col() to virtcol() and now it also works for non-latin too. Not sure about CJK though, can't test it :)
    – Maxim Kim
    Sep 9 '20 at 6:56

What you can do via a series of commands, is as following:

  • From insert mode, go to normal mode: ESC
  • Go one line up: k
  • Yank a word (or in word, whatever you want to choose): yaw or yiw
  • Go the original line: j
  • Put the word before the cursor: 'P'
  • Go on appending after the cursor: 'a'

In short, :inoremap ,<C-Y> <ESC>kyiwjPa should work.

You can change a few things here and there according to your choices.

  • This will break if you're inserting at the end of the line rather than the middle of the line. You'll actually put it before the last character in the line, rather than after it, where the cursor originally was...
    – filbranden
    May 10 '20 at 14:15

I would do that with mouse clicks. First highlight the word (this could be done with a double-click of the mouse) and then move the mouse to where you want to insert and press middle button of the mouse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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