Open reidprichard opened 10 months ago
Got it working using this code:
open_in_terminal = function(state)
local node = state.tree:get_node()
local path = node:get_id()
path = path:gsub(" ", "\\ ")
local the_cmd = string.format([[1TermExec cmd="cd %s"]], path)
vim.cmd(the_cmd)
print("Opening terminal in " .. tostring(path))
end,
I'm looking for a solution to a broader problem, namely I want to execute the currently opened file in terminal:
:TermExec cmd='./%'
but this fails if filename contains any shell special characters, not just spaces but also things like ' " $
etc. Is there a way to pass a shell-escaped filename to cmd=
?
Can't you just surround the filename in quotes?
Can't you just surround the filename in quotes?
No, that won't work for cases when "
is part of the filename, and will also try to expand variables if filename contains something like $foo
.
The best way to escape would be to wrap the whole name in '
, and change every '
to '\''
. The shellescape()
function can do it, but I know nothing about Lua myself.
Oh, duh. Here's my code to run the current file as a Python script. I don't specially handle anything but spaces, but hopefully this is a useful starting point. I wasn't aware of the shellescape()
function, but I think you could just switch it out for my gsub
call.
local function run_python()
vim.cmd.write()
print("Running...")
local script_path = vim.fn.expand("%:p"):gsub(" ", "\\ ")
local command = "python \"" .. script_path .. "\""
toggleterm.exec(command)
end
Note that this (being Lua) would need to be in your nvim config and assigned a keymap.
I run Windows, and due to organizational constraints I'm forced to have spaces in my directories. I frequently want to execute the open Python file with
TermExec cmd="python %"
. As you can imagine, this causes issues if%
expands to a path with a space, as you get e.g.:I've tried every which way to escape quotes, mixing single and double quotes, escaping spaces with backslashes/backticks, etc., but I'm getting nowhere. Using the
dir
argument toTermExec
works if the terminal is not open yet, but it will not change the working directory if there's an open terminal already.Thoughts?