I'd like to be able to easily/often change my PATH environment variable interactively. In other shells, this is really easy; eg, appending with Zsh:
path+=/some/path
With Nushell, the docs say the equivalent is:
$env.PATH = ($env.PATH | split row (char esep) | append '/some/path')
That's kinda crazy to have to type for an interactive shell. But this also seems to work, and is almost reasonable:
let-env PATH = ($env.PATH|append /some/path)
(BTW, why do docs mention both $env.PATH = ... and let-env PATH = ... approaches? Which is preferred and why?)
To make it more reasonable, it feels something like this is needed (in my config.nu), but I'd like to know: is this approach fine for appending to PATH?
def-env path-append [d] { let-env PATH = ($env.PATH | split row (char esep) | append $d) }
...
path-append /some/path
Additionally, is this the right way to remove an entry from PATH?
$env.PATH # see that entry 37 is unwanted
let-env PATH = ($env.PATH|drop nth 37) # remove 37th entry
# so then this is even better (also in my config.nu)?
def-env path-drop [n] { let-env PATH = ($env.PATH|drop nth $n) }
path-drop 37
BONUS: Zsh has vared to interactively edit things like PATH. Is there any such equivalent in Nu?
Note that the docs you are referring to for adding to the Path are assuming that you are doing it at startup, via
env.nu. The docs also mention that the reason for thesplit row (char esep)step is:And I'm separating out the most important sentence here:
So, interactively (a.k.a., "in the Nushell REPL"), you can shorten it to:
If there's a section of the docs that still refers to
let-env, you should open an issue against that (or a PR), sincelet-envhas been removed from Nushell in recent releases.$envwas made mutable, so there is no reason forlet-envany longer.For interactive use, remove the
split...portion as mentioned above. Also use$env:Sure, that will work, assuming you make the same modifications as above (
$envinstead oflet-env).You might get fancier if you'd like with something like:
Then you could call
path-remove /some/pathNot that I can think of.
$env.PATH | explorewould give you a nice read-only view, but not really any improvement in this case over the default table-view. However, there's no equivalent that I can think of for editing.