⚡️ Navigate and search faster in CLI!

December 26, 2021

Finding files or commands when using CLI might be time-consuming. Constant listing files when switching directories might give you a headache. Moving down the repository tree to find a file nested 5 levels deep also requires quite an effort. But what if I will tell you that there is a much more comfortable way to do it? In this article, I will show you how you can improve and save time during those essential operations!

Smarter change directory

zoxide is a smarter cd command, inspired by z and autojump. It remembers which directories you use most frequently, so you can “jump” to them in just a few keystrokes.

Zoxide will learn while you are changing directories and then will pick the best match using its algorithm that gives certain scores for paths depending on when the path was accessed and how frequently it was revisited. If the path is not used for a specific period then it’s removed from the database.

# cd into a path.
[email protected] 📂 ~/ → cd /some/long/path/foo
[email protected] 📂 ~/some/long/path/foo

# zoxide will remember it next time.
[email protected] 📂 ~/ → z foo
[email protected] 📂 ~/some/long/path/foo

Find zoxide on GitHub by clicking here!

Faster commands and file search

fzf is a general-purpose command-line fuzzy finder. It’s an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

The first handy shortcut that you are likely aware of is the reverse search history of executed commands. The default one that comes with your shell is very limited and slow. The one introduced by this tool is fast, looks awesome, and allows you to move the cursor up and down using CTRL-K and CTRL-J. It also comes with search syntax that lets you use exact, prefix, suffix, inverse match, and many more that you can find here.

ctrl + r - find command in the history that contains the specified phrase or expression

[email protected] 📂 ~/<ctrl+r> 

  152 aws ssm start-session --target i-0bdcbec3
167 aws sts get-caller-identity
  248 vim ~/.aws/config

[email protected] 📂 ~/ → aws sts get-caller-identity

You can search for files and directories too! Especially useful when you have multiple directories and want to find the specific file without searching it in the whole repository.

ctrl + t - open search console and find files down the dir you are in.

[email protected] 📂 ~/ → cp <ctrl+t> 

4/14 (0) 
▻  _templates/main.tf

[email protected] 📂 ~/ → cp _templates/main.tf acme-prod

Find fzf on GitHub by clicking here!

Suggestions base on bash history

Fish-like fast/unobtrusive autosuggestions for zsh. It suggests commands as you type based on history and completions. As you type commands, you will see a completion offered after the cursor in a muted gray color.

This will require you to use zsh as your shell. It’s helpful to see suggestions based on the history. For instance, when you run a complex command and you want to run it again, you can just type the first character and it will appear as the suggestion, then a quick jump to the end of the line and you can execute it once again! You can also accept it partially and e.g change the last part of it.

[email protected] 📂 ~/ → terraform plan -var-file config.tfvars

# Next time you will see the suggestion

[email protected] 📂 ~/ → terraform plan -var-file config.tfvars

If you press the → key (forward-char widget) or End (end-of-line widget) with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion.

Find autosuggestions on GitHub by clicking here!

Expand alias to know what you are executing

It might occur that you will forget what alias you typed means or you have an alias that you might want to modify before execution. In both cases, you want to expand it and you can easily do so with globalias plugin.

  • use space and expand your alias before executing it
# aliases
→ d<space> 👉 docker
→ gco<space> 👉 git checkout

# glob expressions
→ touch {1..3}<space> 👉 touch 1 2 3

Add globalias to the plugins array in your .zshrc file. It’s already inside a default ohmyzsh plugins directory so there is no need to install anything.

plugins=(... globalias)

Find globalias plugin in ohmyzsh repo by clicking here!

It’s all for this post. I hope you found that interesting.


Copyright © Damian Budelewski 2022