Sunday, March 30, 2008

gedit Regular Expression Plugin


A friend told me that gedit was missing a regular expressions plugin that could replace. I use gedit occasionally, and so I worked on this plugin. It uses Python's good regular expression module, and supports backreferences, making it possible to do significant text processing.

This plugin is based on extensão para o gEdit by Daniel Carvalho. I fixed bugs, cleaned up the interface, moved the menuitem to the Search menu, added backreferences support, added replace all, and added an option for case-sensitivity. Also, the search mode is multiline, so ^ matches the start of a line. Download
To install, place the files in ~/.gnome2/gedit/plugins/. Then open gedit, choose Preferences from the Edit menu. On the plugins tab, you should see Regular Expression Replace in the list. Check to enable. Now, you should have a "Regular Expression" item in the Search menu. Tell me if you have any feedback.

Backreferences are very useful. In the screenshot above, I've written a regular expression for turning "one.tim" into "tim.one", and so on.

75 comments:

Kalar said...

SECURITY CENTER: See Please Here

Ben said...

Thanks, it works perfectly.

Question: Is it possible to use Ctrl-H shortcut? Or add a shortcut for it, at least.

eplawless said...

Thanks for this; really excellent work.

Sandy said...

Thank you, I was looking for just this sort of thing!

Rafael said...

Hey man, very nice to see my contribution could be extended!

I'll test later!

Thank you!

mork said...

thank you! good work!

milf said...

black mold exposureblack mold symptoms of exposurewrought iron garden gatesiron garden gates find them herefine thin hair hairstylessearch hair styles for fine thin hairnight vision binocularsbuy night vision binocularslipitor reactionslipitor allergic reactionsluxury beach resort in the philippines

afordable beach resorts in the philippineshomeopathy for eczema.baby eczema.save big with great mineral makeup bargainsmineral makeup wholesalersprodam iphone Apple prodam iphone prahacect iphone manualmanual for P 168 iphonefero 52 binocularsnight vision Fero 52 binocularsThe best night vision binoculars here

night vision binoculars bargainsfree photo albums computer programsfree software to make photo albumsfree tax formsprintable tax forms for free craftmatic air bedcraftmatic air bed adjustable info hereboyd air bedboyd night air bed lowest pricefind air beds in wisconsinbest air beds in wisconsincloud air beds

best cloud inflatable air bedssealy air beds portableportables air bedsrv luggage racksaluminum made rv luggage racksair bed raisedbest form raised air bedsaircraft support equipmentsbest support equipments for aircraftsbed air informercialsbest informercials bed airmattress sized air beds

bestair bed mattress antique doorknobsantique doorknob identification tipsdvd player troubleshootingtroubleshooting with the dvd playerflat panel television lcd vs plasmaflat panel lcd television versus plasma pic the bestThe causes of economic recessionwhat are the causes of economic recessionadjustable bed air foam The best bed air foam

hoof prints antique equestrian printsantique hoof prints equestrian printsBuy air bedadjustablebuy the best adjustable air bedsair beds canadian storesCanadian stores for air beds

migraine causemigraine treatments floridaflorida headache clinicdrying dessicantair drying dessicantdessicant air dryerpediatric asthmaasthma specialistasthma children specialistcarpet cleaning dallas txcarpet cleaners dallascarpet cleaning dallas

vero beach vacationvero beach vacationsbeach vacation homes veroms beach vacationsms beach vacationms beach condosmaui beach vacationmaui beach vacationsmaui beach clubbeach vacationsyour beach vacationscheap beach vacations

bob hairstylebob haircutsbob layeredpob hairstylebobbedclassic bobCare for Curly HairTips for Curly Haircurly hair12r 22.5 best pricetires truck bustires 12r 22.5

washington new housenew house houstonnew house san antonionew house venturanew houston house houston house txstains removal dyestains removal clothesstains removalteeth whiteningteeth whiteningbright teeth

Rafael said...

Hey Ben,
I'm commenting just to thank you for the work. It's being useful.

Best,
Rafael.

Waldir said...

