vim nmap which disables another nmap - vim

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

Related

How do I map CMD (Command ⌘) to shift MacVim window

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.

vim mapping problem in vimrc file but can not find the mapping that I set

I am using vim-markdown-toc plugin(successfully installed) and want to remapping some hotkey to specific function. I export this code autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked to my .vimrc file. But when I type :verbose imap <C-x>, it shows can not find the mapping.
Can anyone tell me what's the problem about this?
and also I also want to ask how to map one hotkey to multiple function?
autocmd Filetype markdown noremapb <silent> <C-x> :GenTocMarked
has two obvious errors:
noremapb should be noremap, without the b:
noremap <silent> <C-x> :GenTocMarked
There should be a <CR> at the end:
noremap <silent> <C-x> :GenTocMarked<CR>
The right-hand-side of a mapping is a macro: since you press <CR> to execute the command :GenTocMarked, it should be present in the RHS.
Then comes the diagnostic mistake: the :map command, and its non-recursive buddy :noremap create mappings for normal, visual, and operator-pending modes, but :imap prints out insert mode mappings so you can't really expect it to find a mapping created with :map.
Then comes the semantic mistake: the re in noremap is part of nore (short for non-recursive), not of remap. <C-x> is not a mapping so you are not "remapping" anything.
Then comes the scoping mistake: :noremap creates mappings for three modes, which is something you probably don't want. You should be more specific:
" normal mode mapping
nnoremap <silent> <C-x> :GenTocMarked<CR>
and, finally, the autocommand abuse mistake: there already is a built-in mechanism for sourcing filetype-specific config so there is no need to reinvent the wheel in your vimrc:
" in after/ftplugin/markdown.vim
nnoremap <buffer> <silent> <C-x> :GenTocMarked<CR>

Why nnoremap doesn't work with CtrlP and nmap does work? - VIM

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.

How map <leader>char in Vim?

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.

Normal command map, tab is interpreted as literal

When I make a .vimrc entry,
nnoremap <silent> <leader>c :colorscheme <tab>
The tab is applied if I understand the terminalogy, as a literal, that is, upon typing ,c, I get in Vim command line,
:colorscheme ^I
I tried to internet the search terms, but mostly I get results about remapping Vim Tabs; the closest I found was somebody putting quotes around their <tab>, but I think that is for a different desired outcome.
I also have this, which is why I want the tab in my shortcut,
set wildmenu
set wildmode=longest:list,full
You'll need the 'wildcharm' option:
set wildcharm=<C-z>
nnoremap <silent> <leader>c :colorscheme <C-z>
See :help 'wildcharm'.
As a side note, I use that option with great effect for switching buffers:
nnoremap <leader>b :buffer <C-z><S-Tab>
and a file-opening variant would be just as easy and just as useful:
nnoremap <leader>e :edit <C-z><S-Tab>

Resources