External Tools

 

Often you would like to have a feature in gedit like, Compile when F7 is pressed, Compile and Run when F8 is pressed and more and more. One simple (grrr…) solution for this is to write our own plugins. But to write a plugin for gedit in python, you need to know pygtk and of course python. A simple linux user need not know all these and hence will go for some other editor which provides these features. External Tools is a plugin which enables you to make gedit do anything. Yeah ANYTHING.

The great news is that External Tools plugin is comes with the gedit. So no need to download or install anything. Just Enable the External Tools plugin and you can configure the plugin either by clicking Configure Plugin in the Plugins tab or choosing Tools -> External Tools… from the menu bar.

 

Gedit External Tools

 

Gedit menu

 

The External Tools manager window is the place where you add your custom scripts. The manager window looks like,

External Tools Manager

As you can see, every tool has a
Name: A unique string to identify itself
Description: A small description about your tool
Shortcut Key: Set the keys to be pressed so that the tool is run
Commands: The actual commands
Input: Input from the current tool
Output: What to do with the output of the tool
Applicability: To what documents is this tool applicable.

Now let us write a simple tool to compile a C/C++ program when Control F7 is pressed. We’ll assume that the filename ends with .c or .cpp extension and there is no ‘.’ in the filename.

  • Click the New Button in the External Tools Manager
  • Rename the New tool to ‘Compile C/C++ Code’
  • Change the Description to ‘Compiles the C/C++ Program when Ctrl F7 is pressed.
  • Click in the shortcut key region and press Control F7. This sets the shortcut key for this tool as Control F7.
  • Paste this code in the Commands

#!/bin/bash
# Store the file name in a variable
FILE_NAME=$GEDIT_CURRENT_DOCUMENT_NAME
# Check if file extension is .cc
if [ `echo $FILE_NAME | cut -d "." -f 2` = "cc" ]
then
FILE_NAME_LEN=`expr ${#FILE_NAME} - 3`
FILE_NAME_BASE=${FILE_NAME:0:$FILE_NAME_LEN}
make $FILE_NAME_BASE
fi
# Check if the file extension is c
if [ `echo $FILE_NAME | cut -d "." -f 2` = "c" ]
then
FILE_NAME_LEN=`expr ${#FILE_NAME} - 2`
FILE_NAME_BASE=${FILE_NAME:0:$FILE_NAME_LEN}
make $FILE_NAME_BASE
fi
exit 0

  • Set the Input to ‘Nothing’
  • Set the Output to ‘Display in bottom pane’
  • Set the Applicability to ‘All Documents’

Before we explore the code, let us see a few useful variables which can be used in the commands region
$GEDIT_CURRENT_DOCUMENT_URI
$GEDIT_CURRENT_DOCUMENT_NAME
$GEDIT_CURRENT_DOCUMENT_SCHEME
$GEDIT_CURRENT_DOCUMENT_PATH
$GEDIT_CURRENT_DOCUMENT_DIR
$GEDIT_DOCUMENTS_URI
$GEDIT_DOCUMENTS_PATH

As you can see, the last two variables are applicable when you do some action for all the tabs opened. Since we are interested in compiling only the current tab, we need to use only $GEDIT_CURRENT_DOCUMENT_NAME.
For a file named deer.txt in my home(/home/hari/) directory, the variables are set as

Variable Name Value
$GEDIT_CURRENT_CURRENT_DOCUMENT_URI file:///home/hari/deer.txt
$GEDIT_CURRENT_CURRENT_DOCUMENT_NAME deer.txt
$GEDIT_CURRENT_CURRENT_DOCUMENT_SCHEME file
$GEDIT_CURRENT_CURRENT_DOCUMENT_PATH /home/hari/deer.txt
$GEDIT_CURRENT_CURRENT_DOCUMENT_DIR /home/hari
$GEDIT_CURRENT_DOCUMENTS_URI file:///home/hari/deer.txt
$GEDIT_CURRENT_DOCUMENTS_PATH /home/hari/deer.txt

The script is pretty obvious. It takes the file name (say deer.c) and cuts the file name using ‘.’ as the seperator. If the second field is c, then it removes the last two characters in the file name (which gives deer) and compiles it using the make command.

That gives us so much possibilties with gedit. You can compile, compile and run a C/C++ script, run a PHP, Perl, Python within your gedit.

If you don’t know how to write shell scripts don’t panic. Any other language like python, perl, php will run there. You can also share the tools files with your friends ๐Ÿ™‚ :). All you need is the tools folder in ~/.gnome2/gedit/. A zip file of my tools folder can be downloaded here. It contains scripts to

  • Compile C/C++
  • Run C/C++/Python/PHP/Perl/Shell/
  • Check PHP Syntax
  • Indent the current file

There are few things which External Tools can’t do. Like changing only a specific text in the buffer. You need to write your own plugin for those compicated issues. If you’re planning to write a plugin for gedit, check out the next post which explains about a simple HelloWorld Plugin which replaces all ‘Hello’s in the text by ‘World’.

7 thoughts on “External Tools

  1. Where does it target the file.
    This code throws an error:
    make: *** No rule to make target `source.’. Stop.
    Input file not found

  2. I tried that compile-and-run code here and it’s working fine. I don’t know why it’s showing make error. Maybe you can try to echo $FILE_NAME_BASE and see. Your file extension should be c or cc ( cpp not allowed ๐Ÿ™ )
    Also you don’t have an input file. Give that after the make command works ๐Ÿ™‚

  3. Pingback: NAzT

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.