Skip to content

Instantly share code, notes, and snippets.

@nuffin
Created October 25, 2024 14:58
Show Gist options
  • Save nuffin/e87702e7741fb7b1f900516acbaddedf to your computer and use it in GitHub Desktop.
Save nuffin/e87702e7741fb7b1f900516acbaddedf to your computer and use it in GitHub Desktop.
VIM: grep in a popup and select for open, with fzf plugin
" the key bindings are just as examples, for personal prefer
let fzf_installed = 0
call plug#begin('~/.vim/plugged')
" NOTICE: you should add this part into your plugin#begin() .. plug#end() block
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
nnoremap <C-T>z :FZF<CR>
let g:fzf_action = { 'enter': 'split', }
let fzf_installed = 1
call plug#end()
if fzf_installed
" See `--tmux` option in `man fzf` for available options
" [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]
if exists('$TMUX')
let g:fzf_layout = { 'tmux': '90%,80%' }
else
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.8 } }
endif
function! FzfGrepWithPrompt(with_more_argument=0, whole_word=0, list_files=0, ignore_case=0)
let word = expand('<cword>')
let grep_command = 'grep -r'
if a:ignore_case
let grep_command .= 'i'
endif
if a:whole_word
let grep_command .= 'w'
endif
if a:list_files
let grep_command .= 'l'
else
let grep_command .= 'n'
endif
if a:with_more_argument
let extra_args = input(grep_command . ' ' . word . ' ', '', 'file')
else
let extra_args = ''
endif
" Combine the command and send it to fzf
let command = grep_command . ' ' . word . ' ' . extra_args
" echo command
" echom command
call fzf#vim#grep(command, fzf#vim#with_preview(), 0)
endfunction
nnoremap <silent> <C-K> :call FzfGrepWithPrompt()<CR>
nnoremap <silent> <C-K>? :call FzfGrepWithPrompt(1)<CR>
nnoremap <silent> <C-K><C-K> :call FzfGrepWithPrompt(0)<CR>
nnoremap <silent> <C-K><C-K>? :call FzfGrepWithPrompt(1)<CR>
nnoremap <silent> <silent> <C-K>l :call FzfGrepWithPrompt(0, 0, 1)<CR>
nnoremap <silent> <C-K>l? :call FzfGrepWithPrompt(1, 0, 1)<CR>
nnoremap <silent> <silent> <C-K><C-L> :call FzfGrepWithPrompt(0, 0, 1)<CR>
nnoremap <silent> <C-K><C-L>? :call FzfGrepWithPrompt(1, 0, 1)<CR>
nnoremap <silent> <C-K>w :call FzfGrepWithPrompt(0, 1)<CR>
nnoremap <silent> <C-K>w? :call FzfGrepWithPrompt(1, 1)<CR>
nnoremap <silent> <C-K><C-W> :call FzfGrepWithPrompt(0, 1)<CR>
nnoremap <silent> <C-K><C-W>? :call FzfGrepWithPrompt(1, 1)<CR>
else
nnoremap <silent> <C-K> :!grep -r <C-R><C-W><CR>
nnoremap <C-K>? :!grep -r <C-R><C-W><Space>
nnoremap <silent> <C-K><C-K> :!grep -r <C-R><C-W><CR>
nnoremap <C-K><C-K>? :!grep -r <C-R><C-W><Space>
nnoremap <silent> <C-K>l :!grep -rl <C-R><C-W><CR>
nnoremap <C-K>l? :!grep -rl <C-R><C-W><Space>
nnoremap <silent> <C-K><C-L> :!grep -rl <C-R><C-W><CR>
nnoremap <C-K><C-L>? :!grep -rl <C-R><C-W><Space>
nnoremap <silent> <C-K>w :!grep -rw <C-R><C-W><CR>
nnoremap <C-K>w? :!grep -rw <C-R><C-W><Space>
nnoremap <silent> <C-K><C-W> :!grep -rw <C-R><C-W><CR>
nnoremap <C-K><C-W>? :!grep -rw <C-R><C-W><Space>
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment