2

I'm certain this will have been asked somewhere, but I can't for the life of me find it, and it's not in the Defining command-line commands section of the Vim documentation.

I want to create a user-defined command which will insert the word foo before the current word.

(Note: I want it to be a function because I don't trust myself to remember yet another shortcut key. I know how to do it with noremap...)

In my .vimrc I add:

command AddFoo bifoo<esc>w

But when I type :AddFoo I get Not an editor command: bifoow.

Is it possible to have a function which issues normal mode commands?

3 Answers 3

5

The :normal Ex command allows to issue arbitrary normal mode commands.

command AddFoo normal! bifoo<esc>w

If you want to interpolate expressions etc., you need to use :execute; I'll just show your example again with the use of :help key-notation:

command AddFoo execute "normal! bifoo\<esc>w"

The ! after :normal prevents the use of custom mappings, like :noremap (vs :map).

Sign up to request clarification or add additional context in comments.

Comments

1

Please make sure you get the difference between a command and a function.

The right hand side of a command definition is supposed to be at least one Ex command like write or bnext:

command! Foo update | tabnext

You can call a function:

command! Bar call Bar()

or execute a normal mode macro:

command! Baz normal ciw"<C-r>""

See :help :normal.

Comments

0

Should be simple to get what you want, you just need to switch to normal mode to make your changes:

command AddFoo normal bifoo<esc>w

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.