1

Often in my scripts I have some ugly snippets like the following:

vim9script
var bin = ""
if has("win32")
    bin = "\\bin"
else
    bin = "/bin"
endif

g:path_with_bin = g:path .. bin

The same happens when it comes to environment variables since they are expressed as $MY_VARIABLE syntax in OSX and Linux systems and %MY_VARIABLE% in Windows (at least when using cmd.exe).

I am wondering if there are functions that recognise the OS and prevent us to explicitely use conditionals, something like

g:path_with_bin = g:path .. adjust_slashes(bin)

that sets slash-backslashes automatically depending on the OS, or, for environment variables, something like the following:

env_var_set(MY_VARIABLE, value)

which set $MY_VARIABLE or %MY_VARIABLE% to value depending on the OS.

4
  • 2
    All that typing instead of a simple :h env<C-d>
    – romainl
    Commented May 25, 2023 at 14:12
  • Could you tell us a bit more about what you want to achieve? I believe there are numerous cases where you can use the Unix syntax on both Linux and Windows. The exception being creating a some Windows command line statement but here also Windows accept very often the Unix syntax. An example would be helpful. Commented May 25, 2023 at 14:32
  • 1
    @VivianDeSmedt nothing more and nothing less than what I described: setting paths and environment variables. That is all. I accepted your answer as it solved both of my problems. Thanks.
    – Barzi2001
    Commented May 25, 2023 at 14:59
  • 1
    Thanks for the feedback :-) Commented May 25, 2023 at 15:02

1 Answer 1

2

For environment variable you can use within Vim the Unix syntax:

:echo $foo
let $foo = 'bar'

For the path I believe you can also use the Unix syntax (/ separated path) for all the Vim API.

2
  • Hm, I wonder if this works for :let $PATH = … to convert / in on Windows? I don't have a Windows machine to test
    – D. Ben Knoble
    Commented May 26, 2023 at 12:52
  • Vim will not convert but Windows will make the best of a path containing / instead of `. The only problem I see is that the dos command don't accept path containing /` since they are interpreted as options. But if you are making dos command in a Vim script I suppose you should encapsulate it in if has('win32') anyway. Commented May 26, 2023 at 13:00

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.