This can be done using gedit Snippets. They are available as part of gedit-plugins from your distro's repositories.
Once they're installed, open gedit, go to Edit -> Preferences -> Plugins, and enable Snippets.
Now you have to create the new snippet which is pretty straightforward and an amazing time saver for code you write frequently.
1. Go to Tools -> Manage Snippets.
2. Find the language/type of file you want to use the snippet for (or Global for all) and click the '+' icon to create new snippet.
3. Name the snippet, then click in the "shortcut key" text box on the right and press your shortcut key combination you want to use: e.g. Shift_ctrl_%
4. Then in the box on the right, enter:
$<
import re
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += re.sub('^%((.)*)', r"\1\n", line)
return output
>
This uses a python regular expression to only remove the first character in a line if it is '%'.
note: you can use any python code in a snippet, for example if you wanted to remove multiple instances of '%' or '#' at the beginning of a line you could use lstrip.
$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += line.lstrip('%#') + "\n"
return output
>
5. After you create the snippet, click close, highlight your text and hit your shortcut key.
note: If you want a snippet that will return % back t the beginning of all highlighted lines:
$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
output += "%" + line + "\n";
return output
>
note: for some reason snippets with shortcut keys only work (for me) when they're defined for a specific language. Global shortcut keys don't work but the tab triggers for them do, ymmv.
More info on snippets at http://live.gnome.org/Gedit/Plugins/Snippets
altto select a block of text, then delete. – hyperslug Jan 23 '10 at 2:42