Ubuntu – Piping Terminal command to Gedit for further editing

12.04command linegedit

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.

Best Answer

  • 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