kev
kev

Reputation: 161674

Is there a hook before vim open a filename?

I usually open code.h and code.cpp at the same time.
And display them side by side vertically in a window.

Currently I use:

$ vim -O code.{h,cpp}

If vim can run some commands(to manipulate filename) just before opening a file,
I can simply use:

$ vim code

How can I write this kind of vim-script?


Edit:

file_line.vim is great.

Upvotes: 1

Views: 768

Answers (2)

ib.
ib.

Reputation: 28954

In this case, a possible solution is to modify the argument list (see :help arglist) on Vim startup. It can be done using a VimEnter auto-command that iterates over the argument list and replaces items not corresponding to existing files with those items concatenated with certain suffixes, as follows.

autocmd VimEnter * call AddArgsSuffixes(['.h', '.cpp'])
function! AddArgsSuffixes(sfx)
    let args = []
    for f in argv()
        if filereadable(f)
            call add(args, f)
        else
            for s in a:sfx
                call add(args, f . s)
            endfor
        endif
    endfor
    exe 'args' join(map(args, 'fnameescape(v:val)'))
endfunction

Upvotes: 1

icyrock.com
icyrock.com

Reputation: 28608

Given this:

and :help autocmd-event, you should be able to use BufReadPre event for doing something before buffer is read:

BufReadPre            starting to edit a new buffer, before reading the file

Upvotes: 2

Related Questions