2

In bash you can easily search the current history entries by hitting <C-r> or <C-s>. This search feature is incremental, and once you hit return, you get a bash prompt auto-filled with the closest match in your history. I know if I want to search for a vim command I can hit q: and then search with /. However, I find this slow. Tab completion somewhat alleviates the tedium of searching for command, but is not quite satisfactory. Does vim have a similar feature?

1
  • Like you stated, you could open up the search command window using q: and then search that window with /.
    – TheChetan
    Sep 29, 2019 at 2:44

2 Answers 2

2

I'm not aware that this is possible. The only thing I know is, that Vim uses the already typed characters to filter the history.

If you previously executed:

:%s/This/That/g

and you want to get this command again, you do

:%s/T<up>

This will get only commands that start with %s/T from the history.

See :help c_Up for more details.

2
  • Yes, up arrow is the closest to what you get with Ctrl-R in bash. It's not exactly the same, but I'd say it's close enough. Full search on q: is the next step after that.
    – filbranden
    Sep 28, 2019 at 14:38
  • Everything is possible with enough plugins.
    – eyal karni
    Oct 2, 2019 at 19:20
1

You can use fuzzy searching using FZF(as I do). Check https://github.com/junegunn/fzf.vim .

function! GetCommands()
let lines=[]
let nu=histnr("cmd")
for i in range(1,nu)
    let lines+=[histget("cmd",i)]
endfor
return lines
endfunction

function! HandleCommand(item)
    call feedkeys("q:")
    call feedkeys("G?\\V".escape(a:item,'\/?')."\<CR>")
endfunction

nnoremap <silent> <C-a>c :call fzf#run({'source': GetCommands(),'sink': function('HandleCommand'),'options': '-m'} )<CR>

When I press <c-a>c , it displays me a fuzzy search for command. Then, when I press enter, it goes to the command in the command window. If I press another enter it executes. I can also edit the command.

You may also want to do

set history=10000
nnoremap <silent> <C-a><C-a> <C-a>

to save longer command history, and the functionality of <c-a>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.