7

This question already has an answer here:

I have a complex command in my terminal window which I would like to edit further e. g.

echo "This is a complex command I want to edit in an editor"

How can I pipe this line of code - not the output but the actual code - to e.g. Gedit for further editing? Something along the lines of this solution, just for a GUI based editor: http://www.commandlinefu.com/commands/view/1446/rapidly-invoke-an-editor-to-wri‌​te-a-long-complex-or-tricky-command

Please note that I'm specifically looking for a way to edit in an external editor.

marked as duplicate by kos, Ravan, heemayl, Charles Green, Eric Carvalho Dec 10 '15 at 19:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    ^^ Read my answer there, it does exactly what you're asking for. – kos Dec 10 '15 at 14:15
  • Yes, it is a good answer ^^ – somethis Mar 16 '16 at 1:11
13

bash, zsh, and ksh (including it's derivatives) have this very neat built in command fc, which opens an editor for altering your previous command. If the variable FCEDIT is not set, by default it will call the editor set in EDITOR; if the variable EDITOR is not set, by default it will call nano.

What you can do, is to set FCEDIT=/usr/bin/gedit. Now there's the trick: you run a long command, you decide you want to change it, so immediately after you run it call fc. That will spawn gedit window with your command right there ready for altering. Once you're done altering, save and exit as if you normally would.

The disadvantage ? It will leave a trail of unnecessary gtk messages in terminal. Personally, I use vim or nano command line editors rather than gedit - those don't leave any trace , besides they can be used in TTY not just in GUI environment. I strongly suggest you switch to nano as it is one of the easiest command line text editors.

Extra note in bash, you can do the same with the command line your are currently editing with ctrl+X+E or ctrl+X - ctrl+E; you can have the same behavior in zsh adding to your .zshrc

autoload -z edit-command-line
zle -N edit-command-line
bindkey '^XE' edit-command-line # binds CTRL+X+E
bindkey "^X^E" edit-command-line # binds CTRL+X - CTRL+E
  • Sweet, this is just what I was looking for! Thanks to all the other pretty good (and close) answers. – somethis Dec 10 '15 at 14:24
  • Added a little trick --- @Serg, i hope you don't mind. – Rmano Dec 10 '15 at 15:44
  • @Rmano sure, that's a useful addition to the answer and will be helpful for others. Thank you ! – Sergiy Kolodyazhnyy Dec 10 '15 at 15:49
  • 1
    Me too, I added the second binding so that it mimics exactly Bash's behavior, rephrase, rewrite etc; – kos Dec 10 '15 at 16:15
9

Use gedit -. This way it will read from stdin, so you can use

echo 'echo "complex command"'|gedit -

or simply

gedit - <<< 'echo "complex command"'

This way you won't need to create a separate tmpfile.

3

With this command you can achieve that: echo "This is a complex command" > tempfile | gedit tempfile.

Edit:

As I understand it, you want something like this: echo echo "This is a complex command" > tempfile | gedit tempfile. For example, echo ls -d > tempfile | gedit tempfile will open a gedit file with the following text: "ls -d".

That said I would advice against editing commands on Gedit. The Linux terminal is extremely powerful(we have a powerful tab completion).

  • Thank you. Though, in Ubuntu 12.10 with the standard bash terminal this outputs This is a complex command in Gedit ... which is the output not the command itself. – somethis Dec 10 '15 at 11:45
  • Oh, I see what you mean, you want to see this on Gedit: echo "This is a complex command" ? – Tshilidzi Mudau Dec 10 '15 at 11:50
  • Yes, so that I can edit the command further. – somethis Dec 10 '15 at 11:51
  • @somethis, did that command do what you wanted? If so, could you kindly mark as answer(if it is indeed the answer you were looking for), please? – Tshilidzi Mudau Dec 10 '15 at 12:20
  • While this answer works, my suggestion is to write a bash script . You can then edit the script with any editor. For information on bash scripting see linuxcommand.org/lc3_writing_shell_scripts.php . Of course you can script with tools other then bash such as perl, php, awk .... – Panther Dec 10 '15 at 12:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.