Fasd - Alternative Tool for Autojump to Quick Access Linux Filesystem

February 23, 2017 | By in OPEN SOURCE TOOLS
| Reply More

Fasd is a command-line productivity booster that offers quick access to linux files and directories for POSIX shells (Portable Operating System Interface). It is the similar tool like autojump, z and v used to navigate filesystem. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line. Its name fasd comes from the default suggested aliases f(files), a(files/directories), s(show/search/select), d(directories). Fasd ranks files and directories by "frecency," that is, by both "frequency" and "recency." The term "frecency" was first coined by Mozilla and used in Firefox. Frecency is a score given to each unique URI in Places, encompassing bookmarks, history and tags. This score is determined by the amount of revisitation, the type of those visits, how recent they were, and whether the URI was bookmarked or tagged. Similarly Fasd smartly detect when to display a list of files or just the best match. For instance, when you call fasd in a subshell with some search parameters, it will only return the best match.

If you use your shell to navigate and launch applications most frequently, fasd can help you do it more efficiently, let's see how are can install and use it on CentOS 7 Operating System.

Prerequisites:

To install and use Fasd, you need access to the shell of your UNIX/Linux like Operating System, like here we are using CentOS 7. Let's connect to your server using root or sudo user privileges.

Installing Fasd on CentOS 7

Fasd package is available in various package managers to install on any Linux distribution, while we can manually obtain a copy of fasd package available on Github.

Let's download the fasd package followed by link to download fasd package and place it on your system. Then run the command below to extract the package a Makefile is provided in it to install fasd and fasd.1 to desired places.

# tar -zxf clvv-fasd-1.0.1-0-g4822024.tar.gz

# cd clvv-fasd-4822024/

For the system wide installation use below command.

# make install

While if you wish to install fasd into some other location like '$HOME', then use below command or just copy the 'fasd' file to anywhere you want to place.

# PREFIX=$HOME make install

Now put the lines below in your shell '.bashrc' file to get fasd working in a shell, so that some initialization code must be run.

# vim .bashrc

eval "$(fasd --init auto)"

This will setup a command hook that executes on every command and advanced tab completion for bash and zsh. If you want more control over what gets into your shell environment, you can pass customized set of arguments to fasd --init. This will slightly increase your shell start-up time, since calling binaries has overhead. You can cache fasd init code if you want minimal overhead.

Following is an example code for bash to be placed into '.bashrc' .

# vim .bashrc

fasd_cache="$HOME/.fasd-init-bash"
if [ "$(command -v fasd)" -nt "$fasd_cache" -o ! -s "$fasd_cache" ]; then
fasd --init posix-alias bash-hook bash-ccomp bash-ccomp-install >| "$fasd_cache"
fi
source "$fasd_cache"
unset fasd_cache

Using Fasd on CentOS 7

After you first installed fasd, open some files with any program or change directories around in your shell. Below are some hypothetical situations, where you can type in the command on the left and fasd will "expand" your command into the right side.

v def conf => vim /some/awkward/path/to/type/default.conf
j abc => cd /hell/of/a/awkward/path/to/get/to/abcdef
m movie => mplayer /whatever/whatever/whatever/awesome_movie.mp4
o eng paper => xdg-open /you/dont/remember/where/english_paper.pdf
vim `f rc lo` => vim /etc/rc.local
vim `f rc conf` => vim /etc/rc.conf

For instance, when you call fasd in a subshell with some search parameters, fasd will only return the best match by smartly detecting when to display a list of files or just the best match.

By default Fasd comes with some useful aliases, which are given below.

alias a='fasd -a' # any
alias s='fasd -si' # show / search / select
alias d='fasd -d' # directory
alias f='fasd -f' # file
alias sd='fasd -sid' # interactive directory selection
alias sf='fasd -sif' # interactive file selection
alias z='fasd_cd -d' # cd, same functionality as j in autojump
alias zz='fasd_cd -d -i' # cd with interactive selection

The simplest case, let's assume that we are now in the home directory, create two directories 'foo' and 'bar' and move into these to create the files with same name.

# mkdir foo

# mkdir bar

# cd foo/

# touch foo

# touch bar

# cd ../bar/

Now you can use below fasd commands to move around.

# z foo

# z bar

