Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to **insert** mode from a user-defined command in Vim

Tags:

vim

I'm defining my own command in my vimrc:

command! Tcs :normal lvf`hc

The aim is that I go on a backtick and it will delete the inner content from here to the next backtick. Like:

`hi there` -> ``

Problem is that is remains in normal mode, I would like to replicate the c command behaviour so that I can start typing immediately in insert mode.

I've tried command! Tcs :normal lvf`hc :startinsert but whatever I put after the last c will be typed directly in the editor.

Thanks

like image 284
pietro909 Avatar asked Mar 11 '26 15:03

pietro909


1 Answers

From :help :normal >

        :norm[al][!] {commands}                   *:norm* *:normal*
        (...)
        {commands} should be a complete command.  If
        {commands} does not finish a command, the last one
        will be aborted as if <Esc> or <C-C> was typed.             

This implies that an insert command must be completed.

       (...)

In your case the command normal has been aborted that's why you still in normal-mode. You can still try :normal! i

To get what you want instead you can do :

command! Tcs execute "normal lvf`hd" | :startinsert
like image 192
Meninx - メネンックス Avatar answered Mar 13 '26 08:03

Meninx - メネンックス



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!