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.
Related
I don't know if sequentially is the right word, but I'd like to nnoremap Ctrl+t to Ctrl+y and then a comma. Is that possible or do I need to do this another way?
Yes, add
nnoremap <C-t><C-y>, a
to your ~/.vimrc, where <C-t> and <C-y> are ctrl+t and ctrl+y, respectively, and a is whatever action you want to have the keys map to.
You map the keys you want to press to what you want to happen.
If you want Ctrl+t to happen when you press Ctrl+y then ,, then your mapping should look like this:
nnoremap <C-y>, <C-t>
If you want Ctrl+y then , to happen when you press Ctrl+t, then your mapping should look like this:
nnoremap <C-t> <C-y>,
The concept of mapping is introduced in chapter 40 of the user manual, which you should have read, and further discussed under :help mapping.
But, judging by your comments under the other answer, creating a normal mode non-recursive mapping is not the solution to your problem.
First, because the plugin exposes <C-y>, in normal, visual, select, and insert modes so you must cover all of them:
" insert mode
inoremap <C-t> <C-y>,
" visual mode and select mode
vnoremap <C-t> <C-y>,
" normal mode
nnoremap <C-t> <C-y>,
Second, because <C-y>, is itself a mapping so you want your custom mapping to be recursive:
imap <C-t> <C-y>,
vmap <C-t> <C-y>,
nmap <C-t> <C-y>,
While the mappings above work, the whole idea a bit shortsighted because:
The <C-y> in <C-y>, is a "leader" that is common to all the mappings exposed by that plugin. You can now press <C-t> instead of <C-y>, but all the other functionalities of that plugin are still behind that <C-y> leader and still follow that <leader><key> pattern so your system is now inconsistent.
You are overriding <C-t> in normal and insert modes, where it is pretty useful.
I find a .vimrc file config:
" Move selection up/down (add =gv to reindent after move)
:vmap <D-S-Up> :m-2<CR>gv
:vmap <D-S-Down> :m'>+<CR>gv
I know the D-S-Up must be a key, but what is the key?
I type:
:help D-S-Up
nothing happened
:help key-notation tells you the meaning of all those <key> notations.
You can't expect :help D-S-Up to do anything because:
it doesn't follow established patterns like i_ctrl-r,
it is a custom mapping and Vim only provides documentation for its own commands.
<D> is the Cmd key on Mac keyboards, <S> is the Shift key, and <Up> is the Up arrow key.
So <D-S-Up> means Cmd + Shift + Up.
The Cmd key only works in the MacVim GUI.
Non-portable mappings are worthless.
One should use :xmap or :xnoremap for visual mode mappings, not :v*.
Non-recursive mappings should be used by default unless one really wants recursion.
Using someone else's vimrc is a bad idea.
By the way, here are enhanced versions of those mappings:
nnoremap <silent> <D-S-Up> :<C-u>move-2<CR>==
nnoremap <silent> <D-S-Down> :<C-u>move+<CR>==
xnoremap <silent> <D-S-Up> :move-2<CR>gv=gv
xnoremap <silent> <D-S-Down> :move'>+<CR>gv=gv
where:
<silent> prevents the commands in the mapping from echoing useless info,
<C-u> removes any default range inserted by Vim,
move-2<CR> is a more readable version of m-2<CR>,
== re-indents the line,
gv=gv reselects the lines, re-indents them, and re-selects them again.
Have a look at Move entire line up and down in Vim
In an answer you can read (concerning the line :vmap <D-S-Up> :m-2<CR>gv):
According to the shortcuts above, pressing Cmd-Shift-Up/Down will shift the block selection up/down. "D" is the Command key in MacVim, for Windows try "C" (Control), or "A" (Alt) (eg. would be Control Alt f).
Since I don't have a Mac myself, I can't check, but it's most certainly true.
I'm trying to map <Esc> to turn off search highlighting in Vim. The problem is that keys simulated by the terminal with +Esc are affected.
The terminal sends characters much fast than I type. Is there perhaps a way to map key + timeout in vim?
The same question was asked 4 years ago and the answer was that it can't be done. Is this (still) true?
Mapping :nohlsearch to escape key
Your troubles are being caused by some plugin or other, native vim handles this fine. Start vim with vim --noplugin, or if that's not enough then bypass your vimrc with vim -u NONE (or gvim -U NONE) and :source this:
set nocp " life's too short for pure vi-compatibility mode
set timeout ttimeout " enable separate mapping and keycode timeouts
set timeoutlen=250 " mapping timeout 250ms (adjust for preference)
set ttimeoutlen=20 " keycode timeout 20ms
nno <ESC> :nohls<CR>
I've never seen and can't reproduce the interference you're describing so I don't know what's causing it, all I can suggest is binary search with your plugin set.
Yes, it's still not possible for the reason given by ZyX in his answer.
<Esc> is "special" because its behavior sits between a "normal" key like a (you can map it to whatever you want) and a modifier key (it's used by the terminal to represent a lot of special keys like <Up>).
Safely mapping <Esc> to do anything else/more than <Esc> is possible but you'll have to noremap all the affected keys. Here is what I have in my vimrc to mitigate that side effect:
nnoremap <Esc>A <up>
nnoremap <Esc>B <down>
nnoremap <Esc>C <right>
nnoremap <Esc>D <left>
inoremap <Esc>A <up>
inoremap <Esc>B <down>
inoremap <Esc>C <right>
inoremap <Esc>D <left>
I found this code the forum to make my life easier when switching between tabs and I placed it in my ~/.vimrc. Happily, it works well. Unfortunately, whenever I ctrl+tab to change tab, the cursor moves back one character. Can anyone help me keep the cursor in one place?
set showtabline=2
:nmap <C-S-tab> :tabprevious<cr>
:nmap <C-tab> :tabnext<cr>
:nmap <C-t> :tabnew<cr>
:map <C-t> :tabnew<cr>
:map <C-S-tab> :tabprevious<cr>
:map <C-tab> :tabnext<cr>
:map <C-w> :tabclose<cr>
:imap <C-S-tab> <ESC>:tabprevious<cr>i
:imap <C-tab> <ESC>:tabnext<cr>i
:imap <C-t> <ESC>:tabnew<cr>
The cursor moves back one character because that's how i works. Technically, you can change
:imap <C-tab> <ESC>:tabnext<cr>i
to
:imap <C-tab> <ESC>:tabnext<cr>li
but staying in insert mode is not a very good idea. If you can't help it, I'd suggest you remove the three imap from your ~/.vimrc to force you to use Vim correctly.
By the way…
You don't need that leading : in your ~/.vimrc:
map <C-t> :tabnew<cr>
You don't really need those :map because you already have nmap (and imap, but it should go).
<C-t> is very useful, both in insert mode (indents the current line) and in normal mode (goes back in the tagstack). You shouldn't override it like that.
<C-S-Tab> will probabbly not work in a terminal.
Tabs are not designed as 1-to-1 proxies for files and thus should not be used as such. You should use buffers instead.
Everything romainl said is good advice, in particular that tabs are not supposed to work as one-to-one proxies for files, like in other editors. Learn to use buffers, and your life will be easier. They're really very simple: all you really need is :set hidden, :ls, :bn, :bp, maybe some mappings like the ones you're setting up for tabs, and a bit of the Vim help.
That said, romainl's mapping will fail at the end of a line. A better solution uses <C-o>, which takes you out of insert mode temporarily for one command, and takes into account stuff like end-of-line behavior. You could use:
:imap <C-tab> <C-o>:tabnext<cr>
I use the Vimwiki-Plugin a lot, but remapping <Backspace> and <CR> is just anoying. If I use :nmap, the mapping is shown:
n <CR> #<Plug>VimwikiFollowLink
n <Backspace> #<Plug>VimwikiGoBackLink
If I try to remove tha mapping with :nunmap <CR> I get an "E31: No such mapping" error. Is there a way to give <CR> and <Backspace> its's normal behaviour back?
if you want to just disable it, you could give
:nunmap <buffer> <CR>
because it is a buffer-local mapping.
or
:h vimwiki_<cr>
you found:
<CR> Follow/create wiki link (create target wiki page if
needed).
Maps to |:VimwikiFollowLink|.
To remap: >
:nmap <Leader>wf <Plug>VimwikiFollowLink
if you remap that to another key, e.g. the keys in example <leader>wf, the <cr> would be reset to normal.
because in its code, vimwiki has:
if !hasmapto('<Plug>VimwikiFollowLink')
nmap <silent><buffer> <CR> <Plug>VimwikiFollowLink
endif
same for the <BS>