Randomly Named User
Randomly Named User

Reputation: 1949

Search for every mention of a variable in a file

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.

Upvotes: 0

Views: 325

Answers (3)

mauricio
mauricio

Reputation: 153

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.

Upvotes: 0

lakshayg
lakshayg

Reputation: 2173

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

Upvotes: 2

bookhuntress
bookhuntress

Reputation: 331

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.

Upvotes: 2

Related Questions