Vim press <cr> multiple time - vim

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?

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

Remapping <ESC> breaks <S-TAB> mapping in vim

I had the following mapping on my vimrc
nnoremap <TAB> gt
nnoremap <S-TAB> gT
Then I mapped ESC to clear highlights as:
noremap <silent> <ESC> :noh<return>
And then <S-TAB> wont work anymore. If I remove <silent> from <ESC> mapping I see :noh when I press <S-TAB>. I don't know if <S-TAB> and <ESC> has something in common. I'm on Linux, using vim on gnome-terminal.
You're right in assuming that <S-TAB> and <ESC> have something in common. S-TAB is an escape-prefixed keycode.
The ESC keycode is ^[, while S-TAB is ^[[Z. You can see the first part of the S-TAB key code matching the ESC keycode.
See a full table of combinations here.
So you just can't remap the escape key while also remapping one of the key codes including it. Either pick a different key to clear highlights or a different way of switching tabs.

Conflict between tab to insert spaces and superTab

I have conflict right now so that whenever I want to insert a tab (2 spaces since I have set expandtab in .vimrc) I get list options. Currently Im trying to make a mapping
inoremap <C-tab> <C-v-tab>
But this does not work. What am I doing wrong?
If you want to have an alternative mapping for the <Tab> key, that's
inoremap <C-Tab> <Tab>
The noremap part automatically ensures that SuperTab's mappings don't apply, and you get the built-in functionality.
Yours didn't work because of the invalid key notation, Ctrl + V followed by Tab would be (here with :imap to offer a worse alternative):
imap <C-Tab> <C-v><Tab>

Vim: Why does noremap not work in insert mode?

Consider the unbinding of the arrow keys using
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
This works in normal mode, but it does not work in insert mode: one can still navigate with the arrow keys. As a countermeasure, one must include
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
But this doesn't really make sense to me, since I assume map and noremap should work in all modes, while prepending n/v/x/s/o/i/l/c specifies the mapping to work only within that specific mode. Is there a reason for this?
why there isn't an all-inclusive modal map, rather than issuing both map and map!
That's easy to explain: In insert mode mappings, Vim doesn't automatically switch to normal mode (you may want to stay in insert mode, though text translations are typically done via :iabb, not via :imap), so the set of applicable commands is totally different. For example, in normal mode Ctrl-U scrolls upwards, but in insert mode it deletes the entered characters in the line!
Prefixes like <C-O> temporarily switch from insert mode to normal mode. Actually, one often even has to define a different prefix for command line mode, too, as shown by this example:
noremap <C-Tab> :<C-U>tabnext<CR>
inoremap <C-Tab> <C-O>:tabnext<CR>
cnoremap <C-Tab> <C-C>:tabnext<CR>
So when defining mappings, always consider in which modes they are needed and whether they need remapping (:nmap vs. :noremap, prefer the latter).
:help map-overview
map (and noremap) are for normal, visual, select and operator-pending modes.
Contrary to what you might expect, noremap and map do not actually apply to all modes. Based on the very useful summary from :help map-listing, here is a list of the characters that can be prefixed (or suffixed in the case of !) to map, noremap, unmap, and mapclear, along with the modes that they apply to:
(none) – Normal, Visual, Select, and Operator-pending
n – Normal
v – Visual and Select
x – Visual
s – Select
o – Operator-pending
! – Insert and Command-line
i – Insert
c – Command-line
l – ":lmap" mappings for Insert, Command-line, and Lang-Arg
So a noremap mapping will have no effect in Insert or Command-line mode, and without consideration, may not work as intended in Visual, Select, or Operator-pending mode either.
However, mappings can be adapted to work in different modes, simply by changing mode and back in the mapping. For example, noremap mappings that issue command-line commands but only work in Normal mode can adapted to also work in the other modes as shown by this example:
noremap <C-Tab> :<C-U>set list!<CR>
inoremap <C-Tab> <C-O>:set list!<CR>
cnoremap <C-Tab> <C-C>:set list!<CR>:<Up>
noremap applies to the Normal, Visual, Select, and Operator-pending modes, for which :<C-U> enters Command-line mode then clears the current line in case Vim inserts a range; inoremap applies to Insert mode, for which <C-O>: temporarily exits to Normal mode then enters Command-line mode; and cnoremap applies to Command-line mode, for which <C-C>: exits and re-enters Command-line mode to clear the line but, unlike <C-U>, retain it in the command history so that :<Up> can bring it back.
These three mappings cover all six modes. (Apparently ‘Lang-Arg’ isn't a mode.) There are some corner-cases where it doesn't work, but then there are also some cases it works when I'd have thought it wouldn't, and I don't understand why. Also, most of the modes will loose little things like selections and pending operators, even if the mapped command wouldn't otherwise loose these things. For instance, when in Insert mode, I don't see why the example I've given would need to break the current edit into separate changes in the undo/redo history (try typing i123<C-O><Esc>456<Esc>u). To be honest using key mappings to run commands in this way seems like a bit of a hack to me, but I don't know another way.

Map key to toggle between normal mode and insert mode in Vim

I'd like to use two "controls" as a toggle key to switch between normal mode and insert mode in Vim. So I add the following two lines into my .vimrc
nmap <C-><C-> i
imap <C-><C-> <ESC>
But it doesn't work. What's wrong with the above two lines?
It seems you are trying to map Ctrl+Space to toggle insert mode.
nnoremap <C-space> i
imap <C-space> <Esc>
(Came from this Vim tip (marked obsolete, but there's a link to a more rich document on avoiding which includes the tip).)
Remember that this is not guaranteed to work across all terminals and platforms. Some terminals and platforms may eat a given Ctrl+something shortcut, while others don't, so find one that works in your environment.
nnoremap <silent><C-space> :startinsert
inoremap <silent><C-space> <C-O>:stopinsert
That's definitely not going to work. You could use an F key instead.
nnoremap <C-SPACE> i
inoremap <C-SPACE> <ESC>l
works perfectly with GVIM 7.4

Resources