/ fish-shell Public

# support editing the command line with an external editor#1215

Closed
opened this issue Dec 30, 2013 · 20 comments
Closed

# support editing the command line with an external editor #1215

opened this issue Dec 30, 2013 · 20 comments
Labels
enhancement release notes Something that is or should be mentioned in the release notes
Milestone

### lanterndev commented Dec 30, 2013

 Thanks for sharing, @maxfl. When I try that though I get: usage: mktemp [-d] [-q] [-t prefix] [-u] template ... mktemp [-d] [-q] [-u] -t prefix fish: Expected redirection specification, got token of type '$f' /Users/_pants/.config/fish/config.fish (line 5): commandline -b >$f ^ in function 'edit_cmd', called on standard input,  I'm on OS X 10.9.1 + homebrew fish version 2.1.0.

### maxfl commented Dec 30, 2013

 @skivvies, 'mktmp' should return unique name for the temp filename. You can replace that line with (for example): set -l f /tmp/fish.cmd.(random) On Mon, Dec 30, 2013 at 9:44 PM, skivvies notifications@github.com wrote: Thanks for sharing, @maxfl https://github.com/maxfl. When I try that though I get: usage: mktemp [-d] [-q] [-t prefix] [-u] template ... mktemp [-d] [-q] [-u] -t prefix fish: Expected redirection specification, got token of type '$f' /Users/_pants/.config/fish/config.fish (line 5): commandline -b >$f ^ in function 'edit_cmd', called on standard input, I'm on OS X 10.9.1 + homebrew fish version 2.1.0. — Reply to this email directly or view it on GitHubhttps://github.com//issues/1215#issuecomment-31357797 .

### lanterndev commented Dec 30, 2013

 Ah, BSD (OS X's) mktemp doesn't accept a --tmpdir argument. This works though:  function edit_cmd --description 'Input command in external editor' set -l f (mktemp /tmp/fish.cmd.XXXXXXXX) if test -n "$f" set -l p (commandline -C) commandline -b >$f vim -c 'set ft=fish' $f commandline -r (more$f) commandline -C $p command rm$f end end function fish_user_key_bindings bind \ev 'edit_cmd' end Thanks for the tip, @maxfl.

### zanchey commented Jan 6, 2014

 I think this would be worth including, although we would have to use $EDITOR or$VISUAL rather than hard-encoding vim.

### zanchey commented Jan 15, 2014

 I note that in Bash, C-x C-e executes the command when the editor exits, so we actually need to bind edit_cmd; commandline -f execute. Any suggestions for a default binding? We don't support multi-character sequences yet, so C-x C-e is out. M-e?

### maxfl commented Jan 15, 2014

 I use M-e and F4 bindings. They seem not to intersect with anything.

### mrak commented May 30, 2014

 Thanks all! Super useful

### mfilej commented Sep 1, 2015

 @maxfl's snippet seems to have stopped working for me at some point. Did anything change in fish?

### maxfl commented Sep 1, 2015

 It's because of #2210.

### mfilej commented Sep 1, 2015

 Thanks! I'll keep an eye on that then.

mentioned this issue Jan 13, 2016
mentioned this issue Mar 20, 2016

### braham-snyder commented Dec 6, 2016

 something like this can be useful for completion, etc., depending on your editor: set -l tmpfile (mktemp --suffix=".fish" --tmpdir=.); or return 1  if you're on OS X, you'll need homebrew's gmktemp (available in the formula coreutils, I think)

changed the title support input via external editor support editing the command line with an external editor Dec 6, 2016

### krader1961 commented Dec 6, 2016

 In the past year I've seen several solutions documented in other issues. The one I've been using for a long time is an enhanced version of @skivvies that works whether you're using BSD or GNU mktemp: 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 set f $f.fish else # We should never execute this block but better to be paranoid. set f /tmp/fish.(echo %self).fish touch$f end set -l p (commandline -C) commandline -b > $f if set -q EDITOR eval$EDITOR $f else vim$f end commandline -r (cat $f) commandline -C$p command rm \$f end  Something like that should be bundled with fish. What the default bindings should be is TBD. I use \ee ([meta-e] and \ev ([meta-v] which matches the ksh binding in vi mode that I was used to using). It would be nice if we could get some people to try this and report whether or not it works on their system.

### krader1961 commented Dec 6, 2016

 Also from my comment in issue #3477 regarding the function I just mentioned: It's not perfect. For example, you get a weird but harmless error if you save an empty buffer when exiting the editor. It really should also check the exit status of the editor and use a non-zero status to mean the editors buffer shouldn't be used (like git commit does). But the point is that if you're doing non-trivial editing of the command line you're probably better off using a real editor than adding a bunch of fish key bindings that will never make fish truly behave like your editor.

### braham-snyder commented Dec 6, 2016 • edited

 @krader1961 works for me on El Capitan with emacsclient (with that weird but harmless error when saving an empty command)

self-assigned this Dec 6, 2016

### krader1961 commented Dec 6, 2016

 This question and various solutions has occurred often enough that I decided it was time to make my solution more robust. I've modified the function I posted above to make it more robust and will open a pull request to invite review of it.

closed this as completed in  59fa04b  Dec 9, 2016
modified the milestones: fish 2.5.0, fish-future Dec 9, 2016
added the release notes Something that is or should be mentioned in the release notes label Dec 10, 2016

### hashier commented Sep 19, 2018

 To everyone who started to copy/paste the solution of @krader1961 like me. This is not needed anymore. This got merged into fish and got even two shortcuts. Alt-e and Alt-v. Documentation: https://fishshell.com/docs/current/ (search for "edit the current command line in an external editor") PR: #3627

### dniku commented Mar 28, 2019

 Also, Alt-e and Alt-v are equivalent according to the documentation.

bot locked as resolved and limited conversation to collaborators Apr 17, 2020