The Problem: “cd” & “ls” Suck 👎
Using cd
command to change the directory and ls
to preview files in the current directory may work sometimes, but it’s frustrating to have to use this workflow all the time whenever you want to go somewhere.
The Solution
I will give you 3 solutions not just one to make your life easier in terminal and jump easily between files.
“Give me six hours to chop down a tree, and I will spend the first four sharpening the axe.” — Abraham Lincoln
Let’s Sharpen Our Axe 🪓
You’re gonna need some perquisites to follow with me:
- Zsh
- Tmux
- Zoxide
- LF
- Fzf
Solution 1: Zoxide
zoxide is a smarter cd command
This one is pretty cool; here’s what it does:
- It remembers all directories you visit and organizes them by most frequent.
- All you need is to type some letters from the name
des => Desktop
- It jumps immediately there (super super cool)
Step 1: Install It With Homebrew
Brew install zoxide.
Step 2: Initialize in ~/.zshrc
- open your ~/.zshrc file, and add this to initialize it
eval "$(zoxide init zsh)"
- source ~/.zshrc to apply the changes
source ~/.zshrc
Step 3: Use Zoxide Instead of “cd”
- First, you need to visit the directories you want to jump to, so zoxide can identify it and fuzzy find it.
- Type
z
+ any letters you remember from the directory.
z desk// cd ~/Desktop
Solution 2: LF Manager
lf
(as in "list files") is a terminal file manager written in Go with a heavy inspiration from ranger file manager
Let me tell you a secret; this is my favorite one 🤫
- if you’re like me, using Vim navigation keys hjkl
- After you get used to it, you suddenly want to use them everywhere.
- LF does exactly that; it works like columns view in Finder but with Vim.
- But don’t get me wrong; it still works with regular “Arrow” keys as well.
Step 1: Install It With Homebrew
Brew install lf.
Step 2: Change the Directory on Quit
“ it’s funny, but for some reason, it didn’t change the directory on quit”
- It’s like the main point, right !! 🥲
- So, let’s fix this with a small function working as a wrapper to lf.
- It just saves the last directory path in the temp file, then cd to it.
Add this function to your~/.zshrc
lf Wrapper
function lf { local tmp=$(mktemp)
command lf -last-dir-path="$tmp" "$@" cd "$(cat "$tmp")" rm -r "$tmp" }
Step 3: Customize Your “lfrc” File
It’s the LF config file to customize it; I like to keep it minimal.
- few UI interface configurations
- shortcuts to jump to my frequent main directories
- with a small cheatsheet for favorite LF shortcuts
you’re gonna find it at ~/.config/lf/lfrc
( if you can’t find it, create it )
| |/ | ___ ___ _ __ / () __ _ | | | / / _ | ' | || |/ ` | | | | | (| () | | | | | | (| | ||| */|| ||| |*|, | |_/
UI
Set hidden # show hidden files set icons # show icons for files/directories set relativenumber # show files/directories relative numbers
Jump Shortcuts
map gh cd ~ # use 'gh' to go home directory map gp cd ~/personal/ # use 'gp' to go personal directory map gw cd ~/work/ # use 'gw' to go work directory map gt cd ~/tools/ # use 'gt' to go tools directory map gd cd ~/Downloads/ # use 'gd' to go downloads directory map gv cd /Volumes/ # use 'gv' to go volumes for (external drives) map gb cd ~/brainExt # use 'gb' to go obsidian BrainExt vault
Fav Shortcuts
hjkl/gg/G # Vim-like Navigation (move)
space/v/u # select/invert/unselect (select)
y/d/p/c # yank/delete/paste/clear (change)
i/e/l # open with less/default-editor/default-app (open)
change your default editor in ~/.zshrc
- To make sure that when you use
e
shortcut, it always opens with it.
- Then verify that your default editor changed:
echo $EDITOR
export EDITOR=vi # change default editor to vim
Step 4: Cool Tmux Keybinding
Imagine this scenario: you’re working on a file in Tmux, then do a quick shortcut open lf
in new window to open any file from the same directory 🚀
- Let’s go do exactly that, in
~/.tmux.conf
- But first, make sure that zsh is our default shell for the tmux conf
Set the Default Shell to zsh
set-option -g default-shell /bin/zsh
- this smart keybinding
ctrl+f
opens a new window with the name “Files.”
- The new window starts from the same path as well as your current window.
- And finally, it opens
lf
directly there ( use q to quit )
ctrl+f => lf (q)
bind -n C-f new-window -n "files" -c "#{pane_current_path}" "zsh -ic 'lf; zsh'"
- Don’t forget to source
~/.tmux.conf
tmux source-file ~/.tmux.conf
Solution 3: Tmux-fzf Script
For this one, we don’t just use another utility. We build our own tmux manager script to fuzzy find all our project files in specific directories and jump. 🦘
Step 1: Organize Your Project Files in Specific Directories
For example
- ~/personal
- ~/work
- ~/tools
Step 2: Build “Tmux Manager” Bash Script
- Create a new file with the name
tmux_manager.sh
in the tools directory.
- Change this file into an executable:
chmod +x tmux_manager.sh
- This script lists all projects in those directories to fuzzy find.
- Finally, open the selected project as Tmux Session (or create it if not found)
#!/usr/bin/env bash
| | _ __ ___ _ ___ __| | ' ` _ | | | \ / / | || | | | | | || |> <|| || ||,//_\
use fuzzy finder to get my working directories paths
session=$(find ~ ~/personal ~/work/ ~/tools -mindepth 1 -maxdepth 1 -type d | fzf)
get last part as a name & replace any (.) with (_)
session_name=$(basename "$session" | tr . _)
if the session doesn't already exist, create it.
if ! tmux has-session -t "$session_name" 2> /dev/null; then # remove err msg to null tmux new-session -s "$session_name" -c "$session" -d fi
switch to the session by name
tmux switch-client -t "$session_name"
Step 3: Create an Alias to Our “Tmux Manager” Script
- open ~/.zshrc, to add our new Alias
- Don’t forget to source it 😉
source ~/.zshrc
alias tm="~/tools/tmux_manager.sh"
Step 4: Cooool tmux keybinding for “Tmux Manager”
- The same trick we did before with
lf.
- A quick shortcut
ctrl+t
will run our tmux manager script alias.
- It will open in a new tmux window; a big list of all projects.
- Your selected project will open in its own Tmux session.
- Tell me that this isn’t cool ?! 🙃
bind -n C-t new-window -n "tmux" "zsh -ic 'tm; zsh'"
- don’t forget to source
~/.tmux.conf
tmux source-file ~/.tmux.conf