Home

Efficient file management with lf

Written on 2020-05-25

lf enables very efficient file management workflows when combined with other tools. In this article, I will explain how I use it alongside sxhkd, fzf and dragcli.

sxhkd

First of all, I use sxhkd for my keybinds. Here is the relevant excerpt:

control + alt + r
    $TERMINAL -e lf
Not only is it fast to start, it is also fast to quit: since I run lf in a shell like bash, the window disappears as soon as I hit q. Note that this keybinding only makes sense because I swap the Control and Caps Lock keys.

I do also invoke lf from my shell when I need to peek at/manage files in the context of a long running session and I know that I will go back to my shell shortly after. I also use it to change directory when I need to go far from the current directory (otherwise, typing the path is often faster). To enable that, I have the following snippet in my .bashrc:

lf() {
    tmp="$(mktemp)"
    command lf -last-dir-path="$tmp" "$@"
    cd "$(cat "$tmp")"
    rm -f "$tmp"
}
This way, my shell's directory is changed for the one in which I left lf.

Usage

lf can be navigated using hjlk or the arrow keys. Files can be selected with the Space key. Hitting l or the right arrow key will open the currently selected file(s). They can then be copied (yanked) with y or cut with d and then pasted elsewhere with p. It is not necessary to select with Space to operate on a single file. A copy/cut can be cleared with c. A file can be renamed with r. v will invert the current selection, which is useful to select all/most of the files in a directory. It is possible to start a search by hitting /, to stop it by hitting Enter or to cancel it by hitting Escape.

Because lf has a client/server architecture, it is possible to copy files from one instance of it and paste them in another one.

It is possible to enter commands by hitting :. Available commands include doc, delete and many others. These commands support Tab completion with a nice popup menu.

Configuration

lf is configured via ~/.config/lf/lfrc. Here are some general options:

set shell sh
set shellopts '-eu'
set ifs "\n"
set drawbox
set scrolloff 10
set incsearch
set ignoredia
set period 2
set ratios 1:2:2
You can type :doc in lf to find a description of what those do as I will only go over the most interesting ones:

fzf

I integrate fzf with the following snippet:

map f ${{
    selected="$(fzf)"
    [ -d "$selected" ] && cmd=cd || cmd=select
    lf -remote "send $id $cmd '$selected'"
}}
This allows me to just hit the f key, start typing and end up exactly where I want to be when I hit Enter. It either selects the result if it is a normal file or enters it if it is a directory.

Dealing with compressed files

I use the following commands to deal with compressed files:

map x extract

cmd extract ${{
    set -f
    case $f in
        *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;;
        *.tar.gz|*.tgz) tar xzvf $f;;
        *.tar.xz|*.txz) tar xJvf $f;;
        *.zip) unzip $f;;
        *.rar) unrar x $f;;
        *.7z) 7z x $f;;
    esac
}}

cmd tar ${{
    set -f
    mkdir $1
    cp -r $fx $1
    tar czf $1.tar.gz $1
    rm -rf $1
}}

cmd zip ${{
    set -f
    mkdir $1
    cp -r $fx $1
    zip -r $1.zip $1
    rm -rf $1
}}
This enables me to just hit x on any compressed file to decompress, without having to think about command line options. I can also use :zip myarchive or :tar myarchive to package the selected files into a compressed archive (respectively myarchive.zip or myarchive.tar.gz).

Dealing with drag and drop

Drag and drop is often a pain point when working from a terminal. I have already explain how I tackle it in another article. Together with the fzf integration and the archiving bindings, this makes uploading files to website a breeze.

Miscellaneous binding

I use a few other minor bindings to make my life a little easier: