There is a line below in vimrc example file
inoremap Ctrl-u Ctrl-G u Ctrl-u
What's the meaning of inoremap and what's the function of this line?
For more on why the command has such a bizarre name see this excellent description between the difference between map and noremap. Really good to know!
To summarise that article, here's a choice quote:
One downside of the *map commands is the danger of recursing...
Vim offers another set of mapping commands that will not take mappings
into account when they perform their actions.
So noremap came about to avoid horrible recursion of mappings like
:nmap dd O<esc>jddk
where the dd in the right-hand side of the map recurses back to the left-hand side definition of the map, and Vim gets stuck in an infinite loop!
The vim :help inoremap is very poetic about this:
:ino[remap] {lhs} {rhs} mapmode-i :ino :inoremap
:ln[oremap] {lhs} {rhs} mapmode-l :ln :lnoremap
:cno[remap] {lhs} {rhs} mapmode-c :cno :cnoremap
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings. Often
used to redefine a command. {not in Vi}
Thus it makes some insert-mode mappings for ^U that show the filename (^G, undo the most recent change (u), and scrolls the buffer upwards by half a screen (^U).
I have no idea why someone would want this specific sequence of commands, except to demonstrate the inoremap feature -- the ^U at the refers to the meaning the command had when the definition was created, rather than calling back into the redefined ^U mapping.
I also wondered about this. See http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_1)#Insert_mode_maps :
Insert mode maps
To map keys that work only in the insert and replace modes, use the 'imap' or 'inoremap' command.
Example: The following command maps to insert the directory name of the current buffer:
:inoremap <F2> <C-R>=expand('%:p:h')<CR>
To display the currently defined insert mode maps, use the 'imap' command without any argument:
:imap
To remove a keymap from insert mode, use the ':iunmap' command. For example, the following command removes the insert mode map for .
:iunmap <F2>
As printable keys insert a character in the current buffer in insert mode, you should use non-printable keys to create insert mode maps. Some examples for non-printable keys include the function keys , keys prefixed with the Ctrl or Alt key.
[snip]
So, for example, in my ~/.vimrc I have
inoremap jk <ESC>
inoremap jj <Esc>
which when pressed in Insert mode return me to Normal mode.
Related
I would like to remap <C-W>d (from cucumber-vim) to a simple mouse double-click. I tried
noremap <2-LeftMouse> <C-W>d
but that doesn't do anything except give me E388: Couldn't find definition.
What am I doing wrong?
Because noremap disallows mappings of rhs. See :help :noremap:
:no[remap] {lhs} {rhs}
Map the key sequence {lhs} to {rhs} for the modes where the map command applies. Disallow mapping of {rhs}, to avoid nested and recursive mappings. Often used to redefine a command.
You defined your mapping using noremap, so you can't use the feature of cucumber-vim. Instead it uses default <C-W>d behavior, :help CTRL-W_D:
Open a new window, with the cursor on the first macro definition line that contains the keyword under the cursor. The search starts from the beginning of the file. If a count is given, the count'th matching line is jumped to.
Actually this raises E388: Couldn't find definition error.
So, if you want cucumber-vim behavior, try map <2-LeftMouse> <C-W>d. For your information, the following is the definition of <C-W>d in cucumber-vim.
nnoremap <silent> <script> <buffer> <C-W>d :<C-U>exe <SID>jump('split',v:count)<SID>foldopen<CR>
I'm working on a plugin to allow bracket completion (I know it's available, it's more of a learning exercise). To properly implement it, I need to add to the backspace mapping. However, as it's an important key, I'd rather keep the existing functionality and just add to it rather than reimplementing the functionality. The steps would basically be when in insert mode and press backspace, execute the original backspace key, then check for some conditions and maybe remove more characters.
I've tried something like imap <backspace> <backspace><call_func_here>, but that doesn't seem to work. Again, I know I could remap backspace to just the function and try to recreate the backspace functionality, but I'd prefer to not do that.
Is this possible in vim?
I think what you are trying to do is the following:
inoremap <silent> <BS> <BS><C-o>:call MyFunction()<CR>
inoremap allows to create a non recurrent mapping in insert mode (it is often a good idea to use nore in your mappings). :h :inoremap
<silent> precise that the mapping will not be echoed on the command line (You will not see :call MyFunction() in the command line) :h :map-silent
<BS> is the reference to the backspace key that you want to remap.
The second <BS> is here to issue a backspace in insert mode
<C-o> switches to normal mode for only a command. :h i_CTRL-O
:call MyFunction() is the call to your function the way you would do it in normal mode.
<CR> correspond to the Enter key which validate the call to your function.
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|.
For example, with VimOrganizer:
* one
Then, when I hit Enter, this is what ends up in the buffer:
* oneorg#tbl#kbd_cr()
It looks like you're mixing an expression mapping with a standard mapping.
org#tbl#kbd_cr() is a function that returns keystrokes to an expression mapping (:help map-expr). <SNR>17_AutoPairsReturn is a (script-scoped) mapping itself. To be able to concatenate the two, use an intermediate mapping for the function:
:inoremap <expr> <SID>org-mapping org#tbl#kbd_cr()
:imap <Enter> <SID>org-mapping<SNR>17_AutoPairsReturn
At least for the .vimrc, there are different modes to map a key, for example, imap for insert mode, nmap for normal mode. If you map something with imap, it needs to be aware that it's in insert mode and generate commands appropriately, for example:
:inoremap <F2> <C-R>=expand('%:p:h')<CR>
See Mapping keys in Vim for me info.
i'm looking for a way to trigger SuperTab when writing a . (dot) in insert mode. I came up with something like this:
inoremap <buffer> . .<C-x><C-o>
But this just triggers Omnicompletion and not SuperTab. I want it to trigger SuperTab so I have my SuperTab configuration (preselect first entry, autoclose preview window).
Also I still want my normal SuperTab binding (which is Tab).
This could probably be solved by simulating a keypress (while in insert mode), but I just cant get it working.
inoremap <buffer> . .<Tab>
Just inserts a literal tab.
Any hints?
Thanks!
inoremap means exactly:
:ino[remap] {lhs} {rhs}
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings.
Have you tried:
:imap . .<tab>
?