Hot answers tagged

15

I'm not sure this is what you're looking for, but maybe you could try fzf and fzf.vim. If you use vim-plug as a plugin manager, all you should have to do to install them, is to add these lines in your vimrc: Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' } Plug 'junegunn/fzf.vim' ... somewhere between the lines: call plug#begin('~/.vim/...


8

Vim has a startup option -q to read a quickfix file. So we have options (depending on your shell): cmd > results vim -q results Or my favorite vim -q <(cmd)


5

Why It's Broken The problem is that you've used -bar in your command definition. This attribute is used to specify that you can use a | after an invocation of your new command e.g.: :Ag search_term | copen However, the -bar flag also checks for " characters to start a comment (See :help :command-bar.) This means that when you issue the command: :Ag &...


4

You can sort the resulting quickfix list to give priority to current buffer, then run uniq() on it. Sadly you need to handle sorting quickfix items almost completely. For example the following will sort the results by buffer, then by line, then column, then text. If you need more precise sorting you can add more fields (see :h getqflist() to see what's ...


3

You can use the pattern \%^ to match the start of of the file. Using this, you could populate the quickfix list with the first line of all your markdown files, even empty ones. :vimgrep "\%^" **/*.md


3

An alternative idea: don't use the quickfix list for a simple list of files. That's what the argument list is for! You have a lot of commands for navigating it (:help :args), and it's very versatile to populate: *{arglist}* The wildcards in the argument list are expanded and the file names are sorted. Thus you can use the command &...


3

What I'd like to do is "add a column" to the quickfix list that displays that "type" (so, for @CHECK it would be CHECK, and so on) An input file and the desired quickfix window would have helped, but try to source this code: com -bar QfTodo call s:qf_todo() fu s:qf_todo() abort sil vim /@\u\+/gj % let curfile = expand('%:p') ...


3

If you'd like to invoke the command from inside Vim and populate the quickfix window with its output, you can use :cexpr together with system() or systemlist(). :cexpr system('command_that_makes_these_lines') See also :cgetexpr for a version that doesn't jump to the first result (you might want to combine that with |copen to show the quickfix window with ...


3

While I don't think it's possible out of the box, you can work your way around this: command! -nargs=1 GGrep Ggrep <q-args> -- './*' ':(exclude)*.map' Then use the GGrep command instead of Ggrep: :GGrep foobar See: :h command :h nargs :h <q-args>


2

On unix like systems, you can use backticks directly in the vimgrep command (in any command that takes file names) :vimgrep "search" `cat cppfiles` | cw There is vim-only way by loading the cpp files into the argument list: :%argdelete :argadd **/*.cpp :vimgrep "search" ## ## is a special notation for the argument list.


2

"% is a read-only register. I doubt there is anything you can do to the register. Instead of simply using % in your grep command, e.g. :grep 'foo' %, use <c-r> to put in the contents of the register, e.g. :grep 'foo' <c-r>%. This will paste/put the name of the file in the grep command. To tweak your grep simply do q: and go and edit your last ...


2

Use exec with string concatenation: command! -nargs=0 GJL exec 'vimgrep ref{'.expand('<cword>').' *.tex | copen' In a function, you'd do something similar, but more neatly, I suppose: function Foo() let l:pattern = 'ref{' . expand('<cword>') exec 'vimgrep' l:pattern '*.tex | copen' endfunction


2

You can use {} to specify different extensions: nnoremap <leader>f :vimgrep /<c-r><c-w>/j **/*.{c,h,txt} <bar> :copen <CR> Some suggestions: Use nnoremp unless you want to recursive mappings You may wanto to look into 'wildignore' to ignore object files and other files which should always be ignored You may want to look into ...


2

There are plugins that address this, such as qftools or vim-qf by our own romainl. A quick google gets plenty of results.


2

According to the help you can use :1lvimgrep pattern to have only a single match per file added to the quickfix list. I have never tried that however and so I am not sure how well that works. Update Okay, I have apparently misunderstood the problem as mentioned in the comments. So there does not seem to exist an easy way to do what you want. That leaves us ...


2

You can just use <cword> directly here: function! CustomGrep() vimgrep <cword> **/*.h **/*.hpp **/*.c **/*.cpp endfunction If you look up :help <cword> you'll see it's on a section about "Ex special characters", which also cover special characters you might be familiar with, such as % and # (as in, :e #, to edit the alternate buffer.) ...


2

Did I do something wrong here? :vimgrep is a command which takes a string. expand() is a function call. You must use execute then: execute 'vimgrep' expand('<cword>') '**/*.h ...'


2

The function should just be execute 'vimgrep /\v' . a:query . '/g **/*' copen Or you could do this with the command: command -nargs=* Vsearch vimgrep /\v<args>/g **/* | copen


2

One trick that worked for me was set errorformat=%f | cexpr glob('**/*.md') The :cexpr command takes an expression and uses it to populate the quickfix list. Because the default errorformat doesn't parse plain filenames like glob returns, we set it to just include filenames. To get something like Jake's answer and avoid setting errorformat, we could do ...


2

One way to do it would be to: use :args to populate the arglist with all the files in which you're interested use search() to check whether your pattern can be found in each file, and if it does not, add its path to a list give that list to getqflist() to transform it into a quickfix list, and invoke setqflist() to add it onto the stack of quickfix lists ...


2

It concerns more shell than an editor. I guess, adding this to shell's profile should work vimgrep() { $EDITOR "+vimgrep $1 $2" "+copen"; } To call from terminal do not forget to put single quotation marks: vimgrep '/pattern/' '*.tex'


2

Simpler is function! CustomReplace(target, newWord) call CustomGrep(a:target) cdo execute 's/'.a:target.'/'.a:newWord.'/gc' endfunction Or with cfdo and %s, function! CustomReplace(target, newWord) call CustomGrep(a:target) cfdo execute '%s/'.a:target.'/'.a:newWord.'/gc' endfunction


1

With the help of @ChristianBrabandt, I finished this function: function! CustomReplace(target, newWord) call CustomGrep(a:target) let x = 0 let cnt = len(getqflist()) while x < cnt execute 's/'.a:target.'/'.a:newWord.'/gc' w if x != cnt - 1 cnext endif let x = x + 1 endwhile ...


1

I believe using an external tool is much easier in this case. Let's suppose your set grepprg=/usr/bin/grep\ -n is GNU grep (i.e. it supports grep -L). Then :set gfm=%f :silent grep -L 'pattern' files :set gfm& :copen does it. See also :h :grep, :h 'grepprg' and :h 'grepformat'. If you do this often, you may want to pack it into a user-command.


1

Try the following: set wildignore=*/node_modules/* I just played a little with wildignore, so I don't know if it works in all cases. E.g. if you do a :vimgrep /pattern/ node_modules/** It will find nothing, as all files below node_modules are ignored. See :help wildignore. Update: The option wildignore is a comma-separated list of pattern. To ignore ...


1

Set 'verbose' to 4, and execute your command again, you should see something like this: Calling shell to execute: "(echo !(File3).m)>/tmp/vT1eqVi/2" There is no way to tell whether a space belongs to filename in /tmp/vT1eqVi/2. Read after :h backtick-expansion and :h `= , you will find this: If the expression returns a string then names are to be ...


1

There's no word splitting done on the string read by Vim. That is, there's nothing special about space characters...they are considered part of the filename. There are a couple ways that come to mind to separate the filenames. This one works... vimgrep SearchExpression `printf "\%s\n" !(File3).m` (The % is escaped as Vim will otherwise treat it as special.)...


1

You can always do <up> on the command line with a prefix of :vimgrep to make searching history easier. You may also want to check out q: and the use ?// to search for the command to re-execute. Assuming the quickfix title is set correctly, you can use following the command: :execute get(getqflist({'title':1}), 'title') This however I am not certain ...


1

What about using a function to do so? function! SourceByType() " save position let savePosition = getpos('.') " Get the line containing the type normal! gg let line = search('^type: .*\n---', 'W') " no type found: abort if (line == 0) echo "no type found" return 1 endif " Get the type let type = ...


1

If you are on a *nix system you can use the Ags plugin that uses the Ag external program. Calling :Ags without arguments will search in all the files the word under the cursor. The downfall of Ags is that it search only in the path of the currently opened file, but, if you want to search all files in the directory root (like a git repo) you can add the vim-...


Only top voted, non community-wiki answers of a minimum length are eligible