Testing `gedit` external tools for executing codes in Linux standard text editor.
Tags: linux, programming, tutorial, tips
Published: 2020-09-15 19:47:51

## Background
Text editors support us to create and write codes in various programming languages. In several text editors (such as [Visual Studio Code](https://code.visualstudio.com)), they can also execute code written without executing or compiling the code in external terminal.

Many operating systems has their own standard text editor such as Linux with its own text editor `gedit`. Unlike other standard text editors, `gedit` has special features. It can highlight codes based on code written, and it can also execute codes using plugins!

I'm currently using Ubuntu 18.04 as primary operating system and get interested to try external tools plugin in `gedit` which has been installed by default in Ubuntu 18.04.

## Methodology

By opening **Text Editor** first

![opened text editor](https://dev-to-uploads.s3.amazonaws.com/i/d8npmepn7be2bap4v73d.png)

By clicking text editor menu on the **Top Bar** and accessing the preferences...

![text editor menu](https://dev-to-uploads.s3.amazonaws.com/i/ofzyf60tunictsxu4mb7.png)
![text editor preferences](https://dev-to-uploads.s3.amazonaws.com/i/v7hk8ru3f1i9g9tw15oe.png)

Accessing **Plugins** tab, we can enable "External Tools" by checking it and finally use it...

![external tools enabled](https://dev-to-uploads.s3.amazonaws.com/i/xikr7bwhutq65nzw0hcf.png)

Finally, we can create some extra commands for executing code externally by access external tools menu (by clicking Text Editor menu -> Manage External Tools...).

![external tools](https://dev-to-uploads.s3.amazonaws.com/i/tdh7128fgpble743jt0y.png)

By clicking the plus sign button in the bottom left corner, it will create the new empty tool). There are interesting stuffs that we can see with this external tools menu
- **Space for writing** external tools command (usually using Linux `bash` code, but in my observation it can also use `Python`).
- **Shortcut key** for triggering keyboard shortcut to execute the commands.
- **Input** for specify the input source (if the program receives some input).
- **Output** for specify the output.
- **Applicability** for specify which document supports for this external tool command.

![new external tool command](https://dev-to-uploads.s3.amazonaws.com/i/20sp9jb7h7bv069k7ccn.png)

for example, I would like to **execute some JavaScript code using external tools**. We knew that we can use `node appname.js` for executing JavaScript file using Node.js (assuming Node.js has already installed). By typing the command inside the space,  
```bash
node $GEDIT_CURRENT_DOCUMENT_NAME
```  
and applying shortcut using <kbd>F5</kbd>, we have successfully created our first external command. `$GEDIT_CURRENT_DOCUMENT_NAME` is some `gedit` variable that we can see in [this link](https://wiki.gnome.org/Apps/Gedit/Plugins/ExternalTools).

![first external tools command](https://dev-to-uploads.s3.amazonaws.com/i/xlxjanzq6ftol50d1f4h.png)

## Simulation

Let's test our command in our simple JS file by pressing <kbd>F5</kbd> and we found show the results based on Node.js command.

![command output](https://dev-to-uploads.s3.amazonaws.com/i/yuow6gh6fn7xtlkx6zjw.png)

Let's try the another external code for opening files such as **Python** and **JavaScript** by typing this code and same shortcut.

```sh
#!/bin/sh
TYPE=$GEDIT_CURRENT_DOCUMENT_TYPE
APP_NAME=$GEDIT_CURRENT_DOCUMENT_NAME

# checking file mime-type
# $TYPE

# switch conditional
case $TYPE in
	## nodejs
	"application/javascript")
		node $APP_NAME
		echo "Nodejs file executed"
	;;
	## python
	"text/x-python")
		python3 $APP_NAME
		echo "Python file executed"
	;;
	## else
	*)
		echo "Not supported programming file"
	;;
esac
```

![another command](https://dev-to-uploads.s3.amazonaws.com/i/d961smfkrjvp865jxd1n.png)

It executes successfully on JavaScript file (Node.js)

![output js](https://dev-to-uploads.s3.amazonaws.com/i/w81c3jbhgl1bcv6hqmvp.png)

And it also executes successfully on Python file

![output python](https://dev-to-uploads.s3.amazonaws.com/i/4viaoo5z1b5jssdnhc5k.png)

## Conclusion
In conclusion, `gedit` can executes file externally using external tools. By using some `bash` code, it can used for create command for execute codes for various functions.

## Some reference(s)
- GNOME Gedit External Tools documentation (https://wiki.gnome.org/Apps/Gedit/Plugins/ExternalTools)