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-write-a-long-complex-or-tricky-command
Please note that I'm specifically looking for a way to edit in an external editor.
bash
,zsh
, andksh
(including it's derivatives) have this very neat built in commandfc
, which opens an editor for altering your previous command. If the variableFCEDIT
is not set, by default it will call the editor set inEDITOR
; if the variableEDITOR
is not set, by default it will callnano
.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 callfc
. That will spawngedit
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
ornano
command line editors rather than gedit - those don't leave any trace , besides they can be used inTTY
not just in GUI environment. I strongly suggest you switch tonano
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 inzsh
adding to your.zshrc
Use
gedit -
. This way it will read from stdin, so you can useor simply
This way you won't need to create a separate tmpfile.
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).