3

In my current vim session, I would like to open a new terminal window and immediately execute the cd command in that terminal to change to a specific directory. I have tried

:terminal cd /home/me/folder

But this does not work, and gives me the message

executing job failed: No such file or directory

How can I do this?

3
  • 1
    I'm surprised that fails. Perhaps because cd is a builtin shell command. If there's really no direct way to do this I'd probably use a workaround like a custom command: com! -nargs=1 Cdterm lcd <args> | term. Then :Cdterm somedir.
    – B Layer
    Aug 9, 2020 at 20:59
  • 1
    Duplicate of this question: vi.stackexchange.com/q/19867/71 Aug 10, 2020 at 6:34
  • 1
    If relevant, try using github.com/kassio/neoterm in neovim. No such problems.
    – eyal karni
    Aug 10, 2020 at 9:38

1 Answer 1

2

I agree with B Layer's comment: It fails because cd is a builtin command in most modern systems. That means it does not exist as a standalone command such as /bin/echo and /usr/bin/gcc.

Then, a workaround is to invoke the shell to execute the command.

:ter sh -c "cd /home/me/folder;sh"

If sh does not resolve to your preferred shell, substitute it by the latter, for example, Bash:

:ter bash -c "cd /home/me/folder;bash"

As you can see, the shell is invoked again at the end, because the first shell would quit immediately after executing cd, leaving an unmodified and useless terminal buffer behind.

3
  • 1
    Awesome, thanks! Aug 12, 2020 at 21:45
  • 1
    Now that I think about it, even if cd wasn't a built-in, I don't think :terminal cd would have the effect I want, since it would only leave you with a window showing an "empty" buffer, and the shell would have been exited, which isn't really what I wanted anyway. So to me, the key here is really invoking the the shell a second time as you are doing above. Aug 12, 2020 at 22:04
  • @JamesHungerford You are welcome! Yes, I imagined that was your case. You may want to edit the question to explicitly include the requirement of staying with the shell after executing the cd command. The linked duplicate question does not seem to have that requirement.
    – Quasímodo
    Aug 12, 2020 at 23:54

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