the_silver_searcher: Don't search in files that have the filename X. - search

I'm using ag (https://github.com/ggreer/the_silver_searcher/blob/master/doc/ag.1.md) to search for a pattern in a directory.
What I'd like to do is:
ag SearchPattern --excludeFilesThatHaveThisIntheFileName *Test
is that possible?

The option you are looking for is --ignore. From the man page:
--ignore PATTERN:
Ignore files/directories matching this pattern.
Literal file and directory names are also allowed.
After testing it, it seems the pattern must be a file glob instead of a regular expression.
Your line would then become:
ag SearchPattern --ignore *Test

Related

How do I exclude a character in Linux

Write a wildcard to match all files (does not matter the files are in which directory, just ask for the wildcard) named in the following rule: starts with a string “image”, immediately followed by a one-digit number (in the range of 0-9), then a non-digit char plus anything else, and ends with either “.jpg” or “.png”. For example, image7.jpg and image0abc.png should be matched by your wildcard while image2.txt or image11.png should not.
My folder contained these files imag2gh.jpeg image11.png image1agb.jpg image1.png image2gh.jpg image2.txt image5.png image70.jpg image7bn.jpg Screenshot .png
If my command work it should only display image1agb.jpg image1.png image2gh.jpg image5.png image70.jpg image7bn.jpg
This is the command I used (ls -ad image[0-9][^0-9]*{.jpg,.png}) but I'm only getting this image1agb.jpg image2gh.jpg image7bn.jpg so I'm missing (image1.png image5.png)Kali Terminal and what I did
ls -ad image[0-9][!0-9]*{.jpg,.png}
Info
Character ranges like [0-9] are usually seen in RegEx statements and such. They won't work as shell globs (wildcards) like that.
Possible solution
Pipe output of command ls -a1
to standard input of the grep command (which does support RegEx).
Use a RegEx statement to make grep filter filenames.
ls -a1|grep "image"'[[:digit:]]\+[[:alpha:]]*\.\(png\|jpg\)'

What is file globbing?

I was just wondering what is file globbing? I have never heard of it before and I couldn't find a definition when I tried looking for it online.
Globbing is the * and ? and some other pattern matchers you may be familiar with.
Globbing interprets the standard wild card characters * and ?, character lists in square brackets, and certain other special characters (such as ^ for negating the sense of a match).
When the shell sees a glob, it will perform pathname expansion and replace the glob with matching filenames when it invokes the program.
For an example of the * operator, say you want to copy all files with a .jpg extension in the current directory to somewhere else:
cp *.jpg /some/other/location
Here *.jpg is a glob pattern that matches all files ending in .jpg in the current directory. It's equivalent to (and much easier than) listing the current directory and typing in each file you want manually:
$ ls
cat.jpg dog.jpg drawing.png recipes.txt zebra.jpg
$ cp cat.jpg dog.jpg zebra.jpg /some/other/location
Note that it may look similar, but it is not the same as Regular Expressions.
You can find more detailed information here and here

how to exclude files when using globpath() function

I want to obtain in Vim any file in a path that doesn't have a .tex or .bib extension.
I tried (in order to ignore tex files) the following command
:echo globpath({some path}, '*.[^tex]*')
but that will also ignore combinations of tex characters (for instance .toc files).
So how can I modify the pattern in order to match any file without .tex or .bib extension?
Edit: Vim' negative lookahead is done with \#!. I have a directory /test with the files foo.bib, foo.tex, foo.pdf, foo.aux and foo.toc. I tried doing
:echo globpath('C:/Users/Pedro/Desktop/test', '\w*\.\%\(tex\|bib\)\#!')
but that returns an empty string.
to use globpath() function, you don't need regex. it's a glob function. you can just set the wig option (wildignore) , then call the function .
set wig=*.bib,*.tex
then
:echo globpath('/your/path','*.*')
do a test:
kent$ pwd
/tmp/test
kent$ ls -1
bar.bib
bib.bar
f.txt
tex.foo
x.tex
in vim:
:set wig=*.bib,*.tex
:echo globpath('/tmp/test','*')
we got:
/tmp/test/bib.bar
/tmp/test/f.txt
/tmp/test/tex.foo
note that you can call it globpath('/path','*',0,1) to have returned value in a list, sometimes it is handy to use in script.

Find all PHP files in the current folder that contain a string

How could I show names of all PHP files in the current folder that contain the string "Form.new" in a Linux system?
I have tried grep "Form.new" .
You need to search recursive or using* instead of ., depending of whether you want to search only file right inside that directory or also in deeper levels. So:
grep -r "Form\.new" .
or
grep "Form\.new" *
Assuming that your PHP files have a .php extension, the following will do the trick:
grep "Form\.new" *.php
Like #LaughDonor mentioned, it's good practise to escape the dot; otherwise, dot is interpreted as “any character” by grep. "Form.new" also matches "Form_new", "Form-new", "Form:new", "FormAnew", etc.

vim: comment/uncomment all lines in file containing a string

I have a big python file as follows:
#login_required
#user_passes_test(lambda u: u.is_superuser)
def foo():
//function body
#login_required
#user_passes_test(lambda u: u.is_superuser)
def foobar():
//function body
.
.
.
Like this there are many functions in the file. I want to comment all the lines which contains the pattern login_required or user_passes_test. How to comment those lines?
I use tComment plugin. So I can toggle line comment using gcc key-mapping. Can it be used?
There are also other files in the project which contains similar functions. So how can I comment these lines in all files in the project?
And again if I need to uncomment those lines how do I?
The :global/{pat}/{cmd} command will run a command, {cmd}, on every line matching pattern, {pat}. You can execute your tComment command via the :normal command. All together it looks like this:
:g/#login_required/norm gcc
For more help see:
:h :g
:h :norm
If you want to comment certain lines, then uncomment those same lines later, I'd use some kind of "marker" in the comment to make the job easier.
So to comment, for example:
1,$s/^\(.*#login_require\)/#FOO \1/
Then to uncomment:
1,$s/^#FOO //
You would choose #FOO so as not to be using it anywhere else for another purpose. You can even pick something simpler like ##... really anything that starts with # that you're not already using.
This will not be with VIM, but i think easier way to comment out in all project,multiple files:
sed -i 's/#login_required/#login_required/g' *
or for files in directories:
find ./ -type f -exec sed -i 's/#login_required/#login_required/g' {} \;
Comment lines containing string:
:%s/\(.*string\)/# \1/c
%s - global substitute
\(.*string\) - pattern to match
# \1 - replacement, \1 is for the matched pattern
c - confirm before substitution
Similarly, to uncomment:
:%s/# \(.*string\)/\1/c

Resources