function for editing command line in ext editor#3627
Conversation
| - @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>. |
There was a problem hiding this comment.
Might be worth splitting these other minor changes into a separate commit?
| - @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. |
There was a problem hiding this comment.
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.
|
|
||
| # Edit the command line with the users preferred editor or vim or emacs. | ||
| commandline -b >$f | ||
| if set -q EDITOR |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.') |
There was a problem hiding this comment.
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 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
In psub, we use mktemp -d and then add a file with a suffix.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
We should really have a nicer way to set the cursor to the end, and commandline should probably also accept stdin.
There was a problem hiding this comment.
We should really have a nicer way to set the cursor to the end,
-1 perhaps?
There was a problem hiding this comment.
@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 |
There was a problem hiding this comment.
Maybe nano and vi as well? That should be enough for 99.9% of all systems.
There was a problem hiding this comment.
Also, since 121109e, we have command -sq, so this could just be command -sq emacs.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Maybe comment that this "$status" is the status of the editor, so we need to be careful not to break that pattern here.
|
|
||
| # Edit the command line with the users preferred editor or vim or emacs. | ||
| commandline -b >$f | ||
| if set -q EDITOR |
There was a problem hiding this comment.
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?
|
Closed by merge of commit 59fa04b. |
|
was this ever implemented? |
|
how to disable this feature ? |
|
how to disable default alt-v alt-e keybinding ? |
|
@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 bind -e \ee
bind -e \evHowever I'm not sure why you'd want this? Do you have an issue with it? Do you just press these keys accidentally? |
|
thanks @faho and I also often accidentally press Alt-v. |
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