mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-02-14 15:19:00 +00:00
46 lines
19 KiB
HTML
46 lines
19 KiB
HTML
![]() |
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width"/><meta charSet="utf-8"/><title>Command line: fzf</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async="" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><script type="text/x-mathjax-config">
|
||
|
MathJax = {
|
||
|
tex: {
|
||
|
inlineMath: [ ['$','$'], ['\(','\)'] ],
|
||
|
displayMath: [ ['$$','$$'], ['[',']'] ]
|
||
|
},
|
||
|
options = {
|
||
|
processHtmlClass = "math"
|
||
|
}
|
||
|
}
|
||
|
</script><meta name="next-head-count" content="6"/><link rel="preload" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" as="style"/><link rel="stylesheet" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" data-n-g=""/><noscript data-n-css=""></noscript><link rel="preload" href="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" as="script"/></head><body><div id="__next"><main><h1>Command line: fzf</h1><section class="post-metadata"><span>Date: <!-- -->2021-05-31 23:08:57 +08:00</span><span>Date modified: <!-- -->2021-07-27 12:38:25 +08:00</span></section><nav class="toc"><ol class="toc-level toc-level-1"><li class="toc-item toc-item-h1"><a href="/wiki/cli.fzf#options" class="toc-link toc-link-h1">Options</a></li><li class="toc-item toc-item-h1"><a href="/wiki/cli.fzf#examples" class="toc-link toc-link-h1">Examples</a><ol class="toc-level toc-level-2"><li class="toc-item toc-item-h2"><a href="/wiki/cli.fzf#basic-command-line-file-selection" class="toc-link toc-link-h2">Basic command-line file selection</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cli.fzf#quick-manual-page-selection" class="toc-link toc-link-h2">Quick manual page selection</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cli.fzf#goto-directory-with-fzf-and-bash" class="toc-link toc-link-h2">Goto directory with fzf and Bash</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cli.fzf#package-selection-in-arch-linux" class="toc-link toc-link-h2">Package selection in Arch Linux</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cli.fzf#create-an-interactive-tldr-list" class="toc-link toc-link-h2">Create an interactive tldr list</a></li></ol></li></ol></nav><p>The family's favorite fuzzy finder.
|
||
|
Basically, it takes a list then create an interface out of it.
|
||
|
Not only it is easy to use and very flexible but also very configurable with the options to let you choose the keybindings and the previewer among other things.
|
||
|
</p><h1 id="options">Options</h1><ul><li><p><code class="inline-verbatim">-m, --multi</code> - enables multiple selection; by default, the keybinding to select is <code class="inline-verbatim">Tab</code></p></li><li><p><code class="inline-verbatim">-p, --prompt</code> - display the prompt message
|
||
|
</p></li><li><p><code class="inline-verbatim">--disabled</code> - disable the search making fzf essentially a selection interface
|
||
|
</p></li><li><p><code class="inline-verbatim">--cycle</code> - enables cyclic scrolling in the selection (it's annoying when it does not have those)
|
||
|
</p><p> If no input was passed, it will recursively list all of the files in the current directory.
|
||
|
Pretty useful for a quick opening interface.
|
||
|
</p></li></ul><h1 id="examples">Examples</h1><p>My favorite section where I get to show off some scripts featuring the star of the show.
|
||
|
And also this is where everyone is going to go first.
|
||
|
</p><h2 id="basic-command-line-file-selection">Basic command-line file selection</h2><pre class="src-block"><code class="language-bash">fzf | xargs xdg-open
|
||
|
</code></pre><h2 id="quick-manual-page-selection">Quick manual page selection</h2><pre class="src-block"><code class="language-shell">apropos . \
|
||
|
| fzf --multi --prompt "Choose manual(s) to open > " \
|
||
|
| awk '{ print $1 "." gensub(/[()]/, "", "g", $2) }' \
|
||
|
| xargs man
|
||
|
</code></pre><p>Since a manual page can have the same title in different sections (e.g., <code class="inline-verbatim">tput.1</code> and <code class="inline-verbatim">tput.1p</code>), it is necessary to extract the sections.
|
||
|
</p><h2 id="goto-directory-with-fzf-and-bash">Goto directory with fzf and Bash</h2><p>With my favorite <code class="inline-verbatim">find</code> replacement, <a href="https://github.com/sharkdp/fd"><code class="inline-verbatim">fd</code></a>.
|
||
|
It's a Bash function since <a href="https://stackoverflow.com/a/255415">you can't propagate directory changes with a script</a>.
|
||
|
Just have to put it somewhere in your configuration.
|
||
|
</p><pre class="src-block"><code class="language-bash">function fzf-cd() {
|
||
|
local dir=${1:-$(pwd)}
|
||
|
local dest=$(fd --type directory --hidden --base-directory "$dir" --follow | fzf --prompt "Where to go? > ")
|
||
|
[[ $dest ]] && cd "$(realpath --logical "$dir")/$dest"
|
||
|
}
|
||
|
</code></pre><p>Even better when you bind it with a keyboard shortcut (e.g., <code class="inline-code">bind '"\C-f":'fzf-cd\n"'</code> on your Bash config).
|
||
|
</p><h2 id="package-selection-in-arch-linux">Package selection in Arch Linux</h2><p>Vanilla Arch, if that matters.
|
||
|
You can do this on other operating systems as long as their package manager lets you list all of the packages either from your local database or from a remote server.
|
||
|
</p><pre class="src-block"><code class="language-shell">pacman -S --list --quiet \
|
||
|
| fzf --multi --prompt "Install package(s) > " \
|
||
|
| xargs doas pacman -S --noconfirm
|
||
|
</code></pre><h2 id="create-an-interactive-tldr-list">Create an interactive tldr list</h2><p>Have a <a href="https://github.com/tldr-pages/tldr/wiki/tldr-pages-clients">tldr client</a> (e.g., tealdeer) and <a href="https://github.com/sharkdp/bat">bat</a> installed.
|
||
|
</p><pre class="src-block"><code class="language-shell">tldr --list \
|
||
|
| fzf --multi \
|
||
|
| xargs --replace="{}" tldr {} \
|
||
|
| bat
|
||
|
</code></pre><section><h2>Backlinks</h2><ul><li><a href="/wiki/cli.borg">Command line: BorgBackup</a></li><li><a href="/wiki/cli.flatpak">Command line: Flatpak</a></li><li><a href="/wiki/cli.pacman">Command line: pacman</a></li><li><a href="/wiki/cli.podman">Command line: Podman</a></li></ul></section></main></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"metadata":{"date":"\"2021-05-31 23:08:57 +08:00\"","date_modified":"\"2021-07-27 12:38:25 +08:00\"","language":"en","source":""},"title":"Command line: fzf","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"options"},"children":[{"type":"text","value":"Options"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/cli.fzf#options"},"children":[{"type":"text","value":"Options"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"examples"},"children":[{"type":"text","value":"Examples"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/cli.fzf#examples"},"children":[{"type":"text","value":"Examples"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"basic-command-line-file-selection"},"children":[{"type":"text","value":"Basic command-line file selection"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cli.fzf#basic-command-line-file-selection"},"children":[{"type":"text","value":"Basic command-line file selection"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"quick-manual-page-selection"},"children":[{"type":"text","value":"Quick manual page selection"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cli.fzf#quick-manual-page-selection"},"children":[{"type":"text","value":"Quick manual page selection"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"goto-directory-with-fzf-and-bash"},"children":[{"type":"text","value":"Goto directory with fzf and Bash"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cli.fzf#goto-directory-with-fzf-and-bash"},"children":[{"type":"text","value":"Goto directory with fzf and Bash"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"package-selection-in-arch-linux"},"children":[{"type":"text","value":"Package selection in Arch Linux"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cli.fzf#package-selection-in-arch-linux"},"children":[{"type":"text","value":"Package selection in Arch Linux"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"create-an-interactive-tldr-list"},"children":[{"type":"text","value":"Create an interactive tldr list"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cli.fzf#create-an-interactive-tldr-list"},"children":[{"type":"text","value":"Create an interactive tldr list"}]}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"child
|