lf File Manager

repository·master·Indexed 27 days ago

https://github.com/gokcehan/lf

A fast, lightweight, terminal-based file manager written in Go and inspired by ranger. It features a server/client architecture for remote command management, asynchronous IO, and high extensibility through shell commands. lf is cross-platform, supporting Linux, macOS, BSDs, and Windows, and relies on external shell tools for file operations to maintain a small footprint.

Tokens
17.4K
Snippets
27
Records
108
Agent score
89%

What's inside lf

  1. Overview of lf features and limitations

    master

    Features

    • Cross-platform: Supports Linux, macOS, BSDs, and Windows.
    • Single binary: No runtime dependencies.
    • Performance: Fast startup and low memory footprint.
    • Asynchronous IO: Prevents UI locking during operations.
    • Architecture: Server/client architecture allowing remote commands to manage multiple instances.
    • Extensibility: Highly configurable via shell commands and customizable keybindings (defaults include vi and readline).

    Non-Features (External tools required)

    • Tabs/Windows: Handled by your terminal multiplexer or window manager.
    • Pager/Editor: Use your preferred external editor/pager.
    • File Operations: Relies on underlying shell tools (e.g., mkdir, touch, chmod, ln) rather than built-in commands.
  2. Configure lf File Locations

    master

    Depending on your OS, lf looks for configuration, colors, icons, and data files in specific locations. You can override these using environment variables like $LF_CONFIG_HOME or $LF_DATA_HOME.

    File TypeUnix (User)Windows (User)
    Configuration (lfrc)~/.config/lf/lfrc%APPDATA%\lf\lfrc
    Colors (colors)~/.config/lf/colors%APPDATA%\lf\colors
    Icons (icons)~/.config/lf/icons%APPDATA%\lf\icons
    Selection (files)~/.local/share/lf/files%LOCALAPPDATA%\lf\files
    Marks (marks)~/.local/share/lf/marks%LOCALAPPDATA%\lf\marks
    History (history)~/.local/share/lf/history%LOCALAPPDATA%\lf\history

    System-wide locations:

    • Unix: /etc/lf/lfrc, /etc/lf/colors, /etc/lf/icons
    • Windows: C:\ProgramData\lf\lfrc, C:\ProgramData\lf\colors, C:\ProgramData\lf\icons
  3. Install lf from source

    master

    You can build and install lf using Go. It is recommended to set CGO_ENABLED=0 to produce a static binary. Use the following commands based on your operating system:

    # On Unix
    env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/gokcehan/lf@latest
    
    # On Windows cmd
    set CGO_ENABLED=0
    go install -trimpath -ldflags="-s -w" github.com/gokcehan/lf@latest
    
    # On Windows PowerShell
    $env:CGO_ENABLED = '0'
    go install -trimpath -ldflags="-s -w" github.com/gokcehan/lf@latest
  4. Integrate lf with your shell to change directories

    master

    To allow lf to change your shell's working directory upon exit, use the -print-last-dir flag. This prints the last directory to stdout, which can be captured by a shell command.

    cd "$(lf -print-last-dir)"
  5. Use piping shell commands for UI feedback

    master

    Use the % prefix to run a shell command that pipes its stdin, stdout, and stderr to the lf statline at the bottom of the UI. This prevents the UI from flickering and allows users to see brief error messages or prompts without dropping to a full shell.

    Example: Rename with overwrite prompt in statline

    cmd rename %mv -i $f $1
  6. Search and filter files

    master

    Locate files using search, find, or filter commands:

    • Search (pattern matching):
      • /: Search forward.
      • ?: Search backward.
      • n: Next match.
      • N: Previous match.
    • Find (filename matching):
      • f: Find forward.
      • F: Find backward.
      • ;: Next match.
      • ,: Previous match.
    • Filter:
      • filter <pattern>: View only files matching the pattern.
      • setfilter <pattern>: Immediately set the filter using an argument.
  7. Manage file selections and clipboard

    master

    LF provides several commands to select, copy, and move files:

    • Selection:
      • v: Enter Visual mode to select files.
      • toggle: Toggle selection of current file or arguments.
      • u: Unselect all files in all directories.
      • invert: Reverse selection of all files in the current directory.
      • glob-select / glob-unselect: Select/unselect files matching a glob pattern.
    • Clipboard Operations:
      • y (copy): Save paths of selected files to the clipboard for copying.
      • d (cut): Save paths of selected files to the clipboard for moving.
      • p (paste): Copy/move files from the clipboard to the current directory.
      • c (clear): Clear the clipboard.
  8. Configure icons in lf

    master

    Icons can be configured using the LF_ICONS environment variable or an external icons file. The syntax follows the same pattern as LS_COLORS or LF_COLORS, but instead of colors, you use single characters or symbols as values.

    Key features:

    • The ln entry supports the special value target, which uses the link target to select an icon (mirroring GNU ls behavior).
    • An icons file consists of whitespace-separated arrays. Each line can contain 1-3 columns: file type/pattern, icon, and an optional icon color.
    • Using only one column disables all rules for that type or name.
    • Important: You must add set icons true to your lfrc to enable icon display.
  9. Use lf as an 'open file' dialog

    master

    You can use lf to select files and pass them to another program by using the -print-selection flag.

    1. Select the files you want.
    2. Confirm the selection by opening a file.
    3. lf will quit and print the newline-separated list of selected files to stdout.
  10. Display custom information using `on-load` and `addcustominfo`

    master

    The on-load command can be used to fetch metadata (like Git status or image dimensions) and display it in the lf info column using the addcustominfo command sent via lf -remote.

    # Example: Displaying image dimensions in the info column
    cmd on-load &{{
        cmds=""
    
        for file in "$@"; do
            mime=$(file --mime-type -Lb -- "$file")
            case "$mime" in
                # vector images cause problems
                image/svg+xml)
                    ;;
                image/*|video/*)
                    dimensions=$(exiftool -s3 -imagesize -- "$file")
                    cmds="${cmds}addcustominfo \"${file}\" \"$dimensions\"; "
                    ;;
            esac
        done
    
        if [ -n "$cmds" ]; then
            lf -remote "send $id :$cmds"
        fi
    }}
  11. Start lf at a specific path

    master
    You can specify a starting location by providing a positional argument. If the path is a directory, lf starts in that directory. If the path is a file, lf starts in the file's parent directory and selects that file. If no path is provided, lf uses the current working directory.
  12. Execute shell commands from LF

    master

    You can run shell commands directly from LF using modal commands:

    • $: Execute a shell command (modal).
    • %: Execute a shell command and pipe its standard I/O to the bottom statline.
    • !: Execute a shell command and wait for a key press at the end.
    • &: Execute a shell command asynchronously without standard I/O.

    To interrupt a running shell-pipe command, use <c-c>.