-
Notifications
You must be signed in to change notification settings - Fork 2
add post about fzf #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seemethere
wants to merge
1
commit into
master
Choose a base branch
from
fzf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
--- | ||
title: "fzf and vim, a match made in heaven" | ||
date: 2020-01-24T08:38:36-08:00 | ||
tags: ["tools", "vim"] | ||
draft: true | ||
--- | ||
|
||
Tools are just one of those things that every now and then we need an update. | ||
Just like a carpenter will one day need to update their hammers and saws, we as | ||
software engineers should learn to adapt to the newest tools out there. Here | ||
are some of the tooling updates I've recently introduced into my workflow that | ||
I think would be useful to other developers. | ||
|
||
# fzf(.vim) | ||
|
||
## Some context | ||
|
||
I've been using [`fzf`](https://github.com/junegunn/fzf) for the better part of | ||
3 years and have loved it as a tool for completion on the command line. When | ||
initially trying out the tool I was amazed at how well it worked with my shell | ||
and loved the integrations with common developer workflow like `CTRL-r` and even | ||
autocompletion with tools like `kill`. | ||
|
||
From the point that I started using `fzf` I had been using | ||
[`ctrlp`](https://github.com/kien/ctrlp.vim/) as my main way to do fuzzy file | ||
finding in vim, but I had found it suffered when presented with large | ||
directories. Luckily the creator of `fzf` had also created a vim plugin for | ||
`fzf` entitled appropriately as | ||
[`fzf.vim`](https://github.com/junegunn/fzf.vim). | ||
|
||
Admittedly, 3 years ago the `fzf.vim` plugin was not ready, in my opinion. | ||
Integration was weird in that it needed a separate terminal window in order | ||
to actually do your fuzzy file finding. This led to new terminal applications | ||
opening up in macOS and cluttering your screen. So at the time I kept with | ||
`ctrlp`. | ||
|
||
> Enter 2020 | ||
|
||
At this point I've started a new job and was in the process of setting up | ||
my new developer environment. With setting up a new environment at a new | ||
company comes some opportunity so I took the chance to try out new things and | ||
decided to dive into `fzf.vim` again. | ||
|
||
## Setting it up | ||
|
||
### Requirements | ||
|
||
In order to use `fzf.vim` as I am using it you need to use | ||
|
||
* [`neovim`](https://github.com/neovim/neovim) > `0.4` | ||
* [`ripgrep`](https://github.com/BurntSushi/ripgrep) | ||
* [`findutils`](https://www.gnu.org/software/findutils/) if on `macOS` | ||
|
||
### Configuration | ||
|
||
Most of the configuration I use is pulled directly from the GitHub repositories | ||
[`README.md`](https://github.com/junegunn/fzf.vim). | ||
|
||
~/.config/nvim/init.vim | ||
```vimscript | ||
" fzf { | ||
" Using floating windows of Neovim to start fzf | ||
if has('nvim') | ||
function! FloatingFZF(width, height, border_highlight) | ||
function! s:create_float(hl, opts) | ||
let buf = nvim_create_buf(v:false, v:true) | ||
let opts = extend({'relative': 'editor', 'style': 'minimal'}, a:opts) | ||
let win = nvim_open_win(buf, v:true, opts) | ||
call setwinvar(win, '&winhighlight', 'NormalFloat:'.a:hl) | ||
call setwinvar(win, '&colorcolumn', '') | ||
return buf | ||
endfunction | ||
|
||
" Size and position | ||
let width = float2nr(&columns * a:width) | ||
let height = float2nr(&lines * a:height) | ||
let row = float2nr((&lines - height) / 2) | ||
let col = float2nr((&columns - width) / 2) | ||
|
||
" Border | ||
let top = '╭' . repeat('─', width - 2) . '╮' | ||
let mid = '│' . repeat(' ', width - 2) . '│' | ||
let bot = '╰' . repeat('─', width - 2) . '╯' | ||
let border = [top] + repeat([mid], height - 2) + [bot] | ||
|
||
" Draw frame | ||
let s:frame = s:create_float(a:border_highlight, {'row': row, 'col': col, 'width': width, 'height': height}) | ||
call nvim_buf_set_lines(s:frame, 0, -1, v:true, border) | ||
|
||
" Draw viewport | ||
call s:create_float('Normal', {'row': row + 1, 'col': col + 2, 'width': width - 4, 'height': height - 2}) | ||
autocmd BufWipeout <buffer> execute 'bwipeout' s:frame | ||
endfunction | ||
|
||
let g:fzf_layout = { 'window': 'call FloatingFZF(0.9, 0.6, "Comment")' } | ||
endif | ||
|
||
" Hide statusline | ||
if has('nvim') && !exists('g:fzf_layout') | ||
autocmd! FileType fzf | ||
autocmd FileType fzf set laststatus=0 noshowmode noruler | ||
\| autocmd BufLeave <buffer> set laststatus=2 showmode ruler | ||
endif | ||
|
||
map <C-p> :Files<CR> | ||
let find_command = "find" | ||
" For some ungodly reason mac's version of find does not include | ||
" -printf, so we need to switch to gfind here | ||
if has('macunix') | ||
let find_command = "gfind" | ||
endif | ||
let $FZF_DEFAULT_COMMAND = find_command . " . -type f -not -path '*/\.git/*' -printf '%P\\n'" | ||
command! -bang -nargs=* Rg | ||
\ call fzf#vim#grep( | ||
\ 'rg --hidden -g "!.git" --column --line-number --no-heading --color=always --smart-case '.shellescape(<q-args>), 1, | ||
\ fzf#vim#with_preview(), <bang>0) | ||
" } | ||
``` | ||
|
||
Some key differences: | ||
|
||
* I use a custom `FZF_DEFAULT_COMMAND` in order to auto-include hidden files, | ||
excluding those in `.git` | ||
* I use a custom `rg` command that does similar things to my | ||
`FZF_DEFAULT_COMMAND` | ||
* I add the `CTRL-p` default keybind to bring up the fuzzy file finder | ||
|
||
## Some demos | ||
|
||
### fzf.vim searching through files | ||
|
||
With my config searching through files in your git repo is as easy as | ||
just pressing `CTRL-p`. | ||
|
||
With neovim this key bind will bring up a floating window that allows | ||
you to enter the name of the file that your are searching for. | ||
|
||
In this example we'll be searching for a python script that we think is | ||
named `should_run.py` | ||
|
||
 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would word this as "every now and then we need to update"