Is there a way to map a sequence of keystrokes to a command-line commmand (a command entered after :
in the Ex mode) in vim?
-
1Sure! Can you give an example of what are you trying to do, or are you just asking in general ?Rook– Rook2009年12月05日 06:13:22 +00:00Commented Dec 5, 2009 at 6:13
-
Just in general, lot's of times I find that I'm doing the same thing repeatedly but on the other hand, I don't want to create a key binding for it (I don't want another Emacs where I'm using modifier keys for all my commands)Opt– Opt2009年12月06日 03:40:02 +00:00Commented Dec 6, 2009 at 3:40
1 Answer 1
Yes, and it's intuitively called :map
Example:
:map foo :echo "bar"<CR>
No when in insert mode you press the keys foo
vim will respond with "bar".
Type :help :map
in vim for more information.
You can place mappings you want to load by default in your .vimrc file.
You can independently map keystrokes for different modes, such as insert mode (:imap) and visual mode (:vmap). See also vim help on the subject of remapping (:noremap)
Update
If you want to use an alias for command mode (but this can be done for insert mode too), you'll want to use abbreviations.
To define an abbreviation for command mode, use :ca (which is a shorthand for :cabbrev). See vim help :help :ca
and for more info :help :abbreviations
.
Notice that unlike map, abbreviations are not replaced by vim commands but by literal characters. Abbreviations are triggered when you press space or enter.
Examples:
" let me type :syn=cpp instead of :set syntax=cpp
"
:ca syn set syntax
" fix my favorite spelling error
"
:abbr teh the
" this does something different than the :map example above
"
:iabb foo :echo "bar"<CR>
" this is ugly, misusing an abbreviation as :map by simulating ESCAPE press
"
:iabb hello <ESC>:echo "world"<CR>
-
This makes certain stuff more annoying though - if for instance I want to enter fo, then I have to way a while for that command sequence to execute since fo is a substring of foo. Is there a way to map command-line commands (commands entered after the : prompt) to a sequence of key presses?Opt– Opt2009年12月06日 03:42:40 +00:00Commented Dec 6, 2009 at 3:42