A Blazing Fast File Management for Terminal ⚡️

Written by belalsamyyy | Published 2024/03/20
Tech Story Tags: terminal | dotfiles | file-manager | tmux | zsh | file-management | lf | fzf

TLDR“cd” and “ls” 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. I will give you 3 solutions not just one to make your life easier in terminal.via the TL;DR App

The Problem: “cd” & “ls” sucks 👎

keep using cd command to change directory and ls to preview files in current directory, may work some times 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 gonna need someperquisites to follow with me

  • Zsh
  • Tmux
  • Zoxide
  • LF
  • Fzf

Solution 1: Zoxide

zoxide is a smarter cd command

this one is pretty cool, here is what it’s doing:

  • it remember all directories you visit, organize them by most frequent
  • all you need is to type some letters from the name des => Desktop
  • it jump 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 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, do exactly that, it works like columns view in Finder but with vim
  • But don’t get me wrong, it still work with regular “Arrow” keys as well

Step 1: Install it with Homebrew

brew install lf

Step 2: Change directory on quit

“ it’s funny, but for some reason, it didn’t change directory on quit”

  • It’s like the main point, right !! 🥲
  • so lets fix this with small function work as wrapper to lf
  • it just save last directory path in temp file, then cd to it


Add this function to your~/.zshrc

# lf wrapper 
function lf {
  local tmp=$(mktemp)

  precmd() {
    echo "$PWD" > "$tmp"
    precmd_functions=()
  }

  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 small cheatsheet for favorite LF shortcuts

you gonna find 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, when you use e shortcut, it always open 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 file in Tmux, then do quick shortcut open lf in new window, to open any file from same directory 🚀

  • Lets go do exactly that, in ~/.tmux.conf
  • But first, make sure that zsh is our default shell for tmux conf

# set default shell to zsh 
set-option -g default-shell /bin/zsh

  • this smart keybinding ctrl+f, open new window with name “Files”
  • The new window start from same path as well as your current window
  • And finally it open 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

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 new file with name tmux_manager.sh in tools directory
  • change this file into executable: chmod +x tmux_manager.sh
  • This script list all projects, in those directories to fuzzy find
  • Finally, open 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 name & replace any (.) with (_)
session_name=$(basename "$session" | tr . _)

# if session isn't already existed, 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 session by name 
tmux switch-client -t "$session_name"

Step 3: Create 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
  • quick shortcut ctrl+t will run our tmux manager script alias
  • it will open in new tmux window, big list of all projects
  • your selected project will open in it’s 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


Let’s be friends on twitter 👋

https://twitter.com/belalsamyyy


Written by belalsamyyy | Software Engineer | iOS
Published by HackerNoon on 2024/03/20