Sublime Forum

How to search in current file changes?

#1

I couldn’t find any info about this, but sometimes I want to search in my current file changes (unstaged files), for example to look for forgotten “console” statement in JavaScript files before committing.

I’ve looked at the search options but none of them seems to allow searching in current changes. Any idea if that can be done at all?

0 Likes

#2

Is this a Sublime Merge or Sublime Text question?

0 Likes

#3

Right, sorry wrong category, it’s indeed for Sublime Merge.

0 Likes

#4

Since Merge maps a GUI onto underlying git commands, as far as I know there’s not currently any way to examine the contents of files that aren’t committed yet. There is a contents: operator in the query panel, but it looks for commits that inserted or deleted text, so unchanged files are not likely to show up there (unless it also searches stashes).

0 Likes

#5

git diff | grep "something" would achieve search using a git command, so I feel it’s still related, but if it doesn’t exist yet I can live without it.

0 Likes

#6

There may already be a request for this, but if not you can add one to the issue tracker.

Merge has the ability to execute arbitrary git commands, so there is some recourse for something like this. Unfortunately, git diff has no flag for checking only unstaged files without more gymnastics involved.

You can however do tricks with aliases. For example, with an alias like this:

[alias]                                                                
    searchunstaged = "!f() { grep --with-filename "$1" $(git ls-files -m --others --exclude-standard); }; f"  

You could do something like:

(master *=) tmartin:dart:~/test_repo> git searchunstaged stuff
yarn.lock:I did some stuff

With that out of the way, you can get Merge to execute this command and ask you for the text to include by creating a file named Default.sublime-commands in your Merge User package with the following content (or just add the command if you already have such a file):

[
    {
        "caption": "Search unstaged files...",
        "command": "git",
        "args": {"argv": ["searchunstaged", "$text"] }
    }
]

Then you can select the command and enter the text:

image

And when you run the command, you can click the terminal icon (in the image above, it’s visible at the top-right with the green check) to see what the command did:

2 Likes

#7

see these issues:

and my forum comment:

0 Likes

#8

Many thank for the info @OdatNurd, I’ll give that a try.

0 Likes