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.
nestedis a part of the autocmd, not a modifier for an ex command. The autocmd needs to change toautocmd BufNewFile * nested call LookupFiles() | e– jamessan Feb 25 '15 at 15:47