I just installed vim-indent-guides plugin.
The default mapping to toggle the plugin is <Leader>ig and I want to remap this to Ctrl-i.
These are my attempts:
nnoremap <C-i> <leader>ig
nnoremap <C-i> <Leader>ig
I have a similar approach to switch to relative numbers with Ctrl-n and it's working:
nnoremap <C-n> :set relativenumber!<ENTER>
What's wrong with the <Leader> key?
With :noremap, the original mapping isn't considered. Though :noremap is usually preferred to avoid interactions with other mappings, here you do need to allow remapping:
nmap <C-i> <leader>ig
This plugin offers a <Plug> mapping to facilitate remapping; then, the original <Leader>ig isn't even defined and free for other uses! Therefore, unless you want to have both, prefer this:
nmap <C-i> <Plug>IndentGuidesToggle
Note: This is all documented by the plugin under :help indent-guides-mappings.
Related
I know that I can use ctrl+w to shift between different Vim windows. But how do I remap the CMD-key to replace ctrl+w in various way? I'm specifically looking to bind cmd with the arrow keys, so that I can shift to NERDTree easily with CMD+LeftArrow. Appreciate the assistance.
I've tried to add the following to $MYVIMRC...
nmap <silent> <D-h> :wincmd h<CR> // For going to NERDTree
nmap <silent> <D-l> :wincmd l<CR> // For going back to file I'm working on.
In the left-hand side of a mapping, Command+Left is written <D-Left>. With this and other issues fixed (see below), your mappings should look like this:
nnoremap <D-Left> <Cmd>wincmd h<CR>
nnoremap <D-Right> <Cmd>wincmd l<CR>
or, simply, like this:
nnoremap <D-Left> <C-w>h
nnoremap <D-Right> <C-w>l
Other issues:
Recursive mappings (nmap) should be reserved to the rare situations where you want to use another mapping in your mapping. This is not the case, here, so nnoremap is the best choice.
The mapped commands don't echo anything so <silent> is useless.
Vim's comment leader is ", not //.
You can't have comments on the same line as a mapping anyway, see :help map-comments.
The newish :help <Cmd> is cleaner than using : in the right-hand side of a mapping.
Note that these mappings only work in the MacVim GUI.
I've found some older posts here that discuss the difficulty with mapping the <C-j> shortcut in vim, i.e.
map <C-j> <C-w>j
due to latex-suite's usage of <C-j> to jump forward in the text. I'm in a similar situation, but I would like to keep latex-suite's normal shortcuts for when editing tex files, and only use the new mapping when editing non-tex files. I'm a little confused by why this isn't the default behavior - latex-suite's <C-j> shortcut shows up as a shortcut in the :map output even when I don't have a tex file loaded and other shortcuts (like `a mapping to \alpha) don't work. Am I understanding correctly that unlike most of latex-suite's shortcuts, this <C-j> shortcut from latex-suite gets loaded regardless of filetype for some reason? And if so, how do I make it so that it only gets loaded for tex files, so that I can use other <C-j> mappings for non-tex files?
The only sane solution would be for the maintainers of that plugin to move all their filetype-specific mappings to an ftplugin, where they belong. The way they currently implement them is a silly mix of good practices (<Plug>) and bad practices (global filetype-specific mappings), all in a single script under plugin/. This is very bad.
What they currently do:
" in plugin/imaps.vim
inoremap <silent> <Plug>IMAP_JumpForward <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>
if !hasmapto('<Plug>IMAP_JumpForward', 'i')
imap <C-J> <Plug>IMAP_JumpForward
endif
The first mapping is okay-ish: plugin authors should use virtual :help <Plug> mappings as much as possible in order to allow users to write their own mappings easily. IMAP_JumpForward should be in parentheses, though.
The :help hasmapto() guard is pointless.
plugin/imaps.vim is a "global plugin". As such, whatever it does that is not explicitly scoped to a buffer or a window is done for every buffer and window. That <C-j> mapping only makes sense…
in the context of tex files, when using that latex-suite plugin,
if you actually use that imaps.vim plugin elsewhere.
What they should do:
" in plugin/imaps.vim
inoremap <silent> <Plug>(IMAP_JumpForward) <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>
" in ftplugin/**/<somefile>.vim
imap <buffer> <C-J> <Plug>(IMAP_JumpForward)
Frankly, the way that thing is designed should raise all kinds of red flags. I don't do (la)tex at all but https://github.com/lervag/vimtex seems more competently done.
In my .vimrc I have these lines
nmap :s :update<cr>
nmap <F5> :set number!<cr>
Without the former mapping, the latter works, otherwise it doesn't. Why is this the case?
The problem is that the second mapping begins in a way, :s in :set, that triggers the previous mapping.
In general you should use non-recursive mappings, unless you have a reason to use recursive mappings.
In this case, you have to use
nnoremap :s :update<cr>
nnoremap <F5> :set number!<cr>
More info at
:help recursive_mapping
What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim?
Recursive mappings - Vim Tips Wiki
Learn Vimscript the Hard Way - Chapter 5
I have this in my .vimrc
nnoremap <leader>p :CtrlP<CR><C-\>w
I change all nmap to nnoremap in my .vimrc and everything works fine except this one:
nnoremap <leader>p :CtrlP<CR><C-\>w
With nmap <leader>p :CtrlP<CR><C-\>w it does automatically insert word into CtrlP and with nnoremap it doesn't, I get blank field, like I just pressed Ctrl-P.
Why it doesn't work with nnoremap?
When you create a mapping with nnoremap, it does not consider your prior mappings when resolving what to do. In other words, if you had previously mapped any of these:
<CR>
<C-\>
w
Then those maps would be ignored in your <leader>p mapping, and instead the default action of those keystrokes would be used.
As far as I know, <C-\> has no default action, so I would suspect you have mapped it (or you are relying on a mapping that another plugin has added), but that mapping is not being taken into account here.
I use the delimitMate plugin in vim for auto-close pairs. The is the default mapping to jump out of the pairs in insert mode. I want to remap it as:
imap <C-j> <Plug>delimitMateS-Tab
But it doesn't work at all.