Related to this post, Detect OS in Vimscript
This configuration detects and sets the operating system in Vim. It uses conditional statements to determine if the OS is Windows or Unix-like.
if !exists("g:os")
if has("win64") || has("win32") || has("win16")
let g:os = "Windows"
else
let g:os = substitute(system('uname'), '\n', '', '')
endif
endif
Is there a more efficient or elegant way to achieve this in Lua?
PS: I'm a newbie at this. This is my first try. Please let me know if it needs any fix.
if not vim.g.os then
local is_windows = vim.fn.has("win64") or vim.fn.has("win32") or vim.fn.has("win16")
if is_windows then
vim.g.os = "Windows"
else
local uname_output = vim.fn.system('uname')
vim.g.os = string.gsub(uname_output, '\n', '')
end
end
There's a bug. This code shows Windows in my fedora system.
g:osvariable I suppose on Fedora it should set it toLinux(at least it what it does on Ubuntu). On Windows it set it toWindows. You can verify the variable value with the command:echo g:os– Vivian De Smedt Oct 11 '23 at 06:55E121: Undefined variable: g:os– Mega Bang Oct 11 '23 at 06:59