-
Hi. In iTerm2 I can CMD+click any path and it opens in my editor using What I managed to do is add a simple (and probably buggy) way to detect paths and it opens it in the gnome text editor. My question is can I open it in the editor of my choice? Here's my changes to return {
hyperlink_rules = {
{
regex = "(/[^/ ]*)+/?",
format = "$0",
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
wez
Mar 10, 2021
Replies: 1 comment
-
Yes, but it's a little bit involved because you probably also want Here's a config file that:
local wezterm = require 'wezterm';
-- Use some simple heuristics to determine if we should open it
-- with a text editor in the terminal.
-- Take note! The code in this file runs on your local machine,
-- but a URI can appear for a remote, multiplexed session.
-- WezTerm can spawn the editor in that remote session, but doesn't
-- have access to the file locally, so we can't probe inside the
-- file itself, so we are limited to simple heuristics based on
-- the filename appearance.
function editable(filename)
-- "foo.bar" -> ".bar"
local extension = filename:match("^.+(%..+)$")
if extension then
-- ".bar" -> "bar"
extension = extension:sub(2)
wezterm.log_info(string.format("extension is [%s]", extension))
local binary_extensions = {
jpg = true,
jpeg = true,
-- and so on
}
if binary_extensions[extension] then
-- can't edit binary files
return false
end
end
-- if there is no, or an unknown, extension, then assume
-- that our trusty editor will do something reasonable
return true
end
function extract_filename(uri)
local start, match_end = uri:find("$EDITOR:");
if start == 1 then
-- skip past the colon
return uri:sub(match_end+1)
end
-- `file://hostname/path/to/file`
local start, match_end = uri:find("file:");
if start == 1 then
-- skip "file://", -> `hostname/path/to/file`
local host_and_path = uri:sub(match_end+3)
local start, match_end = host_and_path:find("/")
if start then
-- -> `/path/to/file`
return host_and_path:sub(match_end)
end
end
return nil
end
wezterm.on("open-uri", function(window, pane, uri)
local name = extract_filename(uri)
if name and editable(name) then
-- Note: if you change your VISUAL or EDITOR environment,
-- you will need to restart wezterm for this to take effect,
-- as there isn't a way for wezterm to "see into" your shell
-- environment and capture it.
local editor = os.getenv("VISUAL") or os.getenv("EDITOR") or "vi"
-- To open a new window:
local action = wezterm.action{SpawnCommandInNewWindow={
args={editor, name}
}};
-- To open in a pane instead
--[[
local action = wezterm.action{SplitHorizontal={
args={editor, name}
}};
]]
-- and spawn it!
window:perform_action(action, pane);
-- prevent the default action from opening in a browser
return false
end
end)
return {
hyperlink_rules = {
-- These are the default rules, but you currently need to repeat
-- them here when you define your own rules, as your rules override
-- the defaults
-- URL with a protocol
{
regex = "\\b\\w+://(?:[\\w.-]+)\\.[a-z]{2,15}\\S*\\b",
format = "$0",
},
-- implicit mailto link
{
regex = "\\b\\w+@[\\w-]+(\\.[\\w-]+)+\\b",
format = "mailto:$0",
},
-- new in nightly builds; automatically highly file:// URIs.
{
regex = "\\bfile://\\S*\\b",
format = "$0"
},
-- Now add a new item at the bottom to match things that are
-- probably filenames
{
regex = "\\b\\S*\\b",
format = "$EDITOR:$0"
},
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sqbell
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, but it's a little bit involved because you probably also want
file://
URIs to open as well, and you probably don't want to try to edit eg: jpeg images.Here's a config file that:
$EDITOR:
URI scheme$EDITOR:filename
$EDITOR:
andfile://
URIs, decide if it should be opened in the editor, and then spawns a window with that command (you can also spawn in a pane or a tab)