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?
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>
Related
Is there any way to achieve that remapping? I looked a bit into
map
and
inoremap
but they appear to affect within a given mode, not how to enter a given mode.
You could use nmap, e.g.,
:nmap ; :
to map semicolon to colon. The 'n' in nmap indicates normal mode.
If you wanted to use, say, the <F2> function key to enter command-line mode from insert mode, you can do:
:imap <F2> <Esc>:
There's an exhaustive 3-part tutorial about key mappings in vim here.
In vim I'd like to use a hotkey to enter a mapping command, which in turn maps a hotkey to a string of commands. Here's what I'm doing:
nnoremap <leader>r :map `t :w<cr>:silent !make<cr>:redraw!<cr>
As you can see, when I press <leader>r, vim will put the mapping command on the command line and I can modify the actual command (in this case make) then create the mapping by pressing enter.
Now, this doesn't really work because the <cr>s will apply to the nnoremap command. How do I escape those and have them show up on the command line so they apply to the :map command?
To add <...> literally to a mapping you need to escape the file < with <lt>. So <cr> would become <lt>cr>.
So your mapping would look like
nnoremap <leader>r :map `t :w<lt>cr>:silent !make<lt>cr>:redraw!<lt>cr>
This is covered in the help as a subsection to :h command-bang
Replacement text
The replacement text for a user defined command is scanned for special escape
sequences, using <...> notation. Escape sequences are replaced with values
from the entered command line, and all other text is copied unchanged. The
resulting string is executed as an Ex command. To avoid the replacement use
<lt> in place of the initial <. Thus to include "<bang>" literally use
"<lt>bang>".
I see <cr>s a lot in vim mappings, but what does it do?
:help key-notation
says:
notation meaning equivalent decimal value(s)
-----------------------------------------------------------------------
<CR> carriage return CTRL-M 13 *carriage-return*
<Return> same as <CR> *<Return>*
<Enter> same as <CR> *<Enter>*
Mappings often involve Ex commands and you must press <CR> to execute them so it's included in the mapping.
Why <special keys>?
While you can use literal keys in mapping definitions (the Enter key would appear as ^M, or even just as an additional new line, depending on the settings), Vim provides a special key notation for key (combinations), so that it is easier to define (you don't have to use i_CTRL-V to literally insert the special character) and understand (<A-a> better expresses the intention than the equivalent รก) the mappings.
See :help key-notation for a list and explanation.
Why <CR>?
As many mappings invoke Ex commands (e.g. :w) and therefore have to switch from normal to command-line mode, they have to conclude the command with <Enter> (or <CR>), just as you would when manually typing the command.
The <CR> in vim mappings is the carriage return usually the Enter on your keyboard.
<CR> in a mapping corresponds to the Enter key just like a in a mapping corresponds to the A key. Ley's say you have this mapping
:map <f8> :wq<cr>
This will map F8 to the key sequence :WQEnter (which would save the current buffer and quit).
It's basically a means to say "this is the end", see map:
When you have a mapping that contains an Ex command, you need to put a line
terminator after it to have it executed. The use of is recommended for
this. Example:
:map _ls :!ls -l %<CR>:echo "the end"<CR>
also,
<CR> [count] lines downward, on the first non-blank character |linewise|.
I have a some command set up in my .vimrc for mapping ,r.
But when I press ,r, vim does not trigger a command immediately but waits a while so I can type more characters. That means I have something set up for longer mapping, e.g. ,re or ,rt.
I don't use any mapping like that, nor see any <leader>rX mappings in my .vimrc. Looks like some plugin is setting it.
Is there a way to show all leader mappings to figure out what mapping is that?
Try with :map command. It shows all current mappings including those created with <leader>.
On my system (vim 7.3 on OS X)
:map ,r
shows all mappings starting with ,r
The output of :map can be a bit too long. Refining the question gives better results:
:verbose map <leader> " all mapping in all modes that use <leader>,
" and where they are defined
:verbose map <buffer> " all mappings defined for the current buffer,
" and where they are defined
:verbose nmap <leader> " all normal mode mappings that use <leader>,
" and where they are defined
More info:
:h :map
I found this snippet from unite.vim
nnoremap <leader>t :<C-r>Unite -no-split -buffer-name=files -start-insert file_rec<cr>
It seems it try to map "\t" to "Unite -no-split -buffer-name=files -start-insert file_rec"
what does <C-r> mean here ? what is the difference from the following:
nnoremap <leader>t :Unite -no-split -buffer-name=files -start-insert file_rec<cr>
In Ex mode, Ctrl+R (written <C-r> in Vim) will insert the content of the specified register on the Ex command line. <C-r>U will insert the content of the U register. All commands starting with : are Ex mode commands.
To see this in action, do the following:
Delete a word using "adw. This means "delete a word to register a".
Enter normal mode.
Type in :<C-r>a. Your Ex mode command line will now contain the word.
I have looked at the relevant .vimrc and the unite.vim source, and I doubt the <C-r> is supposed to be there. It is most likely a bug or a typo.
For more information, see :help c_CTRL-R.