mapping a button to sequence of buttons - vim

I want to map tab to the following button sequence: ctrl+x -> tab to a method call in .vimrc (in insert mode). The method is also in .vimrc
I kwow i should use inoremap <tab> button-sequence=method()<CR>
But how should I write the button sequence in the row above?
Thank you

I'm not sure I understand your question but this is how you'd map <Tab> to execute function() in insert mode.
inoremap <Tab> :call function()<CR>
edit
I had to double check because I didn't know that <C-x><Tab> thing. It turns out that the real mapping is <C-x><C-i>: :h i_ctrl-x_ctrl-i. <Tab> and <C-i> represent the same character from the terminal's (and Vim's) standpoint.
So… this is what you want, even if mapping <Tab> to anything other than <Tab> in insert mode seems rather silly to me:
inoremap <Tab> <C-x><C-i>
See :h key-notation and :h mapping.

Related

Vim: Remap Ctrl-W in insert mode to behave as it does in normal mode

I would like Ctrl-W to allow me to switch windows even when I am in insert mode. How can I make this change?
My motivation is to not need to press escape before shifting windows.
For <c-w><c-j> (as an example), you can do:
inoremap <c-w><c-j> <esc><c-w><c-j>gi
Then you can repeat this kind of mapping for every command you use:
inoremap <c-w><c-k> <esc><c-w><c-k>gi
inoremap <c-w><c-w> <esc><c-w><c-w>gi
inoremap <c-w>+ <esc><c-w>+gi
inoremap <c-w>- <esc><c-w>-gi
...
If you choose this simple solution, then you can finally add this mapping to inhibit the native <c-w> key (= delete the last word):
inoremap <c-w> <nop>
More "smart" solutions could be written, but they would imply a bit more code.
Note 1: as noted in the comments, the mappings to choose depend on which mode you want to reach after the keystroke: the suffix gi in the commands above means that you want to go back to insert mode in the new window; but you can remove this suffix if you want to be in normal mode.
Note 2: the suffix gi could be simply i, depending on the case : see :h i and :h gi

Vim press <cr> multiple time

How do you simplify this
inoremap <C-S-Tab> <cr><cr><cr><cr>
to this?
inoremap <C-S-Tab> <cr>*4
Being an insert mode mapping,
inoremap <C-S-Tab> <cr><cr><cr><cr>
can't be "simplified" with a count because numbers are printable characters too. You want four <CR>s, your mapping has four <CR>s: it's perfectly fine as-is.
There are a few ways to "complexify" it, though…
inoremap <expr> <C-S-Tab> repeat('<CR>', 4)
uses an "expression mapping" which computes the expression at runtime, see :help <expr>, :help repeat().
inoremap <C-S-Tab> <C-o>4a<CR><Esc>i
leaves insert mode for one normal mode command and then does a<CR> four times before coming back to insert mode.
Note that <C-S-Tab> is not a portable combo as it is indistinguishable from <S-Tab> in most environments.
how about use record function like using 'r' to record return press then use 4#r to execute 4 times?

Vim - Command Line - previous and next command key mapping

When opening the command line and pressing the up arrow or down arrow keys, it shows the commands there were typed the last time. Is there a way to map this behaviour? For example when I press ctrl p, I want vim to show me my previous command (make vim act as if I pressed the up arrow). The same thing for ctrl n for the next command.
How can I make this happen?
The CTRL-P and CTRL-N keystrokes already do what you want, they search your command history. See :help c_CTRL-P, which explains how it will "recall older command-line from history."
The way CTRL-P and CTRL-N work differs slightly from the up and down arrow, in that the arrows will only go through the items in history that start with the characters you typed. So :e, space, up arrow will go to the last command you used to open a file for editing. See :help c_<Up> for details.
You can remap them so that they do the same as their counterpart, by using the cnoremap command, which creates mappings for keystrokes typed while in the Vim command-line.
For example, to make CTRL-P and CTRL-N behave the same as the arrows (complete respecting the prefix), you can use the following commands to create a (somewhat naive) mapping:
cnoremap <C-P> <Up>
cnoremap <C-N> <Down>
The shortcoming of this approach is that CTRL-P and CTRL-N behave differently on a wildmenu, so a more complete mapping would be:
cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"
That will preserve the original behavior of CTRL-P and CTRL-N in the wildmenu.

Vim mapping Cntrl-i to toggle between normal and insert

inoremap <C-i> <Esc> " cntrl-i to switch to normal
nnoremap <C-i> a "cntrl-i to switch to insert
Tried the following to have cntrl-i toggle between normal and insert modes. However, when I toggle into insert mode it works but pastes everything after a, this happens also when replacing a with i. It fails completely when in insert mode and just inserts a tab.
Alternatively if anybody has better suggestions for a mapping to toggle between both, I'm all ears. I wanted to do caps lock however couldn't find a mapping for it in the vim docs.
Dont put comments after maps:
inoremap <C-i> <Esc>
nnoremap <C-i> i

Jump with Ctrl-I doesn't work in my MacVim, but Ctrl-O works?

Jump with CtrlI doesn't work in my Vim, but CtrlO works.
It's weird, verbose map <c-i> or verbose map <c-I> shows below:
s <Tab> <Plug>snipMateNextOrTrigger
Last set from ~/.vim/bundle/vim-snipmate/after/plugin/snipMate.vim
x <Tab> >gv
Last set from ~/.vim/plugin/settings/Settings.vim
n <Tab> v>
Last set from ~/.vim/plugin/settings/Settings.vim
When I press CtrlI, the current line will be indented, and vim goes to Visual Mode.
I tried to add unmap <c-i> to ~/.gvimrc, but it failed, when macvim starts
Error detected while processing /Users/dfang/.vimrc:
line 83:
E31: No such mapping
How can I get back my CtrlI, and how can I remap CtrlO to Ctrl- (left from = key) ?
<C-i> and <Tab> are strictly equivalent.
You are actively overriding <Tab> and therefore <C-i> in ~/.vim/plugin/settings/Settings.vim.
If you want <C-i> to work normally don't override it (or <Tab>). Simple.
Unless you have a very specific reason to do so, you don't need to put anything in ~/.gvimrc.
The normal place for your settings/mappings is ~/.vimrc, there's no valid reason to put them anywhere else (~/.vim/plugin/settings/Settings.vim or wherever).
Use nnoremap <C-+> <C-o> if you want <C-+> to do what <C-o> does, whatever benefit you think you will get from doing that.
Ye snipmate is adding the mapping.
Thanks #dfang. Based on your question
I tried to add unmap <c-i> to ~/.gvimrc, but it failed, when macvim
starts
I tried to use <C-i> instead and it worked!
An alternative is to press Ctrl-Shift-i (also Ctrl-Shift-o) instead.
Older keyboards did not have the TAB key so Ctrl-I was used for that purpose, but for vim, it seems we can take advantage of this:
Ctrl-i == TAB
Ctrl-Shift-i != Shift-TAB

Resources