You are here
gEdit's Amazing External Tools
In a few recent conversations I have become aware of an unawareness - an unawareness of the awesome that is gedit's best feature: External Tools. External Tools allow you to effortlessly link the power of the shell, Python, or whatever into an otherwise already excellent text editor yielding maximum awesome. External Tools, unlike some similar features in many IDEs is drop-dead simple to use - you do not need to go somewhere and edit files, etc... you can create and use them without ever leaving the gedit UI.
Plugins tab of the Preferences dialog.
To enable External Tools [which is a plugin - as is nearly every feature in gedit] go to the Plugin tab of Preferences dialog and check the box for "External Tools". External Tools is now active. Close the dialog and proceed in defining the tools useful to you.
With External Tools enabled there will be a "Manage External Tools..." option in the global menu. When in the Tools menu not there is also an "External Tools" submenu - every external tool you define will be available in the menu, automatically. The list of defined tools in that submenu will also include whatever hot-key you may have bound to the tool - as you likely will not remember at first.
Manage External Tools Dialog
Within the Manage External Tools dialog you can start defining what tools are useful to you. For myself the most useful feature is the ability to perform in-place transformations of the current document; to accomplish this set input to "Current Document" and Output to "Replace Current Document". With that Input & Output the current document is streamed to your defined tool as standard input and the standard output from the tool replaces the document. Don't worry - Undo [Ctrl-Z] still works if your tool did not do what you desired.
What are some useful External Tools? That depends on what type of files and data you deal with on a regular basis. I have previously written a post about turning a list of value into an set format - that is useful for cut-n-paste into either an SQL tool [for use as an IN clause] or into a Python editor [for x=set(....)]. That provides a simple way to take perhaps hundreds of rows and get them into data very simply.
Otherwise some tools I find useful are:
Format JSON to be nicely indented
#!/bin/sh
python -m json.tool
Use input/output settings to replace current document.
Open a terminal in the directory of the document
#!/bin/sh
gnome-terminal --working-directory=$GEDIT_CURRENT_DOCUMENT_DIR &
Set the input/ouput for this action to "Nothing"
Remove leading spaces from lines
#!/bin/sh
sed 's/^[[:blank:]]*//'
Use input/output settings to replace current document.
Remove trailing spaces from lines
#!/bin/sh
sed 's/[[:blank:]]*$//'
Use input/output settings to replace current document.
Keep only unique lines of the file
#!/bin/sh
sort | uniq
Use input/output settings to replace current document.
Format an XML file with nice indentation
#!/bin/sh
xmllint --format - -
Use input/output settings to replace current document.
IN Clause Generator
This takes a document with one value per line and converts it to an SQL like IN clause. The output is also appropriate for creating Python set values.
#!/usr/bin/env python
import sys
iteration = 0
line_length = 0
text = sys.stdin.readline()
while (text != ''):
text = text.strip()
if (len(text) > 0):
if (iteration == 0):
sys.stdout.write('(')
else:
sys.stdout.write(', ')
if (line_length > 74):
sys.stdout.write('\n ')
line_length = 0
if (len(text) > 0):
sys.stdout.write('\'{0}\''.format(text))
line_length = line_length + len(text) + 4
iteration = iteration + 1
text = sys.stdin.readline()
sys.stdout.write(')')
sys.stdout.flush()
Input is "Current document", output is "Replace current document".