Petr Skocik
Petr Skocik

Reputation: 60058

Quickly switching between a file and a test file in vim

Suppose I'm editing

src/a/b/c/d.c

and I expect a test file for this file to be in

test/a/b/c/d.c.c

how can I alternate between files following this pattern quickly?

Upvotes: 1

Views: 779

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32926

a.vim and my alternate-lite fork support a searchpath option where you could specify how we can (quickly) switch between directories. They're more tuned to jump between a header file and a definition file, but it should be possible to add test files as well -- I don't know how it'd behave with .c.c VS .c actually.

Given the pattern you've given us, the vanilla (non scalable) approach would be something like (untested):

function! s:alt_name(name) abort
    if a:name =~ '\.c\.c$'
        return substitute(a:name, '\v<test>/(.*)\.c', 'src/\1', '')
    elseif a:name =~ '\.c$'
        return substitute(a:name, '\v<src>/(.*\.c)', 'test/\1.c', '')
    else
        return a:name
    endif
endfunction

command! -nargs=0 Switch :exe ':e '.s:alt_name(expand('%'))

Of course, if you need to jump to a window where the buffer is already opened, or split, or... well. That's why there are plugins.

Upvotes: 1

Related Questions