0

I would like to the present working directory (:pwd) be changed across all tabs and subwindows at the same time. I tried:

let PreferredPWD=expand('%:p:h') " The desired working directory

" Need to replace PreferredPWD with its string.
" None of the following work:

tabdo windo cd PreferredPWD
cd(PreferredPWD)
cd &PreferredPWD
cd =PreferredPWD
cd $PreferredPWD

How does one replace the variable name with its contents?

Afternote

The following uses register a to work around my ignorance of how to replace a variable name with the variable content, but I'd still be interested in how such replacement is done in VimScript.

let @a=expand('%:p:h')
tabdo windo cd <Ctrl+R><Ctrl+A>
1

2 Answers 2

1

To change it to the content of PreferredPWD you can do:

:execute 'cd' PreferredPWD

If you have multiple tabs and multiple windows that have local working directory (lcd) you can change them all to the content of PreferredPWD with:

:tabdo windo execute 'cd' PreferredPWD

Remark: One of the usage of execute is that it let you use Vim expression to build your command.

5
  • 1
    For many years if not decades, I also believed that there was only one present working directory. It may be the intended behaviour, but in many recent years, I have found that it is not the case in practice. In view of this, your solution can be adapted for use across tabs and windows as tabdo windo execute 'cd' PreferredPWD. BTW, I was wondering whether you might add to your answer and describe how to substitute a variable's content for its name in a VimScript command? Thanks! Commented Jan 12, 2023 at 17:19
  • 2
    See :h :lcd and :h :tcd.
    – romainl
    Commented Jan 12, 2023 at 17:40
  • @romainl: How very interesting! Vim as evolved. Or maybe Vim always had this, and a portion of my awareness is trapped back in the "vi" stage. However, I have never used lcd or tcd, so there must be some other avenue by which my tabs and subwindows got different present working directories. Commented Jan 12, 2023 at 19:49
  • 1
    @user2153435 thanks for the remark, I have learned something about the current working directory of windows in Vim ;-). I have adapted the solution accordingly. Commented Jan 12, 2023 at 20:17
  • 1
    :tcd is relatively new but :lcd has been there for a while.
    – romainl
    Commented Jan 12, 2023 at 21:39
1

Like most Ex commands, at least those inherited from ex or inspired by it, :cd doesn't accept variables as argument but it can expand a bunch of things like %, documented under :help cmdline-special. In fact it also honors filename modifiers so you could simply do:

:cd %:p:h

If you still want to store that value, you can put it in a variable and then use :help :execute, as in the other answer, but there is another way that you unknowingly encountered while trying those various methods. Environment variables, also, are expanded on the command-line, so you could define one:

:let $PREFERRED_DIR = expand('%:p:h')

and use it directly with :cd:

:cd $PREFERRED_DIR

This is basically the same mechanism as in the popular :so $MYVIMRC.

5
  • Thanks, @romainl. Yes, I do need to assign %:p:h to a persistent variable because tabdo windo cd %:p:h) causes % to be interpretted differently according to each individual subwindow. My aim is to have all subwindows on the same present working directory. Since environmental variables are substituted in VimScript commands, that would do the trick. From the perspective of expediency, however, I may just resort to using a register. It also keeps the environment unpolluted by non-environment information. Commented Jan 12, 2023 at 19:55
  • However, I would really like to know whether in general there is a way to cause variable substitution in a VimScript command before execution. You said that cd doesn't accept variables, but aside from using environment variables, does VimScript have some mechanism for expanding non-environment variables in a command before execution? Commented Jan 12, 2023 at 19:57
  • 1
    I suppose this is one of the main purpose of execute put a side the possibility to concatenate commands with | that don't support it. Commented Jan 12, 2023 at 20:19
  • 1
    Yes, that's one of the purposes of :execute.
    – romainl
    Commented Jan 12, 2023 at 21:40
  • OK, thanks. I'm going to see if I can mark both this and Vivian's answer as "the" answer. I seem to recall that Stack Exchange limits this, but here goes...darn. No can do..... Commented Jan 13, 2023 at 1:28

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.