简体   繁体   中英

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 <cd> to list out completion
  • Use partial file name. eg :b foo . Works great with <tab> .
  • Globbing. eg :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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM