Learning Linux commands are getting easier day by day! If you know how to use man pages properly, you are halfway across Linux commandline journey. There are also some good man page alternatives available which helps you to display Linux commands cheatsheets. Unlike the man pages, these tools will only display concise examples for most commands and exclude all other theoretical part. Today, let us discuss one more useful addition to this list. Say hello to eg, a command line cheatsheet tool to display useful examples for Linux commands.
Table of Contents
What Is Eg?
Eg provides practical examples for many Linux and Unix commands. If you want to quickly find out examples of a specific Linux command without going through the lengthy man pages, eg is your companion.
Just run eg followed by the name of the command and get the concise examples of the given command right at the Terminal window. It is that simple!
Eg is a free, open source program written in Python language and the code is freely available in GitHub.
For those wondering, eg comes from the Latin word "Exempli Gratia" that literally means "for the sake of example" in English. Exempli Gratia is known by its abbreviation e.g., in English speaking countries.
Install Eg In Linux
Eg can be installed using Pip package manager. If Pip is not available in your system, install it as described in the below link.
After installing Pip, run the following command to install eg on your Linux system:
$ pip install eg
Display Linux commands Cheatsheets Using Eg
Let us start by displaying the help section of eg program. To do so, run eg without any options:
$ eg
Sample output:
usage: eg [-h] [-v] [-f CONFIG_FILE] [-e] [--examples-dir EXAMPLES_DIR] [-c CUSTOM_DIR] [-p PAGER_CMD] [-l] [--color] [-s] [--no-color] [program] eg provides examples of common command usage. positional arguments: program The program for which to display examples. optional arguments: -h, --help show this help message and exit -v, --version Display version information about eg -f CONFIG_FILE, --config-file CONFIG_FILE Path to the .egrc file, if it is not in the default location. -e, --edit Edit the custom examples for the given command. If editor-cmd is not set in your .egrc and $VISUAL and $EDITOR are not set, prints a message and does nothing. --examples-dir EXAMPLES_DIR The location to the examples/ dir that ships with eg -c CUSTOM_DIR, --custom-dir CUSTOM_DIR Path to a directory containing user-defined examples. -p PAGER_CMD, --pager-cmd PAGER_CMD String literal that will be invoked to page output. -l, --list Show all the programs with eg entries. --color Colorize output. -s, --squeeze Show fewer blank lines in output. --no-color Do not colorize output.
You can also bring the help section using this command too:
$ eg --help
Now let us see how to view example commands usage.
To display cheatsheet of a Linux command, for example grep
, run:
$ eg grep
Sample output:
grep print all lines containing foo in input.txtgrep "foo" input.txt
print all lines matching the regex "^start" in input.txtgrep -e "^start" input.txt
print all lines containing bar by recursively searching a directorygrep -r "bar" directory
print all lines containing bar ignoring casegrep -i "bAr" input.txt
print 3 lines of context before and after each line matching "foo"grep -C 3 "foo" input.txt
Basic Usage Search each line ininput_file
for a match againstpattern
and print matching lines:grep "<pattern>" <input_file>
[...]
As you see in the above output, eg displays example commands for grep
utility along with a brief description. No need to go through long man pages, no need to refer any flags. You will get examples for the given command instantly.
You can even get the examples for eg command as well:
$ eg eg
Change Pager
By default, Eg uses less
pager to display the command examples page by page if the entire output doesn't fit on the screen. Hit ENTER key to navigate through all examples in the subsequent pages.
You can also use different pager using --pager-cmd
option. For example, to use cat
as a pager, run:
$ eg grep --pager-cmd=cat
This will show the entire output in a single page on the screen.
To change pager permanently, you need to set the pager in eg configuration file.
Edit eg configuration file:
$ nano ~/.egrc
Note: If the configuration file doesn't exist, create it.
$ touch ~/.egrc
And then open the eg config file in a text editor and add the following lines in it:
[eg-config] --pager-cmd=cat
Save the file and close it. From now on, eg will use cat
command as pager.
List Available Commands
The developer and all other contributors of eg project have added many examples for each command. You can view the list of all available commands using command:
$ eg --list
As of writing this guide, eg provides examples for 85 command line utilities.
$ eg --list | wc -l 85
Edit Commands
If you want to add more examples to a command, simply pass -e
flag to edit the default set of commands and add your own commands.
Before adding/editing custom commands, create a directory to save the custom commands. This is the directory where you are going to save all your custom commands.
$ mkdir ~/.eg/
Next, edit the eg configuration file:
$ nano ~/.egrc
Add the following lines in it:
[eg-config] custom-dir = ~/.eg/
Save the file and close it. Now you can edit any command and add your custom examples.
For instance, to edit grep
command examples, run:
$ eg -e grep
This will open your default editor. Add the examples and save them. The newly added commands will be shown before the default examples the next time you run eg grep
command.
Eg is highly customizable. You can change the colors in the output, remove the empty lines in the output, and regex substitutions etc. Here is a sample egrc file with every option specified:
[eg-config] # Lines starting with # are treated as comments examples-dir = /path/to/examples/dir custom-dir = /path/to/custom/dir color = true squeeze = true pager-cmd = 'less -R' [color] pound = '\x1b[30m\x1b[1m' heading = '\x1b[38;5;172m' code = '\x1b[32m\x1b[1m' prompt = '\x1b[36m\x1b[1m' backticks = '\x1b[34m\x1b[1m' pound_reset = '\x1b[0m' heading_reset = '\x1b[0m' code_reset = '\x1b[0m' prompt_reset = '\x1b[0m' backticks_reset = '\x1b[0m' [substitutions] # This will remove all four-space indents. remove-indents = ['^ ', '', True]
Many man pages doesn't provide example commands. Also some man pages are very long. Eg is one of the good alternative to man pages. Instead of scrolling through a lengthy man page, you can quickly find an example of given command instantly.
Since there is no command named woman in Linux, why don't you just alias eg
to woman?
$ alias woman=eg $ man grep $ woman grep
For more details, refer the official GitHub repository of eg utility given below.
Resource:
2 comments
Very good! Can replace many cheat sheets.
May God pay you with a son dude :- 1