Vim Tips Wiki
No edit summary
No edit summary
Line 16: Line 16:
   
 
Most buffer navigation commands, such as :bnext and :bprevious, skip unlisted
 
Most buffer navigation commands, such as :bnext and :bprevious, skip unlisted
buffers. Which can prove frustrating when there buffers have been opened to
+
buffers. Which can prove frustrating when buffers have been opened to view
view directory listings. Consider the below example.
+
directory listings. Consider the below example.
   
 
<pre>
 
<pre>
Line 34: Line 34:
 
The problem gets worse with several levels of directory navigation.
 
The problem gets worse with several levels of directory navigation.
   
The simplest solution for this is the following two mappings:
+
The simplest solution for this are the following two mappings:
   
 
<pre>
 
<pre>
Line 41: Line 41:
 
</pre>
 
</pre>
   
This maps <C-n> to switch to the buffer with the buffer number one higher than
+
This maps <C-n> to switch to the buffer which has a buffer number one higher
the buffer number of the active buffer. Whilst <C-p> cycles in the opposite
+
than the buffer number of the active buffer. Whilst <C-p> cycles in the
direction.
+
opposite direction.
   
 
However, this does not always work, because there might be holes in the buffer
 
However, this does not always work, because there might be holes in the buffer
 
numbers. E.g. right now my buffer list contains buffer numbers 1, 2, 6, 9, 10
 
numbers. E.g. right now my buffer list contains buffer numbers 1, 2, 6, 9, 10
and 14. Using the above map from buffer #6, would produce the error message
+
and 14. Using the above map for <C-n> from buffer #6, would produce the error
"E86: Buffer 7 does not exist".
+
message "E86: Buffer 7 does not exist".
   
 
A solution is to write a function searching for the next buffer with
 
A solution is to write a function searching for the next buffer with

Revision as of 23:25, 7 July 2008

Tip 873 Printable Monobook Previous Next

created February 10, 2005 · complexity basic · author Anon · version 6.0


This tip provides a means to cycle through all of your buffers (including :help unlisted-buffer buffers such as directory listings):

Most buffer navigation commands, such as :bnext and :bprevious, skip unlisted buffers. Which can prove frustrating when buffers have been opened to view directory listings. Consider the below example.

gvim ~ /etc/motd
    gvim goes to dir ~
  :bn
    gvim goes to /etc/motd
  :bp
    but will not go back to ~, because it is unlisted.
  :ls!
    ~ is there in list of the buffers 1,2
  :buf 1
    have to give it the buffer number to find it.

The problem gets worse with several levels of directory navigation.

The simplest solution for this are the following two mappings:

:map <C-n> :exe ":buf ".((bufnr("%") % bufnr("$"))+1)<CR>
:map <C-p> :exe ":buf ".((bufnr("%") % bufnr("$"))-1)<CR>

This maps <C-n> to switch to the buffer which has a buffer number one higher than the buffer number of the active buffer. Whilst <C-p> cycles in the opposite direction.

However, this does not always work, because there might be holes in the buffer numbers. E.g. right now my buffer list contains buffer numbers 1, 2, 6, 9, 10 and 14. Using the above map for <C-n> from buffer #6, would produce the error message "E86: Buffer 7 does not exist".

A solution is to write a function searching for the next buffer with bufexists().

function! SwitchToNextBuffer(incr)
  let s:last = bufnr("$")
  let s:new = bufnr("%") + a:incr
  while s:new < 1 || !bufexists(s:new)
    let s:new = s:new + a:incr
    if s:new < 1
      let s:new = s:last
    elseif s:new > s:last
      let s:new = 1
    endif
  endwhile
  exe ":buf ".s:new
endfunction

nnoremap <silent> <C-N> :call SwitchToNextBuffer(1)<CR>
nnoremap <silent> <C-P> :call SwitchToNextBuffer(-1)<CR>

References

Comments