8

While trying to devise a solution to How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?, I have got so far using Python:

function LookupFiles ()
    python <<EOF
from os.path import *
from vim import *
current_file = eval ('expand("%")')
PATHS = ['~', '~/.vim', '/etc']

if not isabs (current_file):
    for p in map (expanduser, PATHS):
        if isfile (join (p, current_file)):
            current.buffer.name = join (p, current_file)
            break
EOF
endfunction

autocmd BufNewFile * call LookupFiles() | e

Simply assigning a path to buffer.name doesn't cause it to load that file. So I had to call :edit manually, which tried both with vim.command('e') and the | e that you see now. However, for a file opened this way, syntax highlighting isn't present (as well as other plugin effects, from what I can tell). If I manually do :e again, everything becomes all right. Why is this, and how can I use Python to correctly open the file? I'd rather not open another buffer unnecessarily, but if that's the case, so be it.


Assigning to current.buffer.name has its own problem: even though vim can load the buffer just fine with e, Vim continues to see it as an entirely new file, and attempts to write through a warning that the file already exists. So I adapted to this version:

function LookupFiles ()
    python <<EOF
from os.path import *
from vim import *
current_file = eval ('expand("%")')
current_index = str (current.buffer.number)
PATHS = ['~', '~/.vim', '/etc']

if current_file != '' and  not isfile (current_file):
    for p in map (expanduser, PATHS):
        f = join (p, current_file)
        if isfile (f):          
            command ('bad ' + f)
            command ('bd ' + current_index)
            break
EOF
endfunction

The problem of syntax highlighting and other plugin effects still show with this method.

0

1 Answer 1

6

From :help autocmd-nested:

By default, autocommands do not nest.  If you use ":e" or ":w" in an
autocommand, Vim does not execute the BufRead and BufWrite autocommands for
those commands.  If you do want this, use the "nested" flag for those commands
in which you want nesting.  For example: >
  :autocmd FileChangedShell *.c nested e!
The nesting is limited to 10 levels to get out of recursive loops.

In your case, instead of this line:

current.buffer.name = join (p, current_file)

You should say:

vim.command("edit " + join(p, current_file))

And to have the syntax highlighting work, you need to update your autocmd to read:

autocmd BufNewFile * nested call LookupFiles()
3
  • Actually, nested is a part of the autocmd, not a modifier for an ex command. The autocmd needs to change to autocmd BufNewFile * nested call LookupFiles() | e
    – jamessan
    Commented Feb 25, 2015 at 15:47
  • @jamessan That's what I understood of the help text and used. I accepted assuming this usage also works.
    – muru
    Commented Feb 25, 2015 at 15:59
  • whoops, I'll revise my comment. Should have read the docs more closely.
    – Matt Boehm
    Commented Feb 25, 2015 at 16:50

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.