VIM not able to use <tab> key in mapping - vim

I try to make this mapping:
nnoremap <leader>b :b <tab>
but it does not work.
It prints this string in the command line:
:b ^I
It seems that I don't use in the proper way.

The key you type to start command-line completion is defined by the option :help 'wildchar'. It is set to <Tab> by default, which can't be used in a macro and thus inside a mapping:
The character is not recognized when used inside a macro. See 'wildcharm' for that.
You need a proxy for that: the aforementioned :help 'wildcharm'.
set wildcharm=<C-z>
nnoremap <leader>b :b <C-z>

Related

vim nmap which disables another nmap

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

Mapping Ctrl+[ and Ctrl+] to move between buffers in Vim

I'm trying to map Ctrl+[ and Ctrl+] to move between buffers.
I have this in my .vimrc:
nnoremap <c-[> :bprevious<CR>
nnoremap <c-]> :bnext<CR>
nnoremap <Esc> :noh<CR>
The Ctrl+] works. The Ctrl+[ trigger an :noh and I don't know why.
I would like to Ctrl+] and Ctrl+[ simply move between buffers and Esc to trigger an :nho.
ctrl+], ctrl+ [ and ESC are already being used by vim. Mapping keys which are already being used by vim is not recommended.
More at :help map-which-keys.
So, instead of mapping those keys, I would like to suggest, for example, to use F2 and F3
nnoremap <F2> :bprevious<CR>
nnoremap <F3> :bnext<CR>
#dlmeetei and #Lucas Beier are correct. These are poor keys for Vim.
Map safe keys like function keys, leader mappings, or unused mappings. Example (same as unimpaired.vim):
nnoremap [b :bprevious<c>r
nnoremap ]b :bnext<cr>
nnoremap ]B :blast<cr>
nnoremap [B :bfirst<cr>
For more help see:
:h map-which-keys
:h key-notation
:h :bfirst
:h :blast
We can do better! or The problem with cycling buffers
Cycling buffers is kind slow. I believe :bprevious and :bnext are only useful in a narrow set of conditions:
These commands become useful after you use more than 2 buffers (probably due to <c-6>/<c-^>).
Once you hit a certain buffer number threshold, there is sort of an upper limit on the usefulness of cycling with these commands. Is it faster to cycle forward? Backwards? Does it matter because it simply takes too long either way?
Instead of cycling with :bp and :bn you can jump directly to a buffer via :b command. Simply use :b {partial_name}<tab>.
Behold the power of :b:
Uses <tab> completion
Use <c-d> to list out completion
Use partial file name. e.g. :b foo. Works great with <tab>.
Globbing. e.g. :b foo*bar or :b foo/**/bar
Split variant of :b is :sb.
Also accepts a buffer number
A common mapping: nnoremap <leader>b :ls<cr>:b<space>
For more help see:
:h :b
:h :ls
:h cmdline-completion
:h file-searching
Can we do better than :b?
Skip the buffer management completely and use tags, cscope, and/or GNU Global. These will help you go directly to where you want to go not just the right buffer with where ever you last left the cursor.
For beginners to tags I suggest Gutentags and :h tags.
You can also use :find with tab completion and set your 'path' to .,,** for a basic less fuzzy finder.
For more help see:
:h CTRL-]
:h tags
:h cscope
:h :find
:h 'path'
Plugins?
A fuzzy finder like CtrlP or fzf allows for general file navigation. For more specific project navigation you can use something like Projectionist.vim.
Conclusion
I would suggest slowly learning more buffer and general navigation commands. These commands will serve you well and help you navigation quicker without resorting to buffer cycling.
Personally, I use a combination of :b, tags, cscope/GNU Global, and projectionist.vim for most of my navigation needs. I often have over 50+ buffers open and get to my desired file without ever resorting to buffer cycling.

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>

mapping a button to sequence of buttons

I want to map tab to the following button sequence: ctrl+x -> tab to a method call in .vimrc (in insert mode). The method is also in .vimrc
I kwow i should use inoremap <tab> button-sequence=method()<CR>
But how should I write the button sequence in the row above?
Thank you
I'm not sure I understand your question but this is how you'd map <Tab> to execute function() in insert mode.
inoremap <Tab> :call function()<CR>
edit
I had to double check because I didn't know that <C-x><Tab> thing. It turns out that the real mapping is <C-x><C-i>: :h i_ctrl-x_ctrl-i. <Tab> and <C-i> represent the same character from the terminal's (and Vim's) standpoint.
So… this is what you want, even if mapping <Tab> to anything other than <Tab> in insert mode seems rather silly to me:
inoremap <Tab> <C-x><C-i>
See :h key-notation and :h mapping.

How to map <c-leader> in 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=" "

Resources