Sunday, April 13, 2008

gedit External Tools indent-- & indent++

UPDATE: The same thing can be accomplished by simply pressing the tab key when you have a block of code selected, hold shift to decrease indent instead of increase. Thanks Ben !

program conditional block example There's times when I have to remove an outter conditional block from a script & in order to keep everything orderly this means I also need to decrease the indent in the code within that conditional block by one.

To the right is an example of this situation. I want to remove the else outter conditional since the inner code needs to be evaluated whether the preceeding code succeeds or not.

Normally in something like Notepad this would mean alot of fancy finger work on the down arrow and the delete key on my keyboard.
In Notepad2 I was able to utilize the find & replace support for Regular Expressions and remove the tabs from the beginning of each line in the selection.

Now that I have gedit as my primary editor since I'm on Ubuntu I don't have acces to Notepad2, however I do have access to the External Tools plugin for gedit.

Through the external tools plugin I can filter selected text through sed, which is basically a RegEx filter designed to be used with pipes.

So I setup two new commands through that plugin, one for decreasing the indent by one which I call indent-- and another which I use to do the exact oppisite which I call indent++

If you're new to gedit like I am, first you'll need to enable the Enternal Tools plugin.
You do this by going through
Edit -> Preferences -> Plugins Tab -> Check "External Tools"


This will enable the following menu option where you can manage commands to filter selected text and documents through.
Tools -> External Tools


The dialog for managing tools is pretty self explainitory, here's what I entered for the command to be used for my indent-- tool.
#!/bin/sh

sed 's/^\t//g'



Here's the command I'm using for my indent++ tool.
#!/bin/sh

sed 's/^/\t/g'


I actually got the idea after seeing the existing command to remove whitespace from the end of lines.

5 comments:

Ben said...

Not sure what version of Ubuntu/Gnome/Gedit you're using, but mine came with a plugin that already does that.

Joe said...

I see you can use the TAB key to increase the indent of selected text now.

Not sure how you can decrease the indent in a way similar to that though.

Keiji said...

I just use Kate, where I can just select a bunch of lines and use tab and shift-tab to indent and unindent respectively... and on Windows, I can do the same in EditPlus. I've always just taken that for granted, it seems you're making a mountain out of a molehill =p

Jotie said...

In gedit, use tab to increase indent and shift-tab to decrease indent. This work for single line and multiple selected lines.
Shift-tab also work for removing regular space from the beginning of the lines

Anonymous said...

Perfect, regex always comes in handy.