I have this in my .vimrc

let $ROOTPATH=FindRootDirectory()
nnoremap <leader>ct :!clear<CR>:!$ROOTPATH/app/Console/cake test %:p<CR>

Is there a way I can write this in one line?

Using vim-rooter pluguin

nnoremap <leader>ct :exe "!clear; ".FindRootDirectory()."/app/Console/cake test %:p"<CR>

The key is the :exe command, which executes its argument strings. In this case there is one argument string, which is the concatenation (. operator) of "!clear; ", the output of the FindRootDirectory() function, and "/app/Console/cake test %:p". The <CR> terminates the mapping. I transformed the two shell commands into one, but you can use two commands if you prefer. See

:help :exe

An alternative to @garyjohn's answer is to use a map expression, i.e.

nnoremap <expr><leader>ct ':!clear<cr>:!' .
  \ FindRootDirectory() . '/app/Console/cake test ' . expand('%:p') . '<cr>'

With the <expr> keyword for the mapping, the right-hand side is evaluated and it's result will be used as the mapping. In this case, the result is the concatanated string. See :h map-expression for more details.

  • +1 Thank you very much. It does work perfectly. Sorry for not accepting you answer. Learn a lot. Again, thank you man.
    – whitesiroi
    Feb 16 '16 at 8:03
  • 2
    No problem, you can only mark one answer as accepted. Which answer you prefer is of course up to you. I just wanted to show a different way of solving the same problem, and I think both solutions are relatively equivalent. Feb 16 '16 at 8:07

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.