Thanks a lot for the excellent plugin. However it would be even better if it didn't disable the replace buttons if there's no text in the replace field. Sometimes we need to just remove some parts of a text, not replace them with something else. Can you do this?

Faceleg said...

Thanks for this!

I agreed with Waldir, being able to replace text with nothing (delete) is very useful.

Andrew said...

Thanks for this, but my first use of it found a bug. I want to remove the first two leading spaces, so I search for '^ '

It looks like the '^' is not bound to the start of the current line, it's bound to the point where the search begins. Being cautious, I used 'find' a few times before doing the replace all. It steps through multiple matches on the same line.

My wishlist:

1. Fix the above
2. Add a hotkey to bring up the dialog
3. Add a replace and search again button with keyboard shortcut.

It's nearly there.

I've been using nedit, which is not bad, except that it doesn't understand the gnome virtual file system, which would be much more convenient sometimes.

rob said...

Nice, thanks! It would be nice to have a keyboard shortcut or a 'highlight all' option.

beli said...

Thanks, very useful!

beli said...

Would it be possible to replace text in a highlighted (selected with the mouse cursor) region?

brian.j.bowers said...

Yeah, I agree. Can we add a checkbox option for "Search/Replace Within Selected Text Only"?

sergey892 said...

Thanks for the great plugin! Regular expressions really make gedit useful!

njcar said...

Hey Ben,

Thanks for the awesome plugin. Hope I have the skills to write these sorts of things one day.

Nick

tiggsy said...

Lovely! Now, why don't they just include this with the regular plugins, i wonder?

ed said...

Easy to install and use.
Many thanks!

heman said...

Thanks!!

LiraNuna said...

I would say, extend the original search and replace interface and add a 'Regular expression' option - best of both world.

fruit.dev said...

very useful plugin, thanks

Maxo said...

I'd like to echo what LiraNuna said. It would be great if this extended the regular Search and Replace window.
Otherwise, very useful.

Noel said...

Great work!! It would be nice to have an option to toggle between single-line and multi-line mode.

johann.fot said...

THX

PhoneixS said...

Thanks for the plugin. Can you make it translatable, if you like, I can translate it's texts into spanish.

Yva said...

Very nice work!

Small bug though: when there is 2 successive matches the second one isn't caught.
Example: when using "<.+?>" on "this is a test test" the < /b> isn't caught.

Thomas Käfer said...

Thanks for the great plugin!

What im missing:
-) Shortcut or merge with standard replace strg+h
-) Dont disable replace buttons when replace field is empty

... said...

For those who want to replace something for nothing (the empty string), if we open the file regexsearchinstance.py and change the line
----
if len(search_text) > 0 and len(replace_text) > 0:
----
to
----
if len(search_text) > 0:# and len(replace_text) > 0:
----
and save, the program will not disable the replace all button. (but I'm not sure if it will break it in anyway - I hope not).

Gavin said...

nice plugin. Im having a problem with replacing, unless I'm doing something wrong. Replace with '\n' inserts literal \n instead of LF.

I'm trying to add text at the beginning of each line:

match: '\n'
replace: '\n2010-09-17,'

the match works but the replace not.

Waldir said...

Gavin, I too find that annoying, but you can use a trick to workaround that: just enclose the \n in the search field as (\n), and use \1 (or whatever numbered reference it is) in the replace field.

Granted, this is not a complete solution, since not always you'll have newlines in the search field, but as I said, it's a workaround :)

Btw, thanks anonymous (↑↑) for the empty string replace trick! However, I think I don't recall doing it, and nevertheless the replace button doesn't get deactivated when the replace field is empty (I can't remember when it stopped doing that). Can anyone confirm this is the default behavior without applying anonymous' hack? Maybe something changed in gedit itself.

pmorton said...

This is really useful. I'm having trouble with changing case though. \L and \U don't work on backreferences in the replace field to change the case to upper. Does anyone know how to change case?

Richard said...

seems to work well, thanks a lot,
gedit really needed some regex support

ihoru said...

Thank u!!

MPS said...

Really, really great!! Exactly what I was looking for. Many thanks

Donald said...

Thanks. This is great!!

Ironarjen said...

Sorry, does not work at all. Regex Search and replace appears in the Plugin list but checking it does not change the search box

Waldir said...

@Ironarjen it does work, but unfortunately it's not integrated into the main search/replcace dialogs. You have to use the menu Search > Regular expression (since there isn't a keyboard shortcut either).

