{"slug": "vimgrep-cheatsheet", "title": "vimgrep cheatsheet", "summary": "The article explains that `vimgrep` is a Vim command that searches across multiple files using native Vim regex patterns and populates the quickfix list with results. It supports features like searching all matches with `/g`, using wildcards and backtick expansion for file lists, and navigating results via quickfix commands such as `:cnext` and `:copen`. The cheatsheet also notes that `lvimgrep` works similarly but uses the location list instead of the quickfix list.", "body_md": "-\nUses native vim regexes (which are slightly different from the regexes used by grep, ack, ag, etc) so the patterns are the same as with vim's within-file search patterns.\nYou can do a normal within-file search first, then re-use the same pattern to search across files with // (see examples below).\n-\nUnlike normal within-file search, vimgrep (or rather, the quickfix list) tells you which match you're on (e.g.\n2 of 5\n).If you want to search within the current file but have this feature, tell vimgrep to search the current file with\n%\n::vimgrep /foo/g %\n(vimgrep doesn't seem to highlight all the matches like normal / search does, but you can get this by doing the search with / first to make the highlights, then doing :vimgrep //g % and using the quickfix list to navigate the results.)\n-\nLike many vim commands it populates the quickfix list with the search results.\n-\nBy default it only finds the first match in every file. Add\n/g\n(see examples below) to find all matches. -\nCan be shortened to just :vim.\n-\nCan be used in conjunction with vim's arg list (see examples below).\n-\nIgnores files that match vim's\nwildignore\nsetting, and searches files insuffixes\nlast. -\nUses vim's\nignorecase\nsetting (but notsmartcase\n).\nSimple example - to search three specific files for foo\n:\n:vimgrep /foo/g foo.txt bar.txt gar.txt\nYou can pass a list of specific files like foo.txt bar.txt gar.txt\nto\nvimgrep but you can also use wildcards and backtick expansion.\nBoth * and ** work:\n:vimgrep /foo/g *.txt\n:vimgrep /foo/g **/*.txt\nFor example list the files that you want to search in a file called\n/tmp/files\nthen do:\n:vimgrep `cat /tmp/files`\nAnother use for this is to search all tracked files in a git project using\ngit ls-files\n:\n:vimgrep /foo/g `git ls-files`\ngit ls-files\nomits git ignored and untracked files. You could just use\nfugitive.vim's :Ggrep command (calls git grep)\nthough.\nYou can populate vim's argument list first with :args {FILES},\nperhaps validate the contents of the argument list with :args,\nand then tell vimgrep to search all files in the argument list with ##\n:\n:vimgrep /foo/g ##\nThis also means that you can reuse the same argument list for multiple searches.\nYou can tell vim to search using the current within-file search pattern using //\n.\nTest a search pattern on the current file and then use it across multiple files\nwith vimgrep:\n:vimgrep //g\nAlternatively you can paste the current search pattern into the command line\n(after :vimgrep /\n) with\nCtrl+r/.\nAfter running vimgrep you navigate the search results by navigating vim's quickfix list.\nAll the commands for navigating the quickfix list begin with :c\n:\n-\n:copen and :cclose: open and close the quickfix window.\nIn the quickfix window Enter jumps to the item under the cursor in the window above the quickfix window (if the file is already open in another window it'll go to that window instead).\nYou can also just close the quickfix window with :q while it's the active window, like any other window.\nIf you keep the quickfix window open (whether the active window or not) and use commands like :cnext etc below to move between matches, then you can see the match in the file and the position of the match in the list of matches side-by-side.\nTo open the quickfix window in a horizontal rather than vertical split do :vert copen.\n-\n:cnext and :cprev: go to the next and previous match. You can precede this with a count, e.g. :5cnext.\nvim-unimpaired adds ]q and [q shortcuts for these.\n-\n:cfirst and:clast go to the first and last match.\nUnimpaired adds ]Q and [Q shortcuts for these.\n-\n:cnfile and :cpfile go to the first or last item in the next or previous match-containing file.\n-\n:cc n goes to the nth match.\nVim remembers the last 10 quickfix lists and you can move between them with :colder and :cnewer (and you can precede these with a count, e.g. :5colder). (Also see :vimgrepa below.)\nlvimgrep is the same as vimgrep but populates the current window's location list with the search results instead of the quickfix list. So you have to use location list commands to navigate through results (e.g. :lnext and :lprev instead of :cnext and :cprev). This means you can have multiple across-files searches in play at once, in different windows.\n:vimgrepa works the same as vimgrep but appends its matches to the end of the existing quickfix list, instead of pushing a new quickfix list. :lvimgrepa does the same but for the location list.\n- Grepper\n- :grep\n- Making :grep call Ack, or any other program with the\ngrepprg\nandgrepformat\nsettings - The ack.vim plugin.\nThis plugin can be configured to use\nag\n(silversearcher) too. - fugitive.vim's :Ggrep command (calls git grep)", "url": "https://wpnews.pro/news/vimgrep-cheatsheet", "canonical_source": "https://gist.github.com/seanh/a866462a27cb3ad7b084c8e6000a06b9", "published_at": "2019-06-12 23:00:01+00:00", "updated_at": "2026-05-22 17:11:50.583529+00:00", "lang": "en", "topics": ["developer-tools"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/vimgrep-cheatsheet", "markdown": "https://wpnews.pro/news/vimgrep-cheatsheet.md", "text": "https://wpnews.pro/news/vimgrep-cheatsheet.txt", "jsonld": "https://wpnews.pro/news/vimgrep-cheatsheet.jsonld"}}