gvim - jumplist to functions within a file - vim

I'm using VIM to work with Powershell files. How do I make gvim:
show full list of strings that match some regex? (either in new buffer or in command window)
go to a line selected in the found list?

The following command will get you all the matching lines into the command window.
:vimgrep /INSERT_EXPRESSION_HERE/ % | cw
You can then use normal vim navigation to find the line inside the command window, and hit Enter to jump to that line in the file. To return to the list again, you can use the normal vim Window movement commands C-w,j in normal mode.

For a non-persistent list of search results, you can use the built-in :ilist command to list and :ijump to jump. My FindOccurrence plugin has extended mappings ([/ to query for a pattern, list all occurrences, and query for a number to jump to, and [N which uses the current search pattern). Here's a little demo:
To persist the list of search results, :vimgrep with the quickfix list can be used (as shown in #merlin2011's answer). My GrepHere plugin makes this even easier. Again, a short demo:

Related

Vim: See all instances on page and choose one

I seem to recall a vim plugin that will allow you to, essentially, execute ":g/pattern/p" and then prompt you to select one. Does anyone know of this plugin? Or is it a built-in function?
Basically, I want to perform a search, see all instances on the current page (or even in open buffers), and then be able to select which one to go to.
Thanks!
EDIT:
I want it to actually take me to the line number when I make my selection, and I want it to be a fairly simple solution. I'm pretty sure I've seen a plugin for this, but I can't remember what it was. Any thoughts?
PS. Thank you to all who are answering. They're great answers.
You can use :vim[grep] or :gr[ep].
:vim foo % | cw
See :help quickfix.
If you want a plugin, you'll have to search vim.org.
This might get you close. Its not an interactive menu but it does tell you the line number for the match
:g/<regex>/#
The # tell global to print the line numbers. Take a look at :h :number (# is a synonym for :number)
You could populate the result into the quickfix window via the :vimgrep command
:vimgrep/regex/ %
% represents the current buffer's filename. Note: buffer must be a file and not a scratch buffer.
Then use quickfix commands like :cnext and :cprev to move through the list. Or open the list via :copen and press <cr> to jump to the match.
You can :vimgrep any number of files:
All *.c files e.g. :vimgrep/regex/ *.c.
Use ** to search down into deep directories e.g. :vimgrep/regex/ **/*.c.
You can also use vimgrep with the args list via :vimgrep/regex/ ##.
For more help see:
:h :vimgrep
:h quickfix
:h c_%
:h arglist
:g/regex/p
:g executes :p by default, so it can be omitted; the trailing slash can also be dropped:
:g/regex
Note that :g jumps to the last occurrence in the file, so you can then execute N/ (in normal mode) to jump to the Nth occurrence. Or, if there are many matches and you don't care to count which one you want, you can at least jump to its line, L, with LG or Lgg or :L.
If you don't use :set number but want line numbers in the output of :p, append nu[mber] or # as #FDinoff suggests:
:g/regex/nu
:g/regex/#
You can confirm one change after another in all open buffers with:
:bufdo %s;<pattern>;<replace>;c | update
The :bufdo goes through all buffers. The c is the flag to confirm all changes. The update writes the current buffer which allows going to the next.

Editable vimgrep string in command line

I use vimgrep a lot to navigate in files and usually use the last search from the history to modify the search pattern and run it again.
Is there a way to display in the command line an editable string like the one below, with the cursor already positioned between the two search pattern slashes (and the pattern being empty)?
:vimgrep // **/*[ch]|copen
I don't want to use a constant mapping (like the one at this vim tip) since I want to be able to add/change options (\c etc.).
I'd recommend using the command-line window for this (q: opens it from normal mode), since you
can edit the command with the regular normal mode keystrokes (and you get syntax highlighting too).
You can also move around in your history just like in a normal buffer. So ?vimgrep<Enter>nnn... will search for and move you to all your old vimgrep commands.
Just hit <Enter> as normal when you are done editing, or :q<Enter> to abort the command and quit the window like you would any other.
Finally, here's a mapping to quickly bring up your empty vimgrep template in the command-line window.
:nnoremap \v q:ivimgrep<Space>//<Space>**/*[ch]<Bar>copen<Esc>F/;i
Reference: :help cmdline-window

Find/Grep in all VI buffers

With many buffers open, I need a simple way to search all buffers for a regex and navigate the search result (quick list?)
I know I can :bufdo command, and it is easy to search and replace with %s, but I can't find a way to do just a simple search and then navigate the results.
I found plugins for that (e.g., buffergrep), but I'll be surprised if this simple task is not natively supported with a vim trick.. is it?
:grep & co. will populate the QuickFix buffer, which allows for fast navigation among results.
from :help grepadd
:grepa[dd][!] [arguments]
Just like ":grep", but instead of making a new list of
errors the matches are appended to the current list.
Example:
:call setqflist([])
:bufdo grepadd! something %
The first command makes a new error list which is
empty. The second command executes "grepadd" for each
listed buffer. Note the use of ! to avoid that
":grepadd" jumps to the first error, which is not
allowed with |:bufdo|.
An example that uses the argument list and avoids
errors for files without matches:
:silent argdo try
\ | grepadd! something %
\ | catch /E480:/
\ | endtry"
"I found plugins for that (e.g., buffergrep), but I'll be surprised if this simple task is not natively supported with a vim trick.. is it?"
Not that I know of. And existence of multiple plugins trying to offer this functionality tends to confirm that. . .
What plugins have you tried and what have they been lacking?
http://www.vim.org/scripts/script.php?script_id=2545
http://www.vim.org/scripts/script.php?script_id=2255
Also, just to make sure, you are aware of vimgrep, right? Vimgrep is an internal command that loads files into buffers and does greps on the buffers, with results in quickfix window. I haven't confirmed, but I assume if a searched file is already open in a buffer that Vimgrep doesn't reload it, at least not if it has 'nomodified' flag set. If so, one way to use Vimgrep for quick-and-easy buffer grepping would be to just create a file list for Vimgrep using the output from the :buffers command.

How do you use vim's quickfix feature?

I'm a pretty new Vim user and I've found that its learning curve is quite steep (at least for me). I just installed this vim script for JavaScriptLint error checking, which shows errors in vim's quickfix window once I save a buffer.
However, I don't know what to do next.. How do I 'scroll' through all the errors? How do I close the quickfix 'window'? How do I get it to check for errors after I've made changes to my code?
I've looked at the vim quickfix docs but the amount of commands are overwhelming and I can't seem to find what I want. Any help would be appreciated.
A side question: is there any way to have javascriptlint check for js errors for code residing in a .html file?
There are a lot of commands for quickfix as you have said, but I tend to find I only use a small subset of them:
:copen " Open the quickfix window
:ccl " Close it
:cw " Open it if there are "errors", close it otherwise (some people prefer this)
:cn " Go to the next error in the window
:cp " Go to the previous error in the window
:cnf " Go to the first error in the next file
:.cc " Go to error under cursor (if cursor is in quickfix window)
I tend to use this with :make and :vimgrep, so I can't comment on the Javascript lint checker, but this should give you something to get started.
Regarding the general use of JavascriptLint, I'm not a javascript programmer, but it looks like the script exposes a function called "JavascriptLint", so if you want to call it manually, you can use :call JavascriptLint(). However, it works on the disk copy of the file, so it'll have to be saved first. If (and only if) the command line jsl works on html files, you should be able to use :call JavascriptLint() on an html file to check the internal javascript. You could also do:
autocmd BufWritePost,FileWritePost *.html call JavascriptLint()
to automate it. If jsl doesn't support html files, then (short of patching the application or asking the author to change it), it's probably a lost cause...
The easiest way to navigate the quickfix list (or the location list, for that matter) is the unimpaired plugin.
Once the quickfix window is populated, [q and ]q go forward and back (respectively) in the quickfix list. [Q and ]Q go to the beginning and end (which is especially handy if you only have one item in the list; this makes vim complain about [q and ]q). So the workflow is:
Run whatever command populates the quickfix list
Type [Q to go to the first item
Scroll through subsequent items (if any) with [q and ]q
If you're using Syntastic, you'll get the location list instead of the quickfix list. No problem; just use [L, ]L, [l, and ]l in the same way.
unimpaired has loads of other handy mappings too -- [e and ]e "bubble" lines up and down, [<Space> and ]<Space> insert blank lines above and below, etc. I was surprised nobody mentioned it here before; that's probably because it didn't exist until January 2010, though the question was asked in 2009.
Put the following two lines in your .vimrc file:
map <C-j> :cn<CR>
map <C-k> :cp<CR>
Now you can navigate through the errors using ctrl-j and ctrl-k, which mimics the standard down and up motion commands j and k.
You can also use :cc 2 (or any other number) to jump to, in this case, the second error in the quickfix window. Navigating with :cn, :cc 4, etc will put the cursor on the line in question.
In addition to #DrAl's great answer about how to open and close the quick window and navigate between entries, I made an image to show some of the other quick fix navigation commands.
Each group of 3 files below represents a set of quickfix results, e.g. from a vimgrep. cnewer and colder are for going through historic result sets.
Maybe this option didn't exist when this question was written (or maybe I'm embarrassing myself because there's something in my .vimrc that makes this happen) but when I get a Quickfix List, I just navigate it with j and k then hit <CR> (i.e. the Enter key) to jump to that place in the file.
Then, to get back to the Quickfix List I type Ctrl+W j for "move down a window" and I'm back.
Finally, when I'm done, I just type :q, like I would to close any normal window.
The best-practice way of integrating JavaScript syntax-checking is using the Syntastic Vim plugin, which is using Vim's location-list (which is parallel to the quickfix) window.
I've written answers for this question and this question explaining how to do it, plus also how to get source-code browsing / tag-list for Vim using the community-driven jshint.com (which is way better than JSLint IMO) and Mozilla's DoctorJS (formerly jsctags).
the quickfix window is operated mostly like any other vim window: j down a line, k up a line, :cn to jump to the next error/warning, etc.
experiment!
Although this requires > Vim 7.4.858, the cdo (or ldo for location lists) command allows updating a non-contiguous set of lines in a way you could once only do with sed:
:vimgrep /re/ %:p
:cdo! norm #a
# or
:cdo! s/re/repl/
The above shows running a recorded macro or a simple search and replace. Missing seems to a be a way to pipe through and external command as you can with :range! command

Hide all (not)matching lines in Vim

Is it possible to show/hide all matching lines in vi or Vim? Not highlight but just show only those lines.
For example I have a text with word the word ERROR. How do I make it show only lines containing ERROR and how to show only lines without ERROR?
Is there a solution without deleting all matching lines and then just undoing it?
Do you know about the :global command? Does this do what you want?
:g/ERROR
and for the opposite:
:g!/Error
or equivalently:
:v/Error
Another approach depending on your use case would be using vimgrep and its results in quickfix. You can do the following:
:vimgrep pattern % will search the current file and take you to the first search result. More importantly it also puts the results in the "quickfix list".
:copen will then open the quickfix list in a separate quickfix-window. So you will have a separate window with all lines from your last vimgrep. Inside the quickfix-window you can then hit Enter or double-click on a line to jump to the corresponding line in your original file.
:colder will let you go back to older quickfix lists (older vimgrep results). And :cnewer goes forward to newer search results.
Note that the quickfix list is also updated when running :make (which is why its called quickfix for fixing errors). Because of this there also is an alterative to the quickfix list called the "location list". To use it instead you use :lvimgrep, then use l-prefixed commands rather than c-prefixed commands - :lopen, :lolder, :lnewer.
There is, of course, a lot more you can do. See :help quickfix for more info.
PS, You said you didn't want an approach that deletes lines and then undoing them. But since you marked g/ERRORas the answer I thought I would point out a quick and dirty way is to do g!/ERROR/d. You can then easily undo it using u. Also FYI, you can do :set hlsearch to highlight patterns matched with :g commands.
You can use
:g/ERROR/
to print all the lines with ERROR
Also there is a Vim plugin which I saw many times but didn't use:
foldsearch : fold away lines that don't match a given pattern
The best way to do this is->
:vimgrep /something/g % | copen
This will open the list of matches for your keyword and also will show only the matched lines in quickfix window.
Replace % with path to file if not considering the current file.
:vimgrep /something/g % | copen works awesome. Also :g/<pattern>/d can be used to delete lines with the pattern
in case you happen to use fzf you could use:
:Lines in all open files
:BLines only in open buffer
:Rg [pattern] using ripgrep
You probably mean command in less vi vim
& /pattern/
which shows lines containing /pattern/ (like grep).
Some hackish dirty way to do this:
:w (save)
ggdG (deletes everything)
:.!grep something % (replace current line with grep output)

Resources