Skip to content

Open command script in external editor on Alt+o#10266

Closed
krobelus wants to merge 1 commit into
fish-shell:masterfrom
krobelus:edit-scripts-faster
Closed

Open command script in external editor on Alt+o#10266
krobelus wants to merge 1 commit into
fish-shell:masterfrom
krobelus:edit-scripts-faster

Conversation

@krobelus

Copy link
Copy Markdown
Contributor

Fish functions are great for configuring fish but they don't integrate
seamlessly with the rest of the system. For tasks that can run outside fish,
writing scripts is the natural approach.

To edit my scripts I frequently run

$EDITOR (which my-script)

Would be great to reduce the amount typing for this common case (the names
of editor and scripts are usually short, so that's a lot of typing spent on
the boring part).

Our Alt+o binding opens the file at the cursor in a pager. When the cursor
is in command position, it doesn't do anything (unless the command is actually
a valid file path). Let's make it open the resolved file path in an editor.

Also enable this functionality in Alt+e. This is experimental, I will probably
drop it. The motivation is that when the cursor is on the command token of a
single-process commandline, the existing Alt+e behavior is probably not very
helpful. So maybe we can improve discoverability of this feature by plugging
it in here. Though I'm not sure how many users would even try Alt+e on the
command token.

In future, we should teach this binding to delegate to "funced" upon seeing
a function instead of a script. I didn't do it yet because funced prints
messages, so it will mess with the commandline rendering if used from
a binding. (The fact that funced encourages overwriting functions that
ship with fish is worrysome. Also I'm not sure why funced doesn't open the
function's source file directly (if not sourced from stdin). Persisting the
function should probably be the default.)

Alternative approach: I think other shells expand "=my-script" to
"/path/to/my-script". That is certainly an option -- if we do that we'd want
to teach fish to complete command names after "=". Since I don't remember
scenarios where I care about the full path of a script beyond opening it in
my editor, I didn't look further into this.

@krobelus krobelus force-pushed the edit-scripts-faster branch from 6534d17 to bd32138 Compare January 27, 2024 08:38
Fish functions are great for configuring fish but they don't integrate
seamlessly with the rest of the system. For tasks that can run outside fish,
writing scripts is the natural approach.

To edit my scripts I frequently run

    $EDITOR (which my-script)

Would be great to reduce the amount typing for this common case (the names
of editor and scripts are usually short, so that's a lot of typing spent on
the boring part).

Our Alt+o binding opens the file at the cursor in a pager.  When the cursor
is in command position, it doesn't do anything (unless the command is actually
a valid file path). Let's make it open the resolved file path in an editor.

Also enable this functionality in Alt+e.  This is experimental, I will probably
drop it. The motivation is that when the cursor is on the command token of a
single-process commandline, the existing Alt+e behavior is probably not very
helpful. So maybe we can improve discoverability of this feature by plugging
it in here. Though I'm not sure how many users would even try Alt+e on the
command token.

In future, we should teach this binding to delegate to "funced" upon seeing
a function instead of a script. I didn't do it yet because funced prints
messages, so it will mess with the commandline rendering if used from
a binding.  (The fact that funced encourages overwriting functions that
ship with fish is worrysome. Also I'm not sure why funced doesn't open the
function's source file directly (if not sourced from stdin). Persisting the
function should probably be the default.)

Alternative approach: I think other shells expand "=my-script" to
"/path/to/my-script".  That is certainly an option -- if we do that we'd want
to teach fish to complete command names after "=".  Since I don't remember
scenarios where I care about the full path of a script beyond opening it in
my editor, I didn't look further into this.
or return 1
test -w $command_path
or return 1
string match -q 'text/*' (file --brief --mime-type -- $command_path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file would be a new dependency, we should probably error out if it doesn't exist.

An alternative is to look at the first line and see if it's a shebang.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL file is POSIX but the specified version doesn't have any of the options we need.
But it seems everyone uses the same implementation (except for OpenBSD but it's still compatible).
I'm leaning towards adding it to the dependencies, and print an error if it fails here. (We can still early-return though it makes no functional difference).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general long options aren't POSIX.

I'm fine with relying on file, I would just like a proper error here.

else
echo
echo (_ 'External editor requested but $VISUAL or $EDITOR not set.')
echo (_ 'Please set VISUAL or EDITOR to your preferred editor.')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason this doesn't just go to stderr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, stderr is better here

@faho

faho commented Jan 27, 2024

Copy link
Copy Markdown
Member

Alternative approach: I think other shells expand "=my-script" to
"/path/to/my-script". That is certainly an option -- if we do that we'd want
to teach fish to complete command names after "=". Since I don't remember
scenarios where I care about the full path of a script beyond opening it in
my editor, I didn't look further into this.

I have, for a laugh, written an abbreviation that does this. I would be okay with shipping that by default after we hash out how default abbrs would work.

I would be quite against making it core syntax, and it would be an awkward compatibility break to make = special in that regard.

@faho faho added this to the fish next-3.x milestone Jan 27, 2024
@krobelus krobelus closed this in 47aa798 Feb 6, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants