1

I know I can execute the current file, lets say a bash file, with :! bash %. But what can I do if I want to execute the current file in a different directory (such as the location of the current file) than the current vim working directory. I know that I could change to the working directory of the current file by :cd %:p:h and then execute the file but then I would need to jump back again.

Would really appreciate any help.

2
  • cd to the interesting directory inside the script?
    – lcd047
    Commented Jun 16, 2015 at 4:38
  • yeah sure could do that but I would like to avoid to hard code the location since I will need to execute it on another machine
    – DUWUDA
    Commented Jun 16, 2015 at 4:42

2 Answers 2

1

There are various possibilities, but I think you'd either have to jump back (cd -), or execute the command in a subshell; e.g.:

:! cd somedir; bash ~-/% ; cd -

:! bash -c 'cd somedir ; bash ~-/%'
1
  • thanks a lo t! ':cd %:p:h ; bash %:t ; cd -' works as I hoped
    – DUWUDA
    Commented Jun 16, 2015 at 7:56
0

Thanks for the hint from the other answer here. It largely works. But keep typing all those ":cd ...; bash ...; cd -" MULTIPLE TIMES are so bothersome.

In my case, I was going to do a bunch of shell commands. More precisely, I started my vim in repo_foo, AND also open some files in helper repo_bar. Later I realized I would need to do several git commands in that helper repo_bar. I ended up shell out to the repo_bar's directory, by:

:!cd %:p:h; bash

I do NOT need to do cd - to go back to my original working directory, because when I later exit the sub-shell, it will automatically go back to the parent shell with the original working directory intact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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