Skip to main content
replaced http://vi.stackexchange.com/ with https://vi.stackexchange.com/
Source Link

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?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.

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.

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.

edited tags
Link
muru
  • 25.1k
  • 8
  • 83
  • 144
Changed question title to better reflect the issue so that others can find it
Link

How do Why don't I get syntax highlighting when I open a new file in the current buffer using Pythonan autocmd?

added 912 characters in body
Source Link
muru
  • 25.1k
  • 8
  • 83
  • 144
Loading
Source Link
muru
  • 25.1k
  • 8
  • 83
  • 144
Loading