Asenar
Asenar

Reputation: 7010

excuting vim command silently then echo in vim

I want to execute a shell command on autocmd BufWrite in my .vimrc , then echo (in vim) a message saying it's donem without showing the shell.

(basically touch *.less), then echo an information message saying "file touched !".

The first part works, but the 2nd command is executed in the shell instead of vim.

The command should be something like this:

autocmd BufWritePost *.less silent! !touch '%:p:h'/*.less | echo 'file  '.expand('%:p:h').' touched !'

I think I need a way to "ends" the command and allow an other command.

I tried this without more success:

:silent !touch '%:p:h'/*.less && exit 0 | unsilent! | echo "file touched"

Any idea ?

Upvotes: 2

Views: 4959

Answers (2)

cchauviere
cchauviere

Reputation: 76

It should work if you past by a function :

function! Cmd()
    silent! execute '! touch %:p:h/*.less'
    echo 'file '.expand('%:p:h').' touched !'
endfunction

autocmd BufWritePost *.less call Cmd()

Upvotes: 6

Kent
Kent

Reputation: 195029

If you call external command, with the leading !, newline is the end of the command, other chars, like |, will be a part of your external command, that is, it will be the shell pipe, and it won't be passed to vim's command. For this part, you can check :h :! for details.

A '|' in {cmd} is passed to the shell, you cannot use
        it to append a Vim command.  See |:bar|.

If you want to combine two commands, one is external, one is vim. You can write a function to wrap the commands, or the following if you prefer shorter one-liner:

:exec "!shell command|shell|shell"|exec "vimcommand"

In this way, the pipe connects the two exec command.

An example you can try:

:exec "!echo 'foo'"|exec "echo 'bar'"

You will get foo in shell stdout and the bar in your vim.

Upvotes: 1

Related Questions