Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found a way to do this using python in a vimscript. With python I was able to get the names of all the buffers from <code>vim.buffers[i].name</code> and used <code>os.path</code> and <code>os.sep</code> to process which buffer to switch to.</p> <p>In the end, I decided that it would be more helpful for it to refuse to do anything if the buffer it was requested to switch to was ambiguous.</p> <p>Here it is:</p> <pre><code>"Given a file, full path, or partial path, this will try to change to the "buffer which may match that file. If no buffers match, it returns 1. If "multiple buffers match, it returns 2. It returns 0 on success function s:GotoBuffer(buf) python &lt;&lt; EOF import vim, os buf = vim.eval("a:buf") #split the paths into lists of their components and reverse. #e.g. foo/bar/baz.py becomes ['foo', 'bar', 'baz.py'] buf_path = os.path.normpath(buf).split(os.sep)[::-1] buffers = [os.path.normpath(b.name).split(os.sep)[::-1] for b in vim.buffers] possible_buffers = range(len(buffers)) #start eliminating incorrect buffers by their filenames and paths for component in xrange(len(buf_path)): for b in buffers: if len(b)-1 &gt;= component and b[component] != buf_path[component]: #This buffer doesn't match. Eliminate it as a posibility. i = buffers.index(b) if i in possible_buffers: possible_buffers.remove(i) if len(possible_buffers) &gt; 1: vim.command("return 2") #delete the next line to allow ambiguous switching elif not possible_buffers: vim.command("return 1") else: vim.command("buffer " + str(possible_buffers[-1] + 1)) EOF endfunction </code></pre> <p><strong>EDIT:</strong> The above code seems to have some bugs. I am not going to fix them because there is another answer which is much better.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload