Search code examples
vimautocmd

how to move buffer position in vimscript


I'm trying to use an autocmd to check for the existence of a certain file when a file is opened. If the file exists, I want to do a vsplit, and move the vsplit buffer to the right. Something like:

function! CheckForFile()
    let file=expand("%:p:h")."/.".expand("%:t").".x"
    if filereadable(file)
        execute "vs " . file
        <C-w>L
   endif
endfunction
autocmd BufReadPost * call CheckForFile()

I can't figure out how to do the <C-w>L part. All I get is syntax errors.

How do I move the buffer in the CheckForFile function?


Solution

  • <C-w>L
    

    is a normal mode command; as such, it can't be used as is in the context of a function. The equivalent ex command of all the <C-w> normal mode command is wincmd {char} as you can see at the end of :help window-move-cursor.

    So the correct notation is:

    wincmd L