Skip to content
suewonjp edited this page Jun 13, 2017 · 3 revisions

g - Quickly search text from files

gi - Same as g except that case insensitive matching is performed for filenames (not for text patterns)

✔️ General Usages

g and gi commands search text from files returned by lf or lfi

  1. g [ text pattern ]
    • Will search files (recursively) for the given text pattern through current working directory.
    • By default, dot folders (like .git, .svn, etc) will be excluded from its search
      • In order to include dot folders, use .+ notation like g [ text pattern ] .+
    • [ text pattern ] is BASIC REGULAR EXPRESSION (which is used by the regular grep command by default) pattern.
  2. g [ text pattern ] [ params for lf ... ]
    • All parameters except the 1st parameter obey the rule of lf command
  3. g
    • Will show help message

g uses lf behind the scene ( gi uses lfi; That's the only difference ) so g and gi commands have the same quirks that lf or lfi commands have.
See lf or lfi for more details

✔️ Examples

$ cat Documents/test/tmp.txt

Java
JavaScript

$ g [Jj]ava$ . Docu .txt

Documents/text/tmp.txt:1:Java

$ g

... help message ...

✔️ Using a Different Command Other Than GREP With _LIST_FILE_GREP_TOOL

g uses grep by default, but you can use other famous commands such as Ack, The Silver Searcher if they are already installed on your system.

e.g., if you want to use The Silver Searcher (ag) instead of grep, add the following line in your .bashrc or .bash_profile:

    type ag >/dev/null 2>&1 && export _LIST_FILE_GREP_TOOL=ag

To use Ack, use:

    type ack >/dev/null 2>&1 && export _LIST_FILE_GREP_TOOL=ack

Or you don't want either of them (you might be a grep fan!), and you are good at Extended Regular Expressions so much; In that case, use:

    export _LIST_FILE_GREP_TOOL=egrep

You can only assign one of grep/egrep/fgrep/ack/ag to _LIST_FILE_GREP_TOOL variable. Notice that other commands cannot be used. As a rule, if you assign another command than those tools, g will be forced to use grep. ( Using another tool other than ones above may be highly rare... )

When you use ag, make sure you install the latest version. See here for further discussion.

✔️ Overriding Behavior Of g Using _LIST_FILE_GREP_OPTIONS

g command invokes grep -n by default

You can override this default setting by defining _LIST_FILE_GREP_OPTIONS environment variable

For example, you can do as follows to make g accept Extended Regular Expression pattern

    $ _LIST_FILE_GREP_OPTIONS="-E -n"

Unset _LIST_FILE_GREP_OPTIONS as follows to restore the default setting

    $ unset _LIST_FILE_GREP_OPTIONS

Notice that making _LIST_FILE_GREP_OPTIONS empty as follows will have a complete different effect; ( It wont't provide any parameter for grep unlike the default setting which is grep -n )

    $ _LIST_FILE_GREP_OPTIONS=

If you set _LIST_FILE_GREP_TOOL variable to another command other than grep, then you may change _LIST_FILE_GREP_OPTIONS too.
In terms of command options, egrep or fgrep may be compatible to grep, but Ack or The Silver Searcher is not 100% compatible to grep. (e.g., -n option has a different meaning between grep and Ack/ag)

Here are default _LIST_FILE_GREP_OPTIONS values for each of commands you can use for _LIST_FILE_GREP_TOOL variable:

  • grep, egrep, fgrep : -n
  • Ack : -H
  • ag (Silver Searcher) : -H

See man page for each of these commands for what the options mean.

Also notice that g ignores GREP_OPTIONS variable so setting GREP_OPTIONS for other applications doesn't affect behavior of g
( See man page for grep about GREP_OPTIONS )