junegunn / fzf.vim

fzf :heart: vim
MIT License
9.59k stars 581 forks source link

Allow to start :History with the cmdline content #264

Open aymericbeaumet opened 7 years ago

aymericbeaumet commented 7 years ago

My goal is to mimic fzf behaviour when searching the Zsh history with <C-R>, the behaviour I expect in vim is:

I came up with the following solution, but I find it very shaky and I believe there would be a cleaner way to do it. Plus it pollutes the history, doesn't support " in the commands and only works for :.

cnoremap <silent> <C-r> <C-b>call fzf#vim#command_history({ 'options': '--query="<C-e>"' })<CR>

The issue is that in order to execute a function from the command line you have to alter it. So I hardly see how it's doable. Any idea?

junegunn commented 7 years ago

Interesting idea, I'll think about the options when I get some time.

aymericbeaumet commented 7 years ago

I managed to come up with a solution allowing to:

I would be glad to hear your feedback on this:

function! s:FzfCommandHistory()
  let s:INTERRUPT = "\u03\u0c" " <C-c><C-l>
  let s:SUBMIT = "\u0d" " <C-m>
  let s:cmdtype = getcmdtype()
  let s:args = string({
  \   "options": "--query=" . shellescape(getcmdline()),
  \ })
  if s:cmdtype == ':'
    return s:INTERRUPT . ":keepp call fzf#vim#command_history(" .  s:args . ")" . s:SUBMIT
  elseif s:cmdtype == '/'
    return s:INTERRUPT . ":keepp call fzf#vim#search_history(" .  s:args . ")" . s:SUBMIT
  else
    return ''
  endif
endfunction
cnoremap <expr> <C-r> <SID>FzfCommandHistory()

Thanks 👍

junegunn commented 7 years ago

Looks good. I'll see if there's any room for improvement and consider including it in the repo as a <plug> map.