How to map <c-leader> in vim? - vim

I would like to map ctrl+leader key. Is it possible?
Tried: :nnoremap <c-leader> :CtrlP<CR>
And it does not work.
(ctrlp bindings conflict with yankring bindings)

<Leader> is a special key notation in Vim; as such, it cannot be combined with modifiers such as C-. Assuming the default setting for it (i.e. \), you can use this:
nnoremap <c-\> :CtrlP<CR>

There are two issues, here:
You didn't read CtrlP's documentation where you would have found this:
Use this option to change the mapping to invoke CtrlP in Normal mode:
let g:ctrlp_map = '<c-p>'
<leader> is supposed to be a cross-platform alternative to using the common modifier keys (Alt, Ctrl, Shift, Cmd) in mappings.
Normally, you would use <leader> in place of <Ctrl> as in:
nnoremap <leader>p :CtrlP<CR>
This line in your ~/.vimrc will probably solve your problem:
let g:crtlp_map='<F11>'
Though it won't help much here are my mappings for CtrlP:
nnoremap <leader>f :CtrlP<CR>
nnoremap <leader>b :CtrlPBuffer<CR>
nnoremap <leader>m :CtrlPMRUFiles<CR>
nnoremap <leader>t :CtrlPTag<CR>

For example to map leader key to space try this ...
let mapleader=" "

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>

A confusion about shortcut key of youcompleteme for vim

all. I am trying to use youcompleteme for code completing in vim. Generally, it works well except that when i need to jump between source files.
First. I use the subcommand
:YcmCompleter GoToDefinition
it can find the definition. BUT it seems so verbose. Then I want to map this subcommand into some shortcut key according to the YCM's instruction:
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>
I am a little confused. what is short cut key? l or gl ?
When you create a mapping with <leader>, <leader> is replaced by whatever is in your "mapleader" variable (by default \).
If you have this in your vimrc:
let mapleader=","
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>
It acts the same as:
nnoremap ,gl :YcmCompleter GoToDeclaration<CR>
Thus you could run the YcmCompleter GoToDeclaration command by pressing ,gl
You can type \gl to go to the definition. The default <leader> is the key \. So if you type \gl, you can go to the definition.
And another shortcut you can try, Ctrl+o -> you can return back where you came from.

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.

How do I map keys for specific file types in Vim?

I want my Enter key to follow links in help files because my keyboard doesn't have a ] key. Therefore I've put:
nnoremap <Enter> <C-]>
In ftplugin/help.vim. This works, but this key map is now "global" and messes up the use of the key in other places, for example the q: command window.
So how do I restrict a key-bind to a single buffer, or perhaps even a single file type?
map the command using autocmd:
autocmd FileType c,cpp,php nnoremap <buffer> <Enter> <C-]>
maps only for filetypes: c,cpp and php
Just add <buffer> to your mapping:
nnoremap <buffer> <Enter> <C-]>

Resources