Nik said...

Amazing thank you very much for this time saving plugin
Very easy to use

quinn said...

Thank you! Your plugin is wonderful.

François Michonneau said...

It doesn't seem to work in gedit 3 (doesn't show up in the menu). I put the plugin in ~/.local/share/gedit/plugins. Any hints on how to solve this?

Paul said...

I'm using the Windows version of gEdit 2.30.1 (latest available!) and when I select the plugin in preferences it just greys out. Any ideas?

That's actually better than most plugins I've tried which just crash gEdit!

Paul said...

Just to update, installing glade 3.6.7 (with GTK+) - all 17M - allowed me to select the check box and the plugin seems to function.

It would be nice to know which bits I really need and where they need to be located in Windows or the gEdit structure?

Bowser said...

Great plugin, does what it says. Thanks for posting!

kyogre said...

Totally awesome !!! Thank u man, a lot!

typedef uchar_t pluto; said...

Thanks very much.. Very useful and most needed plugin.

Michael said...

@François

There is no way to make the plugin work in Gnome 3. Plugins for Gnome 2 are built in Gtk+2 but plugins for Gnome 3 are built in Gtk+3. I've run into this problem with other plugins I use that to run in Gnome 3 they require a complete recoding so until then we are out of luck, which is unfortunate because I really love this plugin.

Ben,

Do you have any plans to update the plugin to gnome 3 or should those of use on gnome 3 look for another solution?

David Brault said...
This comment has been removed by the author.
David Brault said...

Yay! Thank you. Got it working on my machine now.

Stephen Bussard said...

thanks

Nemo said...

How do I get this work on gedit/gnome 3? I placed it under .local/share/gedit/plugins but it doesn't work, and https://code.google.com/p/advanced-find/ is total crap

Etienne Lawlor said...

I am also having trouble installing this plugin with gedit version 3.2.1. Has anyone been able to install this plugin successfully?

KeyNeom said...

What are the dependencies for your plugin? I'm running Arch-Linux with gnome3. I used the gnome3 version of the plugin I found at the gnome3 gedit plugins page but when I click "Regular Expression..." from the Search menu in gedit I get a blank frame that pops up. I haven't seen this happen with anything else so I figure it must be the dependencies you are using but any other suggestions are welcome. Thanks for your work!

webe said...

hi ben.fisher,

thanks alot.

Works great on Ubuntu 10.04.2 LTS.
Great help for editing srt-files.

webe

PS. minor request: remember regex

edek said...

Path for gedit plugins has changed to ~/.local/share/gedit/plugins

Waldir said...

@edek: doesn't work for me (Ubuntu 11.10) and I tried placing it both in ~/.local/share/gedit/plugins (note: the gedit folder didn't exist, I created it myself) and /usr/share/gedit/plugins, to no avail; no menu entry, and no item in the plugin list (in the preferences dialog). Can you give some details on your system?

Jack David Baucum said...

Anyone using a Gnome 3 distribution needs to grab the port at http://live.gnome.org/Gedit/Plugins?action=AttachFile&do=view&target=regex_replace-gedit3.tar.gz also listed in the gedit plugins page at http://live.gnome.org/Gedit/Plugins
Ubuntu 11.10+ uses Gnome 3.

edek said...

@Waldir

I use ubuntu 11.10.

That works for me (assume that your downloaded file is called regex.tar.gz):

tar xzvf regex.tar.gz
mkdir -p ~/.local/share/gedit/plugins
mv regex_replace ~/.local/share/gedit/plugins

start gedit

open preferences window, plugin should appear in the list

Nickleus said...

i made a one-stop tutorial for how to do this on ubuntu 11.10:
http://nickhumphreyit.blogspot.com/2012/01/how-to-install-gedit-regexregexp-plugin.html

Nickleus said...

when i close the regex box then try and open it again it is blank. i have to restart it to get it working again.

