beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 138 forks source link

restricting searches is not working #504

Closed davidhq closed 9 years ago

davidhq commented 9 years ago

I have ack 2.14 and when I run ack --ruby something there are also .js results... :(

my .ackrc:

--type-add
ruby=.haml,.builder,.yml,.feature
--type-add
objc=.h,.m
--type-set
rackup=.ru
--type-add
csharp=.cs
--type-add
python=.py
--type-set
coffee=.coffee
--type-set
sass=.sass,.scss
--type-add
php=.phtml
--type-set
markdown=.markdown,.md,.mdown
--ruby
--objc
--rackup
--csharp
--python
--coffee
--sass
--js
--php
--html
--nocss
--markdown
--ignore-dir=.idea
--ignore-dir=tmp
--ignore-dir=photos
--type-set=cache=.cache
--nocache
petdance commented 9 years ago

There are .js results because you have --js in your .ackrc, which puts --js on your command line.

Why do you think you need to have --ruby, --objc, etc in your .ackrc? They should not be there unless you always want to search --ruby, --objc etc.

petdance commented 9 years ago

Also I see you have .yml lumped under Ruby files. .yml is already included in --yaml, and you can say --noyaml. Run ack --help-types for a listing.

davidhq commented 9 years ago

Now I understand how .ackrc works... but:

I have --ruby, --objc and others in there so that .log and other stuff is not picked up... I now see that --nocss is not neccessary because it gets excluded automatically as well.

But I still don't know how to include only --ruby given my .ackrc... is there a better way to list what I'm interested in and still have the option of searching only in one type (--ruby for example).

Thank you for .yml comment...

davidhq commented 9 years ago

I'm also cleaning it a bit (coffee for example is included)... I got this .ackrc from somewhere a long time ago and I think those type were not yet included in ack by default.

petdance commented 9 years ago

Don't put any types in your .ackrc. Then, when you only want Ruby, then specify --ruby on the command line.

What you may want to do is use the -k flag to say "Only search the files that ack recognizes the types for". Otherwise, ack includes everything that is a text file. But with -k, if it runs into a foo.xyz file, and that isn't recognized as a given type, then ack will ignore it. This is useful where you're searching trees that have lots of weird non-source files in it, like a lot of log files.

petdance commented 9 years ago

Finally, remember that you can have project-specific .ackrc files that work alongside the global ackrc file and any per-user ackrc files, too.

davidhq commented 9 years ago

I see.

However I'd like it to use -k by default because the search I tried now on a regular sized project was 7x faster with -k... but when I include -k then I again loose the ability to filter by only one type (--ruby)... do you think this is a smart suggestion to be able to filter by specific types when using -k mode?

petdance commented 9 years ago

I'm not sure I understand your last question.

It make sense your search might be 7x faster if -k is excluding huge log files or some other huge text files.

Also, one trick for helping debug file selection is to use the -f flag, with no regex, and see all the files that match the command line, so you could do: ack -f --ruby and see all the Ruby files, or ack -k and see all the files that are known types.

davidhq commented 9 years ago

Yes, I know why it's 7x faster, makes sense... and I'd like to have it like this by default (so to include -k option in .ackrc)

-f is nice... maybe I can show you what I ment better using this:

I think ack -k -f --ruby should not return this:

....
public/help.html
public/javascripts/tinymce_new/langs/readme.md
public/javascripts/tinymce_new/plugins/visualblocks/css/visualblocks.css
public/javascripts/tinymce_new/skins/lightgray/fonts/readme.md
public/javascripts/tinymce_new/skins/lightgray/fonts/tinymce-small.svg
public/javascripts/tinymce_new/skins/lightgray/fonts/tinymce.svg
Rakefile
script/rails

but this:

....
config/routes.rb
db/migrate/001_create_db.rb
db/migrate/20130427143408_add_tags_to_post.rb
db/migrate/20130501114358_migration_tags_not_null.rb
db/migrate/20130629121316_add_last_links_to_user.rb
db/schema.rb
deploy/before_restart
lib/tasks/convert.rake
public/dispatch.rb
Rakefile
script/rails
davidhq commented 9 years ago

I solved the issue for me by creating this bash function:

function ack {
  if [[ $@ == *--* ]]; then
    command ack "$@";
  else
    command ack -k "$@";
  fi
}

But I feel this should be the default... maybe I'm wrong and you know much better.. but for now I'll use this because it work perfectly. Thank you for your help and fast responses.