Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function for editing command line in ext editor #3627

Closed
wants to merge 3 commits into from
Closed

function for editing command line in ext editor #3627

wants to merge 3 commits into from

Conversation

krader1961
Copy link
Contributor

This implements a standard function and bindings for editing the command
line in an external editor. This feature has been requested multiple
times in the past year with various solutions cut and pasted into those
issues. This change combines the best aspects of those solutions.

Fixes #1215

@@ -991,15 +991,19 @@ Some bindings are shared between emacs- and vi-mode because they aren't text edi

- @key{Control,X} copies the current buffer to the system's clipboard, @key{Control,V} inserts the clipboard contents.

- @key{Alt,D} moves the next word to the <a href="#killring">killring</a>.
- @key{Alt,d} moves the next word to the <a href="#killring">killring</a>.
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth splitting these other minor changes into a separate commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

- @key{Alt,W} prints a short description of the command under the cursor.
- @key{Alt,w} prints a short description of the command under the cursor.

- @key{Alt,e} Edit the current command line in an external editor defined by the `$EDITOR` env var, else vim if it is in `$PATH`, else emacs if it is in `$PATH`, else an error is displayed.
Copy link
Member

Choose a reason for hiding this comment

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

Suggest: @key{Alt,e} edit [lowercase] the current command line in an external editor. The editor is chosen from the first available of the $EDITORvariable, the$VISUALvariable [if we also add this], or thevimoremacs` commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


# Edit the command line with the users preferred editor or vim or emacs.
commandline -b >$f
if set -q EDITOR
Copy link
Member

Choose a reason for hiding this comment

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

Probably worth checking $VISUAL here as well? I know it is old school and most people have both set, but there's probably a few people who will be tripped up by it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like people who know about both env vars are likely to set EDITOR to something that will work within a terminal and VISUAL to something (e.g., gvim) that will launch a GUI editor in the background. The latter isn't compatible with this function.

Copy link
Member

Choose a reason for hiding this comment

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

OK, sounds good.

Copy link
Member

Choose a reason for hiding this comment

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

I get the feeling that we will get a PR adding it anyway. See #2268 doing that for funced.

Wasn't $VISUAL historically "an editor that displays the text while you are editing it", i.e. not an ed-like?

emacs $f
else
echo
echo (_ 'Sorry, but you did not define $EDITOR and I could not find vim or emacs.')
Copy link
Member

Choose a reason for hiding this comment

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

In general, I think we should be trying for non-first-person error messages.

How about: "External editor requested but $EDITOR / $VISUAL not set, vim or emacs not available."
"Try setting the EDITOR variable to a text editor, or installing vim or emacs."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Kurtis Rader added 2 commits December 6, 2016 21:08
This implements a standard function and bindings for editing the command
line in an external editor. This feature has been requested multiple
times in the past year with various solutions cut and pasted into those
issues. This change combines the best aspects of those solutions.

Fixes #1215
function edit_command_buffer --description 'Edit the command buffer in an external editor'
set -l f (mktemp)
if set -q f[1]
mv $f $f.fish
Copy link
Member

Choose a reason for hiding this comment

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

I think you know that this is racy. Though it seems a rather theoretical criticism since this function won't be executed in a tight loop by multiple shells at the same time.

Any fix to this would require either an alternative to mktemp or a mktemp that accepts a suffix, which is unlikely to be available on BSD or macOS.

Copy link
Member

Choose a reason for hiding this comment

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

In psub, we use mktemp -d and then add a file with a suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even if this were executed in a tight loop it is so improbable there would be collision that it can be safely ignored. It's comparable to getting two commits in the fish project with the same leading eight digits of the commit hash.

# Set the command to the output of the edited command and move the cursor to the
# end of the edited command.
commandline -r (cat $f)
commandline -C 999999
Copy link
Member

Choose a reason for hiding this comment

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

We should really have a nicer way to set the cursor to the end, and commandline should probably also accept stdin.

Copy link
Member

Choose a reason for hiding this comment

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

We should really have a nicer way to set the cursor to the end,

-1 perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@flaom, Perhaps you should open a new issue, and take ownership of it, rather than commenting on a closed PR where your proposal has zero chance of being implemented?

else if command -s vim >/dev/null
vim $f
else if command -s emacs >/dev/null
emacs $f
Copy link
Member

Choose a reason for hiding this comment

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

Maybe nano and vi as well? That should be enough for 99.9% of all systems.

Copy link
Member

Choose a reason for hiding this comment

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

Also, since 121109e, we have command -sq, so this could just be command -sq emacs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I'm going to add the check for $VISUAL and remove the hardcoded checks for vim and emacs. I've decided that dropping the user into what they might perceive as a randomly chosen editor is a bad idea.

return 1
end

if test $status -eq 0 -a -s $f
Copy link
Member

Choose a reason for hiding this comment

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

Maybe comment that this "$status" is the status of the editor, so we need to be careful not to break that pattern here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


# Edit the command line with the users preferred editor or vim or emacs.
commandline -b >$f
if set -q EDITOR
Copy link
Member

Choose a reason for hiding this comment

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

I get the feeling that we will get a PR adding it anyway. See #2268 doing that for funced.

Wasn't $VISUAL historically "an editor that displays the text while you are editing it", i.e. not an ed-like?

@krader1961
Copy link
Contributor Author

Closed by merge of commit 59fa04b.

@GaryFurash
Copy link

was this ever implemented?

@xieyuheng
Copy link

how to disable this feature ?

@xieyuheng
Copy link

how to disable default alt-v alt-e keybinding ?

@faho
Copy link
Member

faho commented Nov 23, 2017

@xieyuheng: The same you'd disable any other binding - by either overwriting it with something else, or erasing it.

Add this to a function called fish_user_key_bindings:

bind -e \ee
bind -e \ev

However I'm not sure why you'd want this? Do you have an issue with it? Do you just press these keys accidentally?

@xieyuheng
Copy link

thanks @faho
I do not need this feature,
because I can open an emacsclient window by a keybinding (Alt-shift-x),
and edit commend there.

and I also often accidentally press Alt-v.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
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.

support editing the command line with an external editor
6 participants