With gedit plugins, you can turn this simple text editor into a lightweight IDE. It’s fast, has good syntax highlighting, and can have code completion, shell integration, and many similar feature you might expect from an IDE. One feature it lacked was trimming whitespace from files. I searched for plugins to do this, and found several, but none of them quite met my expectations, because none were configurable. I typically want my files to end with one and only one newline. Of course, the solution is Perl.
Gedit has a plugin that let you fire shell commands and scripts to do basically whatever you want. You can edit the highlighted text, replace the whole buffer, or start background tasks that don’t edit your document at all.
Go to Edit > Preferences > Plugins
and find “External Tools.” Click the checkbox to active it, and click “Configure Plugin.” Below the list of tools, click the button with the green +
to add a new tool. Give it a name, and add this Perl script to the main text area:
#!/usr/bin/env perl
my $num_newline = 1; # no. of \n to end with
local $/; # slurp...
my $text = <STDIN>; # ... from stdin
$text =~ s/[[:blank:]]+$//mg; # trim each line
$text =~ s/\n+\Z//mg; # trim consecutive \n at end
# Print the text, with requisite no. of \n
print $text, "\n" x $num_newline;
We receive the entire buffer on stdin, then trim any blank characters off the end of each file. Then, we look for consecutive newlines at the very end and remove them. Finally, we print the trimmed text, adding back on the number of desired newlines. For me, that’s 1 – you might want 0.
Set your shortcut key, then set Input to “current document” and output to “replace current document.” Now, whenever you hit that shortcut key, your document will get trimmed just the way you like it. Unfortunately, there doesn’t appear to be a way to have any of these external tools fire on the hooks a proper plugin can access, like every time a document is saved.