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.
Best Answer
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