0

I have a massive file, of length more than 4000 lines. There are more than 200 variables declared, and some of the variables are really badly defined: For example,

int s;
int waste;

Now, I need some tool that helps me find every mention of the variable s. I can't use ctrl+F because that would give me every mention of the letter s, rather than every mention of the variable s. So, s would get detected in waste as well.

For example, when I search for s, I want this to get detected:

if(abc == 2 && s == 3)

But this should not be detected:

if(abc == 2 && waste == 3)

I'm currently using Notepad++, and would like to know if there's any feature on Notepad++ that would allow me to do this.

If not, can you suggest any other editor / tool / software that would do this for me?

If I have something like:

int x;
struct a
{
    int x;
    int y;
} st;

And I wan't to find only all occurrences of the x declared outside the struct, then the ctrl+F match whole word feature of Notepad++ would not work, because even st->x would be matched.

Randomly Named User
  • 1,889
  • 7
  • 27
  • 47

3 Answers3

2

Try using this in vim

/\<your-query\>

And if you want to replace every occurrence of the word foo with bar then

:%s/\<foo\>/bar/g

using \< and \> around the word matches only the complete word.

EDIT:

  • Move the cursor inside the block you want to preserve.
  • Use di{ to kill the block to a temporary register.
  • Use :%s/\<foo\>/bar/g to replace every occurrence of "foo" with "bar"
  • Press `` to go to the last edited position.
  • Press P to yank the text back inside the braces
lakshayg
  • 2,053
  • 2
  • 20
  • 34
  • Yes, thank you so much, @Lakshay. But there are a few scenarios that that would not match. I edited the question after reading your solution, as I realized I hadn't been as clear as I should have been. Please let me know if you have any other method of solving this problem. – Randomly Named User Apr 30 '15 at 07:57
  • @RandomlyNamedUser You could do this by temporarily moving the part that you want to remain unchanged to a register. This can be done in vim using `di{` to select the part inside the braces. Then repeat the above command and then paste the part that was deleted back to its location. – lakshayg Apr 30 '15 at 08:09
2

In Notepad++, press Ctrl+F, check the "Match whole word only" checkbox and click "Find" or "Find All in Current Document". I'm using Notepad++ v6.2.2 (UNICODE). I tried the scenario you presented in your question and using this approach worked for me.

bookhuntress
  • 321
  • 2
  • 12
  • Yes, thank you so much @bookhuntress. But there are a few scenarios that that would not match. I edited the question after reading your solution, as I realized I hadn't been as clear as I should have been. – Randomly Named User Apr 30 '15 at 07:56
  • In Eclipse, users can highlight a variable and press Ctrl+Shift+G to search for all occurrences of this variable. Search results will appear inside the Search tab at the bottom. You can also try this [approach](http://stackoverflow.com/questions/13521485/how-to-search-for-a-method-or-variable-name-within-android-project). I have tried these approaches using Eclipse for Java, but I think there's also an Eclipse version for C/C++. – bookhuntress Apr 30 '15 at 09:41
0

on Sublime Text place the cursor on your variable (don't select it) and press Command + D multiple times until it has added a cursor in front of every match then you can edit the variable and press ESC to exit the multi-cursor mode.

mauricio
  • 153
  • 7