mirror of
https://github.com/foo-dogsquared/wiki.git
synced 2025-01-31 01:57:54 +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":{},"children":[{"type":"text","value":"The family's favorite fuzzy finder.\nBasically, it takes a list then create an interface out of it.\nNot 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.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"options"},"children":[{"type":"text","value":"Options"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"-m, --multi"}]},{"type":"text","value":" - enables multiple selection; by default, the keybinding to select is "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"Tab"}]}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"-p, --prompt"}]},{"type":"text","value":" - display the prompt message\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"--disabled"}]},{"type":"text","value":" - disable the search making fzf essentially a selection interface\n"}]}]},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"--cycle"}]},{"type":"text","value":" - enables cyclic scrolling in the selection (it's annoying when it does not have those)\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":" If no input was passed, it will recursively list all of the files in the current directory.\n Pretty useful for a quick opening interface.\n"}]}]}]},{"type":"element","tagName":"h1","properties":{"id":"examples"},"children":[{"type":"text","value":"Examples"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"My favorite section where I get to show off some scripts featuring the star of the show.\nAnd also this is where everyone is going to go first.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"basic-command-line-file-selection"},"children":[{"type":"text","value":"Basic command-line file selection"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"fzf | xargs xdg-open\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"quick-manual-page-selection"},"children":[{"type":"text","value":"Quick manual page selection"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"apropos . \\\n | fzf --multi --prompt \"Choose manual(s) to open \u003e \" \\\n | awk '{ print $1 \".\" gensub(/[()]/, \"\", \"g\", $2) }' \\\n | xargs man\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since a manual page can have the same title in different sections (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"tput.1"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"tput.1p"}]},{"type":"text","value":"), it is necessary to extract the sections.\n"}]},{"type":"element","tagName":"h2","properties":{"id":"goto-directory-with-fzf-and-bash"},"children":[{"type":"text","value":"Goto directory with fzf and Bash"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With my favorite "},{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" replacement, "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/sharkdp/fd"},"children":[{"type":"element","tagName":"code","properties":{"className":["inline-verbatim"]},"children":[{"type":"text","value":"fd"}]}]},{"type":"text","value":".\nIt's a Bash function since "},{"type":"element","tagName":"a","properties":{"href":"https://stackoverflow.com/a/255415"},"children":[{"type":"text","value":"you can't propagate directory changes with a script"}]},{"type":"text","value":".\nJust have to put it somewhere in your configuration.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-bash"]},"children":[{"type":"text","value":"function fzf-cd() {\n local dir=${1:-$(pwd)}\n local dest=$(fd --type directory --hidden --base-directory \"$dir\" --follow | fzf --prompt \"Where to go? \u003e \")\n [[ $dest ]] \u0026\u0026 cd \"$(realpath --logical \"$dir\")/$dest\"\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even better when you bind it with a keyboard shortcut (e.g., "},{"type":"element","tagName":"code","properties":{"className":["inline-code"]},"children":[{"type":"text","value":"bind '\"\\C-f\":'fzf-cd\\n\"'"}]},{"type":"text","value":" on your Bash config).\n"}]},{"type":"element","tagName":"h2","properties":{"id":"package-selection-in-arch-linux"},"children":[{"type":"text","value":"Package selection in Arch Linux"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Vanilla Arch, if that matters.\nYou 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.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"pacman -S --list --quiet \\\n | fzf --multi --prompt \"Install package(s) \u003e \" \\\n | xargs doas pacman -S --noconfirm\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"create-an-interactive-tldr-list"},"children":[{"type":"text","value":"Create an interactive tldr list"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/tldr-pages/tldr/wiki/tldr-pages-clients"},"children":[{"type":"text","value":"tldr client"}]},{"type":"text","value":" (e.g., tealdeer) and "},{"type":"element","tagName":"a","properties":{"href":"https://github.com/sharkdp/bat"},"children":[{"type":"text","value":"bat"}]},{"type":"text","value":" installed.\n"}]},{"type":"element","tagName":"pre","properties":{"className":["src-block"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-shell"]},"children":[{"type":"text","value":"tldr --list \\\n | fzf --multi \\\n | xargs --replace=\"{}\" tldr {} \\\n | bat\n"}]}]}]},"backlinks":[{"path":"/cli.borg","title":"Command line: BorgBackup"},{"path":"/cli.flatpak","title":"Command line: Flatpak"},{"path":"/cli.pacman","title":"Command line: pacman"},{"path":"/cli.podman","title":"Command line: Podman"}]},"__N_SSG":true},"page":"/[[...slug]]","query":{"slug":["cli.fzf"]},"buildId":"Ie9t5zutrXP6Of75Cb5xF","assetPrefix":"/wiki","nextExport":false,"isFallback":false,"gsp":true}</script><script nomodule="" src="/wiki/_next/static/chunks/polyfills-99d808df29361cf7ffb1.js"></script><script src="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" async=""></script><script src="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" async=""></script><script src="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" async=""></script><script src="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" async=""></script><script src="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" async=""></script><script src="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" async=""></script><script src="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" async=""></script><script src="/wiki/_next/static/Ie9t5zutrXP6Of75Cb5xF/_buildManifest.js" async=""></script><script src="/wiki/_next/static/Ie9t5zutrXP6Of75Cb5xF/_ssgManifest.js" async=""></script></body></html> |