Hubro
Hubro

Reputation: 59313

How do I map a key combination to perform a command in Vim?

I want the key combination <C-X> to run the command :! python application.py. From all my understanding of mapping, I tried the following: :map <C-X> :! python application.py which almost works. It just opens the command line and types the command in, but I still have to press return for the command to launch.

Is there any way I can get it to run the command as well?

Upvotes: 1

Views: 201

Answers (2)

user1902824
user1902824

Reputation:

Don't forget you can escape any key sequence with CtrlV (in Insert mode). So another way of getting the keycode for "Return" is CtrlVEnter. It will come out looking like ^M.

Upvotes: 0

Hubro
Hubro

Reputation: 59313

I found an answer in an unrelated post while doing my last searches before posting the question. It seems that putting <CR> behind the command executes it, making this do what I wanted:

:map <C-X> :! python application.py<CR>

According to the comments (thanks to melpomene) <CR> means "carriage return", which in effect means "press enter" and is just what I needed in this case.


Slightly related, I also found out that :!! repeats the last shell command executed, making my map somewhat superfluous.

Upvotes: 2

Related Questions