Sublime Forum

Search and replace only in selected files

#1

I recently switched to ST from TextMate and I’m really enjoying it so far!
The only thing I’m struggling with is the Search and replace functionality which I tend to use a lot.
I know that ST has a lot of cool possibilities to replace multiple occurrences of a string with multi-selection and such, but sometimes I just need to do the following:

  • Search for a string in all files of the current project
  • Select a few files where the string was found
  • Replace the string for something else only in the selected files

It’s the bold bit I’m struggling with. From the “Find Results” window, how can I only select a few files or occurrences and replace those?
Any ideas or workarounds are appreciated! :smile:

0 Likes

Find in files view
#2

Some of the features you mention included in github.com/titoBouzout/SideBarEnhancements

0 Likes

#3

I have the SideBarEnhancements plugin installed but I don’t see how that could help me…
To clarify: I want to search for a term in the whole project first but then replace only some occurrences which I ideally would like to select in the search results.

0 Likes

#4

I’m also a new ST user (from TextMate) and have found the search & replace in “Find in files…” a bit intimidating and frustrating (especially if there are a large number of search results).

Currently, I’m resorting to opening individual files from the Find Results and running the replace command manually. This certainly isn’t very efficient, so I’m hoping there is an easier way too!

BTW, I’m also using SideBarEnhancements…

0 Likes

#5

I don’t have the sidebar enhancements. But I just found that if I go to Find > Find In Files (Ctrl + Shift + F), type in the string to find, click the ellipses and add whatever folders and files you want, and then click the ellipses again and add an exclude filter and type in the file names of files you don’t want to replace the string with, and it worked for me.

Not sure how well this method is for really large projects. It would be nice to add some more functionality to the ‘exclude filter’ option so that you can select from a list what you want to exclude, but not sure if that plugin has been written yet…

0 Likes

#6

[quote=“LaserFace”]I don’t have the sidebar enhancements. But I just found that if I go to Find > Find In Files (Ctrl + Shift + F), type in the string to find, click the ellipses and add whatever folders and files you want, and then click the ellipses again and add an exclude filter and type in the file names of files you don’t want to replace the string with, and it worked for me.

Not sure how well this method is for really large projects. It would be nice to add some more functionality to the ‘exclude filter’ option so that you can select from a list what you want to exclude, but not sure if that plugin has been written yet…[/quote]

Yeah, that’s what I have been doing as well. It basically forces you to search twice, though.

  1. Seach for “foo”, see in results that it exists in files in folders A, B and C. If you just want to replace the occurrences in A and B
  2. search again, excluding C, and replace.

But what if I want to replace “foo” in just a few files in folder A, not in all of them. Then I have to open them all individually, as eyewinder mentioned.

I think Textmate’s solution for this is really nice, where you can select individual files or whole folders in the search results and just do the replace on them.

0 Likes

#7

Yes! The double searching only makes it more time consuming…

It would be nice if the find and replace panel had a “sticky” option – remaining open unless dismissed. It would also be useful to have a option to flush the previous search buffer output, so the Find Results don’t get overly cluttered.

Of course, these suggestions don’t address the main issue of including just a few files for replace actions – rather than the complicated process of excluding many files.

Maybe this could be addressed with dedicated “Include” and “Exclude” text fields with support for autocomplete (like Goto Anything…), regular expressions and drag and drop? Just a few random thoughts…

0 Likes

#8

I’m also a TextMate convert and running into this inconvenience.

TextMate also allows you to collapse all search results to be able to see the files that were matched. I find this very useful when searching for something that will have lots of matches and intuitively knowing which files i’m not interested in. This is much easier than trying to figure out the right include / exclude filters.

0 Likes

#9

I’m a Windows user and unfortunately I can’t check out TextMate.

I also did not realize how much I needed this feature until tonight!

0 Likes

#10

I believe it does have this feature.

Look under find in files, the middle box is “where” , you can use regex like expressions in this box.

Use the … And it has some options, add folder, add include filter, exclude, open folders, open files.

I couldn’t find too much documentation on this though.

This of course assumes the files follow some kind of naming pattern.

Check it out!

0 Likes

#11

[quote=“gregors”]I believe it does have this feature.
Look under find in files, the middle box is “where” , you can use regex like expressions in this box.
Use the … And it has some options, add folder, add include filter, exclude, open folders, open files.
I couldn’t find too much documentation on this though.
This of course assumes the files follow some kind of naming pattern.
Check it out![/quote]

Actually I did discover this feature before I posted my initial reply and I thought it was awesome, and I still do… But what it sounds to me like everyone else here is talking about is more of a gui interface where you can select from a list the ones you don’t want, rather than doing a search, then finding out what you don’t want, then doing another search with the exclude filter on.

But I could be misunderstanding. Maybe someone could supply a textmate screenshot?

0 Likes

#12

Yes, the feature is indeed nice but it only makes sense if you know where (in which folders/files) you want to search&replace.
Here’s a screenshot of the Textmate s&r interface: screencast.com/t/5MAMIX8qON1
I searched in the whole project for “Mechanize”, and as a result I got this list. You can fold in and out all occurrences in a file (e.g. I folded in CHANGELOG.rdoc, the first file) and select a whole file or specific occurrences in a file for replacement. I selected “spider.rb” and three more occurrences, so if I pressed “replace selected” all occurrences in “spider.rb” and the three other onces would get replaced. Can’t get any more intuitive IMHO.

0 Likes

#13

I would also love to see this functionality added. I think the implementation in TextMate2 is even better. Here’s a screenshot showing some collapsed files (with the number of matches shown) and checkboxes next to each instance. It’s really great to be able to go through and mark which replacements you want to make because it’s often a lot easier to visually inspect a match than create a perfect regex.

0 Likes

#14

Now that looks like the ‘kitchen sink,’ of find and replace interfaces. I want one!

I must say though that ST makes searching and replacing easier than anything else I have used so far and I hear ya about it being easier to do it visually. But yesterday I needed to get rid of a bunch of markup with all kinds of stuff crammed into it and after about 30 minutes of regular expressioning myself to pieces, I came up with something really close to what I wanted and I have to say that it felt really nice. Maybe it would have been quicker to manually edit the file, or more relaxing to use TextMate2’s go-go-gadget thingy, but not for me… no pain no gain!

0 Likes

#15

ST really must add a view-but-exclude-from-replace feature for folders (not filetypes).

When doing a global search and replace in a Rails project I’m pretty sure you would never want to replace in any .rb file in the db/migration folder – but you do need to be able to see them, edit them, and search in them. And you cannot exclude *.rb, obviously. And rails migration filenames dont have a fixed prefix + suffix you can easily write a regex for. And to specify a directory to exclude, afaik you cannot make that exclusion part of your project, you have to enter it each time you do a find/replace in the where] field.

0 Likes