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

Closed
opened this Issue Dec 30, 2013 · 18 comments

Projects
None yet
9 participants

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

### 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/fish-shell/fish-shell/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.
Member

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

### 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?
Contributor

### maxfl commented Jan 15, 2014

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

### 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?
Contributor

### maxfl commented Sep 1, 2015

 It's because of #2210.

### mfilej commented Sep 1, 2015

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

Closed

Contributor

### 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 Edited 1 time braham-snyder edited Dec 6, 2016 (most recent)

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

Contributor

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

 function for editing command line in ext editor 
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
 986dcf0 

### krader1961 referenced this issue Dec 7, 2016

Closed

#### function for editing command line in ext editor #3627

 function for editing command line in ext editor 
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
 d6fb385