F5 to Execute Your Program from Gedit – From Go to Python – Gedit External Tools


Gedit editor can run any command or script for you. Even though Gedit looks very simple, it’s easy to integrate Gedit to your own toolchains.

Here is a simple script to execute your program by pressing F5. The script handles differences between compiled languages (go) and scripts (Python). It’s also a starting point for building your own scripts.

External Tools Runs Commands for You

Menu: Preferences: Plugins: External Tools: Enable.
If you don’t have “External tools”, you might need to ‘sudo apt-get install’ some gedit plugins packages.
A new menu item is added: “Manage External Tools…”

Get your Environment

Write a simple tool to run ‘set’, with output “Display in bottom pane”.

GEDIT_CURRENT_DOCUMENT_DIR=/home/tero/bla/bla/bla/modifyParameter
GEDIT_CURRENT_DOCUMENT_NAME=modifyParameter.go
GEDIT_CURRENT_DOCUMENT_PATH=/home/tero/bla/bla/bla/modifyParameter/modifyParameter.go
GEDIT_CURRENT_DOCUMENT_SCHEME=file
GEDIT_CURRENT_DOCUMENT_TYPE=text/x-go
GEDIT_CURRENT_DOCUMENT_URI=file:///home/tero/bla/bla/bla//modifyParameter/modifyParameter.go
GEDIT_CURRENT_LINE='}'
GEDIT_CURRENT_LINE_NUMBER=9
GEDIT_CURRENT_WORD=
GEDIT_CURRRENT_DOCUMENT_LANGUAGE=go
GEDIT_CWD=/home/tero
GEDIT_DOCUMENTS_PATH=/home/tero/bla/bla/bla/modifyParameter/modifyParameter.go
GEDIT_DOCUMENTS_URI=file:///home/tero/bla/bla/bla//modifyParameter/modifyParameter.go
GEDIT_FILE_BROWSER_ROOT=/home/tero/bla/bla/bla//modifyParameter

These are just GEDIT* variables. Obviously, you can use the others, too.

F5 to Run Your Code

With scripting languages, it’s pretty obvious. Just make your script runnable ‘chmod u+x tero.py’, add a shebang “#!/usr/bin/python3” and you can execute it. But what about go, where you run your script with ‘go run tero.go’? For a tiny script, it would feel overkill to write a Makefile. External tools to rescue!

#!/bin/bash
if [ "go" = "$GEDIT_CURRRENT_DOCUMENT_LANGUAGE" ]
then
 go run $GEDIT_CURRENT_DOCUMENT_PATH
else
 $GEDIT_CURRENT_DOCUMENT_PATH
fi

By applying this example, you can write more ifs if you need more languages.
What are you going to automate next? Can you apply the script to Java, Ruby, R, C, C++ (cpp) and Lua?
Happy coding! Go check out a couple of more Gedit articles.

Bonus Trick: Timeout

If you have a tendency to write infinite loops, add ‘timeout’ to your F5 execution command. Timeout kills command after user defined time period (seconds)  has passed. E.g.

$ timeout 5 cat

Adminstrivia

Tested on Xubuntu 18.04 LTS amd64, Gedit 3.28.1-1ubuntu1. And I’ve used similar setup for a long time with older versions of gedit.
Gedit can be used in place of another text editor or an IDE (Integrated Development Environment): Eclipse, Visual Studio Code, VSCode, Notepad++, Notepad, Geany, Brackets, Atom, Sublime text, Mousepad, xemacs, gvim, emacs, vim, nano, vi.
This article has been updated.

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , | Comments Off on F5 to Execute Your Program from Gedit – From Go to Python – Gedit External Tools

Comments are closed.