Reputation: 631
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
.
Upvotes: 4
Views: 665
Reputation: 10381
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>
Upvotes: 4
Reputation: 45117
@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
Cycling buffers is kind slow. I believe :bprevious
and :bnext
are only useful in a narrow set of conditions:
<c-6>
/<c-^>
).Instead of cycling with :bp
and :bn
you can jump directly to a buffer via :b
command. Simply use :b {partial_name}<tab>
.
:b
:<tab>
completion<c-d>
to list out completion:b foo
. Works great with <tab>
.:b foo*bar
or :b foo/**/bar
:b
is :sb
.nnoremap <leader>b :ls<cr>:b<space>
For more help see:
:h :b
:h :ls
:h cmdline-completion
:h file-searching
: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'
A fuzzy finder like CtrlP or fzf allows for general file navigation. For more specific project navigation you can use something like Projectionist.vim.
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.
Upvotes: 3