Jump to the directory you want, and of course support tab implementation can also be used.

fasd

When you run 'fasd init' code or 'source fasd', fasd adds a hook which will be executed whenever you execute a command. The hook will scan your commands' arguments and determine if any of them refer to existing files or directories. If yes, fasd will add them to the database.

To use an interactive way to jump. Suppose we want to modify ~ / foo / bar this file, we only need to use below command.

~/bar # vim `f bar`

If we want to copy this bar to bar this directory, use the command below.

~/bar # cp `d foo`/bar .

There are now two bar under two different directories, and if you want to edit, you must specify more information for the batch.

~ bar # vim `f foo/bar`

Using Tab Completion

Fasd comes with two completion modes, one is the command mode completion and the second is the word mode completion. The command mode completion works in bash and zsh while the Word mode completion only works in zsh. The command mode completion is just like completion for any other commands. It is triggered when you hit tab on a fasd command or its aliases. Under this mode your queries can be separated by a space. While using if you find that the completion result overwrites your queries, type an extra space before you hit tab.

# vim ,rc,lo
# vim /etc/rc.local

# mv index.html d,www
# mv index.html /var/www/

It works with word completion triggered by any command line argument that starts with , (all), f, (files), or d, (directories), or that ends with ,, (all), ,,f (files), or ,,d (directories). There are also three zle widgets, that is fasd-complete, fasd-complete-f and fasd-complete-d you can bind them to keybindings as shown below.

Use C-x C-a to do fasd-complete (files and directories)

# bindkey '^X^A' fasd-complete

Use C-x C-f to do fasd-complete-f (only files)

# bindkey '^X^F' fasd-complete-f

Use C-x C-d to do fasd-complete-d (only directories)

# bindkey '^X^D' fasd-complete-d

Fasd can also use some additional backends if the data can be converted into fasd's native format by taking dvantage of different sources of recent / frequent files. You can also define your own backend by declaring a function by that name in your '.fasdrc' file.

Tweaking Fasd

In this last section of the article on fasd , we will show you that how we can tweak fasd for some shell variables that you can set before sourcing fasd.
You can make your required changes into '$HOME/.fasdrc' file.

$_FASD_DATA

Path to the fasd data file, default "$HOME/.fasd".

$_FASD_BLACKLIST

List of blacklisted strings. Commands matching them will not be processed. Default is "--help".

$_FASD_SHIFT

List of all commands that needs to be shifted, defaults to "sudo busybox".

$_FASD_IGNORE

List of all commands that will be ignored, defaults to "fasd ls echo".

$_FASD_TRACK_PWD

Fasd defaults to track your "$PWD". Set this to 0 to disable this behavior.

$_FASD_AWK

Which awk to use. Fasd can detect and use a compatible awk.

$_FASD_SINK

File to log all STDERR to, defaults to "/dev/null".

$_FASD_MAX

Max total score / weight, defaults to 2000.

$_FASD_SHELL

Which shell to execute. Some shells will run faster than others. fasd runs faster with dash and ksh variants.

$_FASD_BACKENDS

Default backends.

$_FASD_RO

If set to any non-empty string, fasd will not add or delete entries from database. You can set and export this variable from command line.

$_FASD_FUZZY

Level of "fuzziness" when doing fuzzy matching. More precisely, the number of characters that can be skipped to generate a match. Set to empty or 0 to
disable fuzzy matching. Default value is 2.

$_FASD_VIMINFO

Path to .viminfo file for viminfo backend, defaults to "$HOME/.viminfo"

$_FASD_RECENTLY_USED_XBEL

Path to XDG recently-used.xbel file for recently-used backend, defaults to "$HOME/.local/share/recently-used.xbel"
You can set "_FASD_SINK" in your .fasdrc to obtain a log.

$_FASD_SINK="$HOME/.fasd.log"

Conclusion:

Fasd keeps track of files you have accessed, so that you can quickly reference them in the command line. Fasd uses Bayesian Inference and Bayesian Ranking to rank files and directories for a set of given matching patterns. In this article you learned about its installation on CentOS 7 and its all possible usage and tweaks settings to make it more useful for you. I hope you enjoyed this article and like this awesome tool to use in your daily system administration activities.

Filed Under : LINUX HOWTO, OPEN SOURCE TOOLS

Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.