How to create Ctrl+f and ctrl+shift+f in vim?

17k views Asked by At

I am learning vim recently as i have to use vim in some restricted machines. As i am a vscode user, I would like to find ctrl+f and ctrl+shift+f equivalent to do string search

Ctrl+f will search within the file

ctrl+shift+f  will search in all the files in the folder tree.

I added ripgrep , It search :Rg! ,it always search in all files like Ctrl+shift+f Whether Rg can be used to search with the files? (like ctrl+f).

Also how to add ctrl+f and ctrl+shift+f mapping in vim

2

There are 2 answers

0
Dmitry On

I agree with romainl, vim already has these function without any plugins. Just want to add some details.

Search string in current file is done by / in normal mode, see :h /, :h pattern.txt.

Search string in many files may be done by vimgrep command, see :h :vimgrep.

Also you may find useful vim fzf plugin, it uses ripgrep you already installed and does much more.

Of course you can do mappings on Ctrl-F and Ctrl-Shift-F, but it will override builtin vim command, so I don't recommend to do it.

map <C-F> /
map <CS-F> :vimgrep /

See :h map.txt

1
John Montgomery On

To add on what @Dmitry stated in their answer (regardless of whether or not one should), while you can map Ctrl-F to search, vim doesn't support detection of Ctrl vs Ctrl-Shift keybinds so an alternative would have to be used. One such alternative would be using Ctrl-Alt-F instead:

map <C-f> /
map <C-M-f> :vimgrep /

Or (for compatibility depending on how your terminal treats the Alt key):

map <C-f> /
map <Esc><C-f> :vimgrep /

Edit: Thanks to @Baczek for mentioning in a comment, there's an answer here that shares how to achieve such 'impossible' mappings in some contexts (I haven't tested its solutions myself).