Skip to content

vroby65/fsel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

fsel

A small interactive file selector for Bash using fzf.

It lets you browse directories, open files with your preferred editor, and visually distinguish folders from files using colors.

Features

  • / always shown first to jump to the filesystem root
  • .. always shown second to go up one directory
  • Remaining files and directories sorted alphabetically
  • Directories shown in color
  • Files opened with $EDITOR
  • Optional starting directory as argument
  • Lightweight and easy to modify

Requirements

  • Bash
  • fzf
  • find
  • sort

Installation

Clone the repository or copy the script locally:

chmod +x fsel.sh

Make sure fzf is installed.

Debian / Ubuntu / Linux Mint

sudo apt install fzf

Usage

Run it with:

./fsel.sh

Or start from a specific directory:

./fsel.sh /path/to/start

Behavior

  • Selecting / moves to the root directory
  • Selecting .. goes to the parent directory
  • Selecting a directory enters it
  • Selecting a file opens it with $EDITOR

Example:

EDITOR=micro ./fsel.sh

If EDITOR is not set, the script uses nano by default.

Important note

This is a standalone Bash script, not a shell function.

Because of that, when you change directory inside the script, the directory change only affects the script itself, not the parent shell.

So this works:

  • browse directories inside the selector
  • open files from the selected location

But this does not persist after the script exits:

  • changing the current directory of your terminal session

If you want persistent cd behavior, use a shell function instead of a normal executable script.

Example script

#!/usr/bin/env bash
set -euo pipefail

EDITOR_CMD="${EDITOR:-nano}"

if [[ $# -gt 0 ]]; then
    cd -- "$1" || exit 1
fi

while true; do
    row="$(
        {
            printf '\033[1;35m/\033[0m\t/\n'
            printf '\033[1;35m..\033[0m\t..\n'

            find . -maxdepth 1 -mindepth 1 -printf '%P\n' | sort | while IFS= read -r item; do
                if [[ -d "$item" ]]; then
                    printf '\033[1;34m%s/\033[0m\t%s\n' "$item" "$item"
                else
                    printf '%s\t%s\n' "$item" "$item"
                fi
            done
        } | fzf --ansi --delimiter=$'\t' --with-nth=1 --height 40% --reverse --prompt="$(pwd) > "
    )"

    [[ -n "$row" ]] || exit 0

    sel="${row#*$'\t'}"

    case "$sel" in
        /)
            cd / || exit 1
            ;;
        ..)
            cd .. || exit 1
            ;;
        *)
            if [[ -d "$sel" ]]; then
                cd -- "$sel" || exit 1
            else
                "$EDITOR_CMD" "$sel"
                exit 0
            fi
            ;;
    esac
done

Customization ideas

You can easily extend it to:

  • color executable files differently
  • color symbolic links differently
  • preview files with fzf --preview
  • show directories before files
  • make sorting case-insensitive

License

MIT

About

File selector for fish and bash

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages