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’.

Gedit

Every GNU/Linux user must be knowing about Gedit. This tiny text editor which comes with gnome is much more powerful than we all think. A few customizations and adding a few plugins will convert this simple text editor into a complete IDE for any programming language. In this article, i have mentioned about the configurations i have made in my box and the plugins which you might find useful.

Any text editor has to be customized according to the need of the end user. The customizations for a C or C++ coder and a PHP or Python coder are totally different. So let me write here according to various needs. Check out in which category you fit in and use those customizations.

The Preferences window in Gedit is the core which converts this simple text editor into a complete IDE. It has features to customize almost everything.

Gedit Preferences

General Configurations

Open Preferences Window (Edit -> Preferences)

In the View Tab

  • Check Enable text wrapping.

I prefer to see the long lines in the same window, which reduces the pain of scrolling across the window using the mouse.

  • Check Display line numbers.
  • Check Highlight current line

In my Ubuntu Studio theme, it wasn’t easy to read when the current line is highlighted. So i disabled this in that theme. So check this whether this suits your gnome theme when you enable this.

  • Highlight matching bracket

An useful option if you are used to getting confused with brackets.

In the Editor Tab

  • Set the Tab width as 4
  • Check Enable automatic indentation
  • Uncheck Create a backup copy of files before saving

Again this depends upon your need. Backup files are created by adding a ‘~’ to the current file name. If you are a web developer and coding in any web development language, better don’t enable this option. Many times these ‘~’ files created might create a security hole in your server. For example, mysql_connect.php file will have a backup called mysql_connect.php~ and your web server will parse the mysql_connect.php but it won’t do the same for the backup file. It’ll just display the source code to the client and thereby your your mysql password is gone :P

  • Check Autosave files every 5 minutes and make that 2 mins

If you don’t have the habit of saving the file often, then this might save you when something terrible happens :P

In the Font & Colors Tab

  • Uncheck the Use the system fixed width font
  • Set the size of the editor font as 10 or 12
  • Choose any one font from the following. Note that Mono space 10 is the system fixed with font.

Standars Symbols L
Serif
Vemana
Sans
Monospace
Mallige Normal
Loma Book

  • I prefer the Classic color scheme. Just three more clicks, you can see all the color scheme and choose anything you like.

In the Plugins Tab

 

Gedit Plugins

This sections makes the gedit a really powerful text editor. By default gedit comes with few standard plugins but you can install more plugins by installing the gedit-plugins package. If you don’t have root access, download the plugins from gedit.org and copy it to your ~/.gnome2/gedit/plugins folder. You can continue if you are lazy to download the plugins now. You can do that when a need arises. ;)

After this jump to section which you might be interested in.

Programming

  • Bracket Completion ( You need to install this plugin)

A plugin which automatically adds closing brackets, single quotes and double quotes. Sometimes really useful and sometimes irritating. When you are typing the code, this is pretty useful but when you are editing a code already written, then this irritates you by adding closing brackets or quotes where you never intended to add.

  • Code Comment ( You need to install this plugin)

A useful plugin for any programmer. You can comment or uncomment a selected block of code.

  • Indent Lines

Very useful when you are specific about the indentation rules. Indents or un-indents a selected block of code. Very useful for python.

  • Snippets

Want to insert a piece of code which just a few keystrokes, then this is for you. Yes lesser keystrokes than Ctrl C Ctrl V.

  • External Tools

This gives amazing power to manipulate anything within gedit. I guess i can’t write about this here. Check out this post about External Tools. And don’t skip that post. It’s the BEST plugin for gedit.

HTML

Download the HTML tidy plugin from here and extract the folder to your ~/.gnome2/gedit/plugins folder. Enable the HTML Tidy plugin from the plugins tab in Preferences Window. HTML Tidy is an utility to clean up and pretty print HTML/XHTML/XML. Enable the Bottom Pane from the View Menu and you can clean up or check any HTML/XHTML/XML document.

The default configurations are enough to use Tidy. If you want to tweak it according to your needs, then Configure Plugin ( There are so many options :P )

Snippets

This is an useful plugin once you know what gives what. If you code for online programming contests like spoj, topcoder, acm or usaco and if your programming language is C++, then download this cpp.xml file and put it in your ~/.gnome2/gedit/snippets folder. Then press template[tab] or topcoder[tab] in a new cpp file.

Apart from that, if you are a web developer, then check out the Snippets for HTML and PHP. Once you remember the shortcuts which you need, they’ll make your life really easy.