Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active January 17, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/095fbd52f488ca295ae9fd408973587a to your computer and use it in GitHub Desktop.
Save Integralist/095fbd52f488ca295ae9fd408973587a to your computer and use it in GitHub Desktop.
[Vim Arg List and Searching] #vim #vimgrep #arglist #search #regex

Reference: https://vim.fandom.com/wiki/Find_in_files_within_Vim

Start vim:

vim

Check arguments list is empty:

:args

Add files from current directory:

:argadd *

Clear the arguments list:

:argdelete *

Add different glob of files:

:argadd .aws/**

Search for pattern in files from the arguments list:

:vimgrep /^aws/j ##

Note: ## represents the arg list + the j option isn't documented but apparently means "don't jump to first match".

Improve performance of vimgrep using :noautocmd:

:noautocmd vimgrep /{pattern}/[flags] {file(s)}

vimgrep uses Vim's procedures to read files, which can involve execution of several autocommands. So disable autocommands.

Search for pattern in files from result of a backtick expression:

:vimgrep /ssh/j `find . -type f -name 'tmux*'`

Note: j tells Vim not to automatically jump to the first match

Search based on a previous search pattern:

:vimgrep /<C-r>// *

To clarify the above command, imagine you have a complex pattern you want to play around with and test with a single file so you use / to get vim to jump into search mode for the current buffer content and then type in your complex pattern.

Once happy with your pattern, you now want to use it again for multiple files but you don't want to have to type the pattern out again (especially in case it's complex enough to easily include an unexpected typo).

So you type :vimgrep / and after that is where you would typically start typing your search pattern, at this point press <Ctrl-r> followed by / and Vim will automatically insert the last search pattern for you.

Imagine ... was the last search pattern, this would mean the Ex mode command would currently look like :vimgrep /... so you would need to finish the command / * (so it's almost like you wrote the command in its entirety).

Note: if you use another plugin like :Ack! then <C-r>/ works to insert the last search pattern still (e.g. :Ack! '<C-r>/')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment