Best of both worlds: arrow keys to move the cursor or flip through buffers

I really like this vim trick to use the left and right arrows to switch between buffers:

"left/right arrows to switch buffers in normal mode
map <right> :bn<cr>
map <left> :bp<cr>

(Put this in ~ / .vimrc)

But sometimes I chew a sandwich or something while scrolling through a file, and I really want the arrow keys to work fine. I think it would be most reasonable for the arrow keys to have the aforementioned buffer flush functionality only if there are actually several buffers open.

Is there a way to extend the above to do this?

+3
source share
3 answers

I would rather have a completely different mapping, because:

  • , , .
  • <left> <right>, , l h;

:

nnoremap <expr> <right> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bn\<cr>" : "\<right>")
nnoremap <expr> <left> (len(filter(range(0, bufnr('$')), 'buflisted(v:val)')) > 1 ? ":bp\<cr>" : "\<left>")

:

:h :map-<expr>
:h len()
:h filter()
:h range()
:h bufnr()
:h buflisted()
+3

alt-direction .

nmap <A-Left> :bp<CR>
nmap <A-Right> :bn<CR>

hl, . ( whichwrap, hl .)

- jk, :

" work more logically with wrapped lines
set wrap
set linebreak
noremap j gj
noremap k gk
noremap gj j
noremap gk k

, jk , . ( , .) .

.

help showbreak
+2

I map Tab and Shift + Tab to switch buffers when they are in normal mode (it makes sense for my brain, and the keys do nothing useful otherwise).

Add this to your .vimrc

" Use Tab and Shift-Tab to cycle through buffers
nnoremap <Tab> bnext<CR>
nnoremap <S-Tab> :bprevious<CR>
+1
source

Source: https://habr.com/ru/post/1748252/


All Articles