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

Conversation

@krader1961
Copy link
Contributor

krader1961 commented Dec 7, 2016

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>.

This comment has been minimized.

@zanchey

zanchey Dec 7, 2016 Member

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

This comment has been minimized.

@krader1961

krader1961 Dec 7, 2016 Author Contributor

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.

This comment has been minimized.

@zanchey

zanchey Dec 7, 2016 Member

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.

This comment has been minimized.

@krader1961

krader1961 Dec 7, 2016 Author Contributor

Done.


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

This comment has been minimized.

@zanchey

zanchey Dec 7, 2016 Member

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.

This comment has been minimized.

@krader1961

krader1961 Dec 7, 2016 Author Contributor

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.

This comment has been minimized.

@zanchey

zanchey Dec 7, 2016 Member

OK, sounds good.

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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.')

This comment has been minimized.

@zanchey

zanchey Dec 7, 2016 Member

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."

This comment has been minimized.

@krader1961

krader1961 Dec 7, 2016 Author Contributor

Done.

Kurtis Rader added 2 commits Dec 7, 2016
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
Kurtis Rader
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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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.

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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

This comment has been minimized.

@krader1961

krader1961 Dec 8, 2016 Author Contributor

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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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

This comment has been minimized.

@floam

floam Dec 9, 2016 Member

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

-1 perhaps?

This comment has been minimized.

@krader1961

krader1961 Dec 9, 2016 Author Contributor

@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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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

This comment has been minimized.

@krader1961

krader1961 Dec 8, 2016 Author Contributor

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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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

This comment has been minimized.

@krader1961

krader1961 Dec 8, 2016 Author Contributor

Done.


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

This comment has been minimized.

@faho

faho Dec 7, 2016 Member

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?

Kurtis Rader
@krader1961
Copy link
Contributor Author

krader1961 commented Dec 9, 2016

Closed by merge of commit 59fa04b.

@krader1961 krader1961 closed this Dec 9, 2016
@krader1961 krader1961 added this to the fish 2.5.0 milestone Dec 9, 2016
@krader1961 krader1961 deleted the krader1961:commandline-edit branch Dec 9, 2016
@GaryFurash
Copy link

GaryFurash commented Nov 21, 2017

was this ever implemented?

@xieyuheng
Copy link

xieyuheng commented Nov 22, 2017

how to disable this feature ?

@xieyuheng
Copy link

xieyuheng commented Nov 22, 2017

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

xieyuheng commented Nov 23, 2017

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
Linked issues

Successfully merging this pull request may close these issues.

6 participants
You can’t perform that action at this time.