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

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

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

Related

How do I search in all files of my project using VIM?

There are a couple of things I do not yet understand the VIM way.
One of these is searching in a project like so (using VIM in Atom):
I use CtrlP currently for file names, but what about the contents?
How can I search with a string, and then look through a list of all occurrences using VIM and/or VIM plugins?
I've found an even better solution for this: FZF
It simply searches through everything in your project asynchronously using the :Ag command.
Use :grep or :vimgrep to search file contents. The results are put onto the "location list" which you can open by typing :cw Enter.
Syntax for :grep is, by default, the same as the grep(1) command:
:grep 'my pattern.*' /path/to/dir
By default it will search the current directory (:pwd). I added set autochdir to my .vimrc so my PWD always follows the file I'm editing.
The major difference between :grep and :vimgrep is that :vimgrep (:vim for short) uses Vim-compatible regular expressions, whereas :grep uses whatever regular expressions your &grepprg uses.
You can use a custom program by setting &grepprg to something different. I personally like ack which uses Perl-compatible regex.
Apart from fzf, there are also other excellent plugins for fuzzy finding.
telescope.nvim (neovim only): after install, just use Telescope live_grep to search through your project.
Leaderf: another fuzzy finder with good performance. After install, use Leaderf rg to search through your project.
To open a file, I highlight the row (Shift-v) in the location list and hit Enter.

In Vim, how to choose the nth suggested file while doing :edit?

This is a total newbie vim question. Apologies for the basic-ness involved.
I need to open a lot of files. The :edit <file-name> command seems to open a file and I also see a filename auto-complete feature, which searches for all similar file names in the path. But I don't know how to choose one of those suggestions quickly without writing the whole file name.
set wildmenu
enables the "wildmenu" where you navigate with <Tab> and <S-Tab>, enter a subdirectory with <Down> and select a file with <CR>.
See :help 'wildmenu' and :help 'wildmode'.
Note that :edit can only open one file. If you want to open multiple files at once, use :args models/*.php.
Another, more familiar, way to open files is to use the built-in netrw:
:Explore
See :help netrw for more info.
Sounds like you might enjoy the conveniences of the rather smashing CTRL-P plugin. https://github.com/kien/ctrlp.vim
It will allow you to type any substring of the filename to narrow the available files down. You can mark multiple files for opening.

Vim :Ag search and replace, globally and within a directory

I am using :Ag in vim to search for patterns, but I can't figure out how to search and replace through ALL files in my project, or within a directly that I specify. How is this done?
In Vim, project-wide search and replace is at best a two-step process unless you install a plugin that abstracts those two steps for you like EasyGrep.
In its most basic form, project-wide search/replace looks like this:
:args `grep -nl foo *.js`
:argdo %s/foo/bar/c
It won't help you much with :Ag, though, because Vim doesn't have a built-in command similar to the :*do family that works on the quickfix list.
Drew Neil has a couple of screencasts, here and there, that deal with project-wide search/replace. The :Qfdo command mentionned at the bottom of the second post is specifically geared toward :vimgrep/:Ack/:Ag users. With that command and :Ag, the two-step process becomes:
:Ag foo
:Qfdo s/foo/bar/c
-- EDIT --
Vim now has :cdo, :cfdo, :ldo, :lfdo.
In vim, there are multiple solutions, but you have to open all the files in order to be able to do this. You can open the files in different windows, buffers or tabs, and for all the options, you have a method to operate on all files:
:windo :Ag
or
:bufdo :Ag
or
:tabdo :Ag
See :help windows, :help buffers or :help tab-page-commands for more info.

MacVim-Like Find and Replace for gVim

I know about the Vim find and Replace command, but I find MacVim's Find and Replace ( ⌘+F ) better compared to Vim's Native way of Finding Words. Is there a plugin for (g)Vim to assist ?
I've never used MacVim.
In gvim you can bring up the "Find and Replace" dialog like this:
:promptrepl
See if this mapping works for you:
:map <D-f> :promptrepl<cr>
(According to :h meta, <D- is the command key on Mac but I'm not sure if that would work as I can't test it)

Opening files in Vim using Fuzzy Search

I'm looking for a way to make Vim have the ability to open a file by fuzzy-searching its name.
Basically, I want to be able to define a project once, and then have a shortcut which will give me a place to type a file name, and will match if any letters match up.
This kind of functionality exists in most editors I've seen, but for the life of me I can't understand how to get Vim to do this.
Note that I'm looking for something that won't require me to have any idea where in my directory tree a file is. I just want to be able to open it by the filename, regardless of what directory it's in.
Thanks
There are two great vim plugins for this.
ctrlp:
Written in pure VimL
Works pretty much everywhere
Supports custom finders for improved performance
Most popular fuzzy search plugin for Vim
Command-T:
Written in C, VimL and Ruby
Fast out of the box
Requires +ruby support in Vim
Recommends Vim version >= 7.3
EDIT:
I use CtrlP with ag as my custom finder and it's incredibly quick (even on massive projects) and very portable.
An example of using ag with CtrlP:
if executable('ag')
" Use Ag over Grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif
CommandT for Vim is very much the comparable feature as in TextMate. My work flow is now
1) open up MacVim
2) :cd ~/my_project
3) (I have this mapped as described in the installation help)
4) C-v the file to open the file in a vertical split, or CR to open a new horizontal split.
5) to close the split, use :bd (buffer delete)
6) to switch to another buffer, I have BufferExplorer installed, so just \be and select
This workflow is comparable to TextMate, it takes a while to get used to, and I'm still learning.
Basic solution
Simply add this to your .vimrc
nnoremap <C-p> :find ./**/*
Pressing Ctrl+p will now allow you to fuzzyfind files in your current working directory and sub-directories thereof. Use the tab key to cycle through options.
Related solution
For those who want to keep it basic i.e. no plugins, this entertaining video shows another way to achieve fuzzy file find in vim.
They actually use
set path+=**
set wildmenu
in their .vimrc to find files in current sub-directories.
For example, with :find *Murph followd by tab, I would find the files KilianMurphy2012Why.R and KilianMurphy2014ROLE.R in subdir code which I can cycle through with the tab key. The first solution above has the advantage that the relative path is also shown.
Note that your current working directory will matter and that other files on your path (:set path?) will also be found with the this type of solution. The wildmenu option adds visual information and is not essential.
For a keyboard shortcut, add
nnoremap <C-p> :find *
to your .vimrc. Now you will be able to quickly search for files inside your project/current dir with Ctrl+p in normal mode.
What about http://www.vim.org/scripts/script.php?script_id=1984 Then there is http://github.com/jamis/fuzzy_file_finder .
Also see these blog posts: http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim and http://weblog.jamisbuck.org/2009/1/28/the-future-of-fuzzyfinder-textmate
HTH

Resources