Skip to content

Commit

Permalink
Include mapping to delete buffers following the order in the tabline
Browse files Browse the repository at this point in the history
  • Loading branch information
pacha committed Aug 30, 2020
1 parent d1c954f commit d27465a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
51 changes: 17 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ nmap <leader>l <Plug>vem_move_buffer_right-
nmap <leader>p <Plug>vem_prev_buffer-
nmap <leader>n <Plug>vem_next_buffer-
```
Where leader is typically set to `\` in Vim.

### Deleting Buffers

You can use any Vim command to delete or wipeout your buffers. However, if you
have reordered them, you'll notice that the next buffer to be displayed is not
the next in the tabline, which is not very intuitive. This is because Vim
chooses the next buffer to display from its internal buffer list and not from
the tabline reordered one. If you want to delete a buffer and get the next one
in the tabline selected, use the following keymap:
```
nmap <leader>x <Plug>vem_delete_buffer-
```
If the current buffer has unsaved changes, you'll be prompted to confirm.

Quick access to tabs
--------------------

You may also want to map the numbered keys to quickly access your tabs. To do
so, use the following key mappings:
Expand All @@ -89,40 +106,6 @@ nnoremap <leader>8 :8tabnext<CR>
nnoremap <leader>9 :9tabnext<CR>
```

Deleting Buffers
----------------

If you reorder the buffers in the tabline and then you delete one of them, Vim
will choose a new buffer to display instead. This will usually be the next
buffer in Vim's jump list and not necessarily the next one in the tabline. If
you delete several of them in a row, you don't really know which buffer will be
selected in the tabline and the resulting effect looks a bit random.

If you want to have the next buffer in the tabline to be selected when you
delete the current one, you can add something like this to your `vimrc`:
```
function! DeleteCurrentBuffer() abort
let current_buffer = bufnr('%')
let next_buffer = g:vem_tabline#tabline.get_replacement_buffer()
try
exec 'confirm ' . current_buffer . 'bdelete'
if next_buffer != 0
exec next_buffer . 'buffer'
endif
catch /E516:/
" If the operation is cancelled, do nothing
endtry
endfunction
nmap <leader>x :call DeleteCurrentBuffer()<CR>
```
With this, you can press `<leader>x` (typically `\x`), and the current buffer
will be deleted, and the next one in the tabline selected. If the current
buffer has unsaved changes, you'll be prompted to confirm.

Of course, you can adapt the snippet to your needs (like using `bwipeout`
instead of `bdelete`) or choose a different key mapping.


Filetype icons
--------------

Expand Down
23 changes: 20 additions & 3 deletions autoload/vem_tabline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,37 @@ function! vem_tabline#tabline.get_replacement_buffer() abort
" get buffer position
let bufnum = bufnr('%')
let bufnum_pos = index(self.tabline_buffers, bufnum)
let num_buffers = len(self.tabline_buffers)

" check if current buffer is not in the tabline
if bufnum_pos == -1
" check if current buffer is not in the tabline or it is the last one
if bufnum_pos == -1 || num_buffers < 2
return 0
endif

" get replacement buffer position
let next_pos = bufnum_pos + 1 < len(self.tabline_buffers) ? bufnum_pos + 1 : bufnum_pos - 1
let next_pos = bufnum_pos + 1 < num_buffers ? bufnum_pos + 1 : bufnum_pos - 1

" get replacement buffer number
let next_buf = self.tabline_buffers[next_pos]
return next_buf
endfunction

" Delete current buffer and select next one in tabline as the current one
" (You can delete a buffer with Vim commands but the next buffer selected will
" not follow the order in the tabline).
function! vem_tabline#tabline.delete_buffer() abort
let current_buffer = bufnr('%')
let next_buffer = self.get_replacement_buffer()
try
exec 'confirm ' . current_buffer . 'bdelete'
if next_buffer != 0
exec next_buffer . 'buffer'
endif
catch /E516:/
" If the operation is cancelled, do nothing
endtry
endfunction

" Get next/prev buffer in list (according to the stored sorting)
" 'direction' is 'left' or 'right' and the return value is the buffer number
" if current buffer is not in the list return 0
Expand Down
3 changes: 3 additions & 0 deletions plugin/vem_tabline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ map <silent> <Plug>vem_move_buffer_left- :<C-u>call vem_tabline#tabline.move_buf
" move buffer to the right
map <silent> <Plug>vem_move_buffer_right- :<C-u>call vem_tabline#tabline.move_buffer('right')<CR>
" delete buffer
map <silent> <Plug>vem_delete_buffer- :<C-u>call vem_tabline#tabline.delete_buffer()<CR>
" Autocommands
augroup VemTabLine
autocmd!
Expand Down

0 comments on commit d27465a

Please sign in to comment.