Piping Terminal command to Gedit for further editing [duplicate]

Solution 1:

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

Solution 2:

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.