2

I am trying to modify the following function:

function! changeNumber() abort
    if &nu + &rnu == 0
        set nopaste
        " how to set normal mode after the function exits?
    else
        set paste
        " how to set insert mode after the function exits?
    endif
endfunction

Basically, I'm trying to write a copy-paste helper function that removes the line numbers and enters/exits insert mode when it runs (for the full problem, see: https://stackoverflow.com/a/61952187/651174).

1 Answer 1

3

You can use :startinsert and :stopinsert here.

From :help :startinsert:

Start Insert mode just after executing this command. Works like typing i in Normal mode. When the ! is included it works like A, append to the line. Otherwise insertion starts at the cursor position. Note that when using this command in a function or script, the insertion only starts after the function or script is finished.

So your function snippet becomes:

function! changeNumber() abort
    if &nu + &rnu == 0
        set nopaste
        stopinsert
    else
        set paste
        startinsert
    endif
endfunction

While you might possibly consider using the startinsert! variant to append at the end of the line instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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