1

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?

d0c_s4vage
  • 3,947
  • 6
  • 23
  • 32
  • I feel like this could be an annoying feature. There is a bunch of work that needs to be here to make this snippet work. Wouldn't a nice command or a mapping be a better option than an autocommand? If you are dead set please look at `:h ` and`:h :rightb`. You are also needlessly using `expand()` and you should use `:normal!` to execute normal commands. – Peter Rincker Nov 03 '14 at 21:45
  • yes, potentially annoying, but I'd only explicitly turn it on in certain situations. – d0c_s4vage Nov 03 '14 at 21:59
  • how do you use it without expand? I can't do `let file=%:p:h/.%:t.x` as that gives syntax errors, and `let file="%:p:h/.%:t.x"` with quotes simply yields the quoted string with no expansion. – d0c_s4vage Nov 03 '14 at 22:02
  • using `execute "rightb vs ". file` worked great! but adding `normal! h` doesn't do anything though – d0c_s4vage Nov 03 '14 at 22:11
  • You have 2 `expand()`'s that are not really doing anything productive. You should need just the following `let file = expand('%:p') . '.x'` or better yet do pass in `expand('')` into your function. Use something like `windcmd p` or `windcmd h`. This is far too much information for a comment and I don't feel like posting an answer. I really just wanted you to think about creating a command as I felt like it would be less invasive. – Peter Rincker Nov 03 '14 at 22:16
  • Thank you for taking the time to comment. It is very very appreciated. – d0c_s4vage Nov 03 '14 at 22:19
  • I'm using two expands because I'm prepending a `.` to the basename of the file. Otherwise, yes, `%:p` would have worked perfectly. Thanks again. – d0c_s4vage Nov 03 '14 at 22:24

1 Answers1

3
<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
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    This is the correct answer for this situation. In other cases where you need to use a normal mode command from a script you can use the `:normal` command. (You could use it here too, but it's roundabout given the existence of `wincmd`.) You'd need a literal ctrl-W, or you can use it with `exe` and say something like `exe "normal \L"` (the `\` inside double-quotes will be turned into a ctrl-W for you by vimscript.) – Laurence Gonsalves Nov 03 '14 at 22:23
  • thanks! I didn't know about the escaping of `` in double quotes – d0c_s4vage Nov 03 '14 at 22:25