0

Often I want to view two files at once. I switch between the buffers I want to view, so I want a simple key to do so.

I would like to have keymaps that let me:

  • Select a buffer from the open buffer list, the MRU or the current directory and
  • Display it next to the current buffer (side-by-side).
  • Replace existing vertical split if any exists (OFC display the buffer in the same window , even if the buffer is assigned to another window)
  • Ignore dir list buffers (opens in vertical split as well)

If the selected buffer is already visible and displayed side by side I would like the script to switch the focus to the corresponding window.

(by side-by-side I mean in vertical split, if that is clearer)

5
  • I don't understand your description at all.
    – romainl
    Feb 2 at 7:55
  • Added some details
    – eyal karni
    Feb 3 at 14:31
  • 1
    That sounds a lot like :help :sbuffer with a proper :help 'switchbuf'.
    – romainl
    Feb 3 at 14:49
  • Nice find, but buffer number is not that useful. I mean you need to incorporate fzf or leaderf to find the right buffer .
    – eyal karni
    Feb 5 at 1:54
  • Not a "find", just a basic feature. And no one said anything about buffer number. Those plugins are pointless il you know the basics.
    – romainl
    Feb 5 at 3:51

1 Answer 1

0

I use Leaderf plugin to switch buffer. As it automatically switches to existing window, care should be taken to avoid it. Also, there is some logic in closing buffers, ignoring dirlist buffer (I use nvimtree, but can be adapted).

So, mo switch to already open buffer , \OF to MRU \og to current dir.

nmap <M-Bslash> :let g:Lf_JumpToExistingWindow = 0<CR>:Leaderf --popup buffer<CR>
noremap mm :LeaderfMru<CR>

nmap mo :call CloseVspIfNeed()<CR>:vnew<CR>ml<M-Bslash>
nmap <leader>og :call VspIfNeed()<CR>:let g:Lf_JumpToExistingWindow = 0<CR>:LeaderfFile<CR>
nmap <leader>OF :call VspIfNeed()<CR>:let g:Lf_JumpToExistingWindow = 0<CR>mm


function! CloseVspIfNeed()
    let max_wincol = -1
    let argmax_win_id = -1
    let ll=0 
    "counts real buffers

    for k in getwininfo()
        let win_id=k['winid']

        let winnr=win_id2win(win_id)
        let ft= getbufvar(winbufnr(winnr), '&filetype')
        if k['winrow']<=2 && k['wincol']>max_wincol
            let ll=ll+1
            let max_wincol=k['wincol']
            let argmax_win_id=win_id
        endif
        if (ft=~'NvimTree')
            let ll=ll-1
        endif

    endfor

    if argmax_win_id != -1 && ll>1
        if OnRight()
            call GoOther()
        endif
        call win_gotoid(win_id)
        :close
    endif
endfunction

function! GoOther()
    if OnRight()
        call feedkeys("\<c-w>h")
    else
        call feedkeys("\<c-w>l")
    endif
endfunction
function! OnRight()
    let k=getwininfo(win_getid())[0]
    return (k['wincol']!=1)
endfunction 
1
  • 1
    Is this an answer or additional information that should be edited into the question?
    – Friedrich
    Feb 2 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.