diff --git a/Examples-(vim).md b/Examples-(vim).md index 3376ab6..a7e11e1 100644 --- a/Examples-(vim).md +++ b/Examples-(vim).md @@ -1,6 +1,114 @@ ### `fzf.vim` repository -https://github.com/junegunn/fzf.vim +If you're not familiar with Vimscript or don't have time to write your own +commands, check out [fzf.vim](https://github.com/junegunn/fzf.vim) project +which provides a set of ready-made commands. + +### Basic tutorial + +`fzf#run()` function is the core of Vim integration. It takes a single +dictionary argument. At the very least, specify `sink` option to tell what it +should do with the selected entry. + +```vim +call fzf#run({'sink': 'e'}) +``` + +Without `source`, fzf will use find command (or `$FZF_DEFAULT_COMMAND` if +defined) to list the files under the current directory. When you select one, +it will open it with `:e` command. If you want to open it in a new tab, you +can pass `:tabedit` command instead as the sink. + +```vim +call fzf#run({'sink': 'tabedit'}) +``` + +fzf allows you to select multiple entries with `--multi` (or `-m`) option, and +you can change its bottom-up layout with `--reverse` option. Such options can +be specified as `options`. + +```vim +call fzf#run({'sink': 'tabedit', 'options': '--multi --reverse'}) +``` + +Instead of using the default find command, you can use any shell command as +the source. This will list the files managed by git. + +```vim +call fzf#run({'source': 'git ls-files', 'sink': 'e'}) +``` + +If you use tmux, or use Neovim, you can open fzf in a tmux pane or a split +window so that it doesn't take up the entire screen. + +```vim +" up / down / left / right are allowed +call fzf#run({'source': 'git ls-files', 'sink': 'e', 'right': '40%'}) +``` + +`source` doesn't have to be an external shell command, you can pass a Vim +array as the source. In the following example, we use the names of the open +buffers as the source. + +```vim +call fzf#run({'source': map(range(1, bufnr('$')), 'bufname(v:val)'), + \ 'sink': 'e', 'down': '30%'}) +``` + +Or the names of color schemes. + +```vim +call fzf#run({'source': map(split(globpath(&rtp, 'colors/*.vim')), + \ 'fnamemodify(v:val, ":t:r")'), + \ 'sink': 'colo', 'left': '25%'}) +``` + +The following table shows the available options. + +| Option name | Type | Description | +| -------------------------- | ------------- | ---------------------------------------------------------------- | +| `source` | string | External command to generate input to fzf (e.g. `find .`) | +| `source` | list | Vim list as input to fzf | +| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) | +| `sink` | funcref | Reference to function to process each selected item | +| `sink*` | funcref | Similar to `sink`, but takes the list of output lines at once | +| `options` | string | Options to fzf | +| `dir` | string | Working directory | +| `up`/`down`/`left`/`right` | number/string | Use tmux pane with the given size (e.g. `20`, `50%`) | +| `window` (*Neovim only*) | string | Command to open fzf window (e.g. `vertical aboveleft 30new`) | +| `launcher` | string | External terminal emulator to start fzf with (GVim only) | +| `launcher` | funcref | Function for generating `launcher` string (GVim only) | + + +### Using `fzf#wrap()` function + +`:FZF` command provided by default knows how to handle `CTRL-T`, `CTRL-X`, and +`CTRL-V` and opens the selected files in a new tab, in a horizontal split, or +in a vertical split respectively. And these key bindings can be configured via +`g:plug_action`. This is implemented using `--expect` option of fzf and +the smart sink function. It also understands `g:fzf_layout` and +`g:plug_history_dir`. With `fzf#wrap` function, you can extend the +your command to support the options. + +```vim +" Usage: +" fzf#wrap([name string,] [opts dict,] [fullscreen boolean]) + +" This command now supports CTRL-T, CTRL-V, and CTRL-X key bindings +" and opens fzf according to g:fzf_layout setting. +command! Buffers call fzf#run(fzf#wrap( + \ {'source': map(range(1, bufnr('$')), 'bufname(v:val)')})) + +" This extends the above example to open fzf in fullscreen +" when the command is run with ! suffix (Buffers!) +command! -bang Buffers call fzf#run(fzf#wrap( + \ {'source': map(range(1, bufnr('$')), 'bufname(v:val)')}, 0)) + +" You can optionally pass the name of the command as the first argument to +" fzf#wrap to make it work with g:plug_history_dir +command! -bang Buffers call fzf#run(fzf#wrap('buffers', + \ {'source': map(range(1, bufnr('$')), 'bufname(v:val)')}, 0)) +``` ### `locate` command integration