I usually work with php projects and phpunit. I've mapped <leader>T thus I can run tests.

# my .vimrc file portion
nnoremap <Leader>T :!vendor/bin/phpunit --filter
function! RunFilteredTests()
    let l:filter = input('Filter test with ... ')
    exec ':!php ./bin/phpunit --filter ' . l:filter . "\<CR>"

Lately, I've started to play with golang. Can I use same shortcut with different behavior depending on the current open file?


Sure, you can use the FileType autocmd.

" Set the default behavior
nnoremap <Leader>T :!vendor/bin/phpunit --filter

" Now set it if we open a go file
autocmd FileType go nnoremap<buffer> <Leader>T :call GoFunction()

You can, of course, change go to whatever filetype you want. If you're not sure what filetype to use, open the file that you want it to work in and type :set ft?. This will tell you what filetype your file is.

See :help :autocmd, :help FileType, and :help <buffer> for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.