Beright said...

Thanks for the plugin, very nice. For those complaining you can't replace with nothing, just type a space then delete it. Maybe that's something new though?

Menegazzo said...

Man, i tried everything to put your plugin to work:

http://askubuntu.com/questions/61785/how-do-i-install-a-plugin-for-gedit-v3

Tried many folders, like usr/lib/gedit/plugins, ~/.local/share/gedit/plugins and ~/.gnome2/gedit/plugins and nothing.

I think my problem is because my gedit is V3. Can you help me with hints about what to do? I am really addicted to regular expressions, but really newbie on linux systems. So things like compile/build/make myself are still a mistery.

Thanks in advance

Menegazzo said...

Man, i tried everything to put your plugin to work:

http://askubuntu.com/questions/61785/how-do-i-install-a-plugin-for-gedit-v3

Tried many folders, like usr/lib/gedit/plugins, ~/.local/share/gedit/plugins and ~/.gnome2/gedit/plugins and nothing.

I think my problem is because my gedit is V3. I am really addicted to regular expressions, but really newbie on linux systems. So things like compile/build/make myself are still a mistery.

Can you help me with any hints about what to do in order to gedit reconigze your plugin?

Jack David Baucum said...

@Menegazzo:
Look at the above comments. The Gnome3 version of the plugin is at http://live.gnome.org/Gedit/Plugins?action=AttachFile&do=view&target=regex_replace-gedit3.tar.gz

Menegazzo said...

Sorry, i missed it!

I will try it at home.

Thanks

Menegazzo said...

Working perfectly now. Congrats to Daniel d'Andrada T. de Carvalho and Ben Fisher, youre the guys. Because of your initiative, now i'm very tempted to learn a bit of python and plugin development.

Nickleus said...

*SWITCH* to geany if you want regex =) i've given up on gedit.

sudo apt-get install geany

markling said...

This is pretty damn cool. You've really saved the day.

I hope I may help by making a suggestion. Would you please make it key into Gedit's 'Undo' function? It presently undoes (gedit2) the action prior to the regex command, or indeed any number of regex commands. I could regex for half an hour but undo would still undo the op before the first regex command was executed and therefore wipe out the whole lot.

Cheers.

Evgenii said...

http://live.gnome.org/Gedit/Plugins
links to new version of this plugin look here: http://nickhumphreyit.blogspot.com/2012/01/how-to-install-gedit-regexregexp-plugin.html

download here:
http://live.gnome.org/Gedit/Plugins?action=AttachFile&do=view&target=regex_replace-gedit3.tar.gz

Adam Victor Nazareth Brandizzi said...

Hi, Ben et al.

Some time ago, I forked this plugin and made some significant changes on it. It supports an accel key (Control+R), highlights found text, etc. Also, I put it in a bitbucket project, so I bet it will be easier to manage the various versions and to fork it: https://bitbucket.org/brandizzi/gedit-re-search/wiki/Home

BTW, I recently ported it to Gedit 3, which seems to be a very required feature - already provided by Jack Baucum, but anyway, it is available there too :)

passshok said...

Hello!

I have gedit 3.4.1 on ubuntu 12.04 and I have some bugs with you plugin. Could you please fix it?

1. When I start gedit, the regexp dialog appears on screen for about 1 second and than it hides and gedit loads.
2. When I enter some text in regexp dialog in text field, I see no cursor blinking at all, text is editable, but no cursor indication!

Thank you! Nice day!

passshok said...

Thank you. It was fixed in previous comment.

Etienne Lawlor said...

I just wanted to mention this is a great plugin. I am just having this one issue. I am on Ubuntu 12.04 and using gedit v3.4.1.

The first time i open the plugin it works fine, then if i close it and try to open it back up in the same gedit session, i can only see the title bar of the dialog. When i go to expand the content below the title bar it appears all blank.

sam said...

Great hack, Ben-- you and Nickleus saved my day! --after spending half of it trying to get a regex S/R in gedit, with your help I have now accomplished something...
Thanks!

Bachsau said...

Updated Version for Pluma (MATE):
http://files.bachsau.com/pluma_regex_replace.tar.gz