WebsiteTemplate/cheatsheets/fzf.html
2026-01-25 11:33:37 -04:00

312 lines
9.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<link rel="stylesheet" href="../assets/css/style.css">
<link rel="icon" type="image/x-icon" href="../favicon.ico">
<script>
// Apply theme immediately to prevent flash
(function() {
const theme = localStorage.getItem('theme') ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
})();
</script>
<title>fzf Cheatsheet - Cheatsheets - Launch Pad</title>
</head>
<body>
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
</button>
<br/><br/>
<div class="name">
__ _______________________ _________._________________________
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
\/ \/ \/ \/ \/ \/ \/
</div>
<div class="blog-page-header">
<div class="blog-header-content">
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
</a>
<h1 class="blog-page-title">fzf Cheatsheet</h1>
</div>
</div>
<div class="blog-post-container">
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
<div class="blog-post">
<div class="blog-post-content">
<p><a href="index.html">← Back to cheatsheets</a></p>
<p><a href="../index.html">← Home</a></p>
<hr>
<p>fzf (fuzzy finder) is a command-line fuzzy finder for filtering and selecting from lists. Extremely fast and versatile for navigating files, command history, and more.</p>
<hr>
<h2>Basic Usage</h2>
<ul>
<li>fzf - Fuzzy find from stdin</li>
</ul>
<ul>
<li>ls | fzf - Fuzzy find from file list</li>
</ul>
<ul>
<li>find . -type f | fzf - Fuzzy find from find results</li>
</ul>
<ul>
<li>fzf &lt;dir&gt; - Fuzzy find files in directory</li>
</ul>
<hr>
<h2>Interactive Commands</h2>
<ul>
<li>Ctrl+C, Esc - Exit</li>
</ul>
<ul>
<li>Enter - Select and exit</li>
</ul>
<ul>
<li>Tab - Select multiple items (multi-select)</li>
</ul>
<ul>
<li>Shift+Tab - Unselect</li>
</ul>
<ul>
<li>Up/Down Arrow or Ctrl+P/N - Navigate up/down</li>
</ul>
<ul>
<li>Page Up/Down or Ctrl+V/B - Navigate page up/down</li>
</ul>
<ul>
<li>Alt+Up/Down - Navigate half page up/down</li>
</ul>
<ul>
<li>Ctrl+A - Select all</li>
</ul>
<ul>
<li>Ctrl+D - Deselect all</li>
</ul>
<ul>
<li>Ctrl+T - Toggle all</li>
</ul>
<ul>
<li>Ctrl+L - Clear input</li>
</ul>
<ul>
<li>? - Toggle preview window</li>
</ul>
<ul>
<li>Alt+W - Toggle preview wrap</li>
</ul>
<ul>
<li>Alt+? - Toggle help</li>
</ul>
<hr>
<h2>Search Patterns</h2>
<ul>
<li>`word` - Match lines containing word</li>
</ul>
<ul>
<li>`&#39;word` - Exact match (disables fuzzy matching)</li>
</ul>
<ul>
<li>`^word` - Prefix match (starts with word)</li>
</ul>
<ul>
<li>`word$` - Suffix match (ends with word)</li>
</ul>
<ul>
<li>`!word` - Negation (exclude lines with word)</li>
</ul>
<ul>
<li>`&#39;word1 | &#39;word2` - OR operator</li>
</ul>
<ul>
<li>`word1 word2` - AND operator (both must match)</li>
</ul>
<hr>
<h2>Preview Window</h2>
<ul>
<li>fzf --preview=&#39;cat {}&#39; - Preview file contents</li>
</ul>
<ul>
<li>fzf --preview=&#39;head -100 {}&#39; - Preview first 100 lines</li>
</ul>
<ul>
<li>fzf --preview=&#39;ls -lah {}&#39; - Preview file details</li>
</ul>
<ul>
<li>fzf --preview-window=right:40% - Custom preview window size</li>
</ul>
<ul>
<li>fzf --preview-window=hidden - Hide preview initially</li>
</ul>
<ul>
<li>? - Toggle preview (in fzf)</li>
</ul>
<hr>
<h2>Common Use Cases</h2>
<h3>File Navigation</h3>
<ul>
<li>fzf - Find files in current directory</li>
</ul>
<ul>
<li>find . -type f | fzf - Find all files recursively</li>
</ul>
<ul>
<li>fzf --type f - Files only</li>
</ul>
<ul>
<li>fzf --type d - Directories only</li>
</ul>
<h3>Command History</h3>
<ul>
<li>history | fzf - Search command history</li>
</ul>
<ul>
<li>Ctrl+R - fzf search through history (if configured)</li>
</ul>
<h3>Process Selection</h3>
<ul>
<li>ps aux | fzf - Search and select process</li>
</ul>
<ul>
<li>ps aux | fzf | awk &#39;{print $2}&#39; - Get PID from selection</li>
</ul>
<h3>Git Operations</h3>
<ul>
<li>git log --oneline | fzf - Browse git commits</li>
</ul>
<ul>
<li>git branch | fzf - Select branch</li>
</ul>
<ul>
<li>git stash list | fzf - Select stash</li>
</ul>
<hr>
<h2>Shell Integration</h2>
<h3>Bash/Zsh Key Bindings</h3>
<p>Add to `~/.bashrc` or `~/.zshrc`:</p>
<pre><code># Use fzf for Ctrl+R history search
if command -v fzf &gt;/dev/null 2&gt;&amp;1; then
source /usr/share/fzf/shell/key-bindings.bash # or .zsh
fi</code></pre>
<p>After setup:</p>
<ul>
<li>Ctrl+R - Search command history</li>
</ul>
<ul>
<li>Ctrl+T - Find and insert file path</li>
</ul>
<ul>
<li>Alt+C - Change directory (cd)</li>
</ul>
<h3>Fish Shell</h3>
<pre><code>fzf_key_bindings</code></pre>
<hr>
<h2>Useful Aliases</h2>
<p>Add to your shell config:</p>
<pre><code># File operations
alias fcd=&#x27;cd $(find . -type d | fzf)&#x27;
alias fvim=&#x27;vim $(fzf)&#x27;
alias fcat=&#x27;cat $(fzf)&#x27;
# Git with fzf
alias fco=&#x27;git checkout $(git branch | fzf | sed &quot;s/^..//&quot;)&#x27;
# Process kill
alias fkill=&#x27;kill -9 $(ps aux | fzf | awk &quot;{print \$2}&quot;)&#x27;</code></pre>
<hr>
<h2>Advanced Options</h2>
<ul>
<li>fzf --height 40% - Set height</li>
</ul>
<ul>
<li>fzf --reverse - Reverse layout (top to bottom)</li>
</ul>
<ul>
<li>fzf --border - Show border</li>
</ul>
<ul>
<li>fzf --header=&#39;Select file:&#39; - Custom header</li>
</ul>
<ul>
<li>fzf --multi - Enable multi-select</li>
</ul>
<ul>
<li>fzf --bind=&#39;ctrl-a:select-all&#39; - Custom key bindings</li>
</ul>
<ul>
<li>fzf --exact - Exact match mode</li>
</ul>
<ul>
<li>fzf --extended - Extended search mode (default)</li>
</ul>
<hr>
<h2>Configuration</h2>
<h3>Config File Location</h3>
<ul>
<li>`~/.fzfrc` - fzf configuration</li>
</ul>
<ul>
<li>`~/.fzf.bash` - Bash integration (if installed)</li>
</ul>
<ul>
<li>`~/.fzf.zsh` - Zsh integration (if installed)</li>
</ul>
<ul>
<li>`/usr/share/fzf/` - Default installation directory</li>
</ul>
<h3>Example Configuration</h3>
<pre><code># fzf options
export FZF_DEFAULT_OPTS=&#x27;--height 40% --reverse --border&#x27;
export FZF_DEFAULT_COMMAND=&#x27;fd --type f&#x27; # Use fd instead of find
export FZF_CTRL_T_COMMAND=&#x27;fd --type f&#x27;
export FZF_CTRL_T_OPTS=&#x27;--preview &quot;bat --color=always --style=numbers {}&quot;&#x27;</code></pre>
<hr>
<h2>Tips</h2>
<ul>
<li>Use single quotes `&#39;word&#39;` for exact matches when fuzzy matching fails</li>
</ul>
<ul>
<li>Combine with other tools using pipes: `find . | fzf | xargs vim`</li>
</ul>
<ul>
<li>Use preview window to see file contents before selecting</li>
</ul>
<ul>
<li>Configure shell bindings for Ctrl+R, Ctrl+T, Alt+C</li>
</ul>
<ul>
<li>Use multi-select (Tab) to select multiple files</li>
</ul>
<ul>
<li>Install ripgrep or fd for faster file finding with fzf</li>
</ul>
<ul>
<li>Customize colors and layout with FZF_DEFAULT_OPTS</li>
</ul>
<ul>
<li>Use fzf in scripts for interactive selection</li>
</ul>
<ul>
<li>Combine with vim (fzf.vim plugin) for file navigation</li>
</ul>
<ul>
<li>Great for browsing logs: `tail -f /var/log/syslog | fzf`</li>
</ul>
<hr>
<p><a href="index.html">← Back to cheatsheets</a></p>
<p><a href="../index.html">← Home</a></p>
</div>
</div>
</div>
</div>
<script async type="text/javascript" src="../blog/analytics.js"></script>
<script src="../theme.js"></script>
</body>
</html>