Given the following

  1. I want to write a file
  2. Change to the directory of the file
  3. Execute a bash command
  4. change to the previous directory

I made the following command

command Asc execute ":w | :cd %:p:h | !ansible-playbook --syntax-check % "

This seems to be working so far. However changing back to the previous directory with :cd - does not seem to work.

command Asc execute ":w | :cd %:p:h | !ansible-playbook --syntax-check % | :cd -"

My guess is, that the pipe and the stuff followed is interpreted by bash and not by vim.

How to overcome this problem? Or is there no way at all?

P.S.: I am aware of the autochdir option of vim

  • Also checkout out Vi and Vim, for Vim-focused questions. – muru Apr 10 at 14:14

From :h :!:

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

But you could do something like:

command Asc execute ":w | ! cd %:p:h; ansible-playbook --syntax-check %"

Which eliminates the need to cd back, since only the executed shell changes directories.

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.