Get how many times a given pattern are matched in vim [duplicate] - vim

This question already has answers here:
Show Count of Matches in Vim
(11 answers)
Closed 8 years ago.
I an use /pattern to match patterns in the current file, is there a way to show many many matches there are after I hit enter for previous search command? Then I will have a sense that how many navigation to do to go through all of them.

You can define a simple mapping that prints the number of matches:
:nnoremap <A-n> :%s///gn<CR>
41 matches on 17 lines
My SearchPosition plugin provides a more elaborate variant of this:
1 match after cursor in this line, 8 following, 2 in previous lines;
total 10 within 11,42 for /\<SearchPosition\>/
There's also the IndexedSearch plugin, which integrates the reporting with the n / N commands.

As an alternative to /pattern, you could use:
:vim /pattern % | cw
to open a list of matches in the quickfix window.
You can also use:
:il[ist] /pattern/
and choose from the list with:
:{line number}

Related

Remove until end of the line after the same character from different locations in multiple lines [duplicate]

This question already has answers here:
Delete all characters after "." in each line
(5 answers)
Closed 17 days ago.
I want to remove anything from : to the end of line in all of the following lines in Vim:
key1: A
key2_long: B
key3_longerrr: C
key: D
So the result would be:
key1
key2_long
key3_longerrr
key
For a single line I use f:d$. What are better ways to do this for all lines, wihtout repeating f:d$ for all lines?
:%s/:.*//g
It searches and replaces what matches the regex between the first and second / with what is between the second and third in all the file.
you can find more on this in the documentation, look for substitute
If you want to stick with f:d$ (which should be f:D), you can use :normal for a one-off macro:
:,+3normal f:D
or:
vjjj:normal f:D
See :help :range and :help :normal.

How to search and substitute inside Vim? [duplicate]

This question already has answers here:
How can I replace a pattern only on lines that do or do not contain another pattern?
(2 answers)
Closed 1 year ago.
Is there a way to specifically search for a pattern first and then replace using %s
Why I specifically chose vim is because I can easily make change using :wq
Consider I got a file abc.txt where I need to replace all two's with three:
cat abc.txt
one two
two two
one two
But I only need to replace only those lines that is having "one".
Is there a way to first grep or filter lines having "one" inside vim and then replace using
:%s/two/three/g.
I need a one-liner command that can filter out all lines having "one" and perform substitution.
Sample output
cat abc.txt
one three
two two
one three
Many thanks in advance!
Use a Capture Group or a Look Behind Pattern to Solve Substitution
This ex command should meet your needs:
:`%s/\(one\) two/\1 three/
This is also a classic look behind pattern matching problem (:help /#<=).
:%s/\(one\)\#<= two/ three/
Another approach might be to use \zs, \ze pattern matching technique. Look up :help \zs.
%s/one \zstwo\ze/three/

Negative numeric argument in Vim [duplicate]

This question already has answers here:
Delete n lines in the up direction in vim
(5 answers)
Closed 9 years ago.
Is there a way to apply a vim command backward? For example, I want to kill 5 lines backward, instead of 5dd, is there something like -5dd?
From :he d:
["x]d{motion} Delete text that {motion} moves over [into
register
x]. See below for exceptions.
How about 5dk 4dk (k being the motion for upwards)?
Edit: changed count to 4 as this results in 5 lines being deleted apparently...

How to move to a certain character from right to left of line in Vim [duplicate]

This question already has answers here:
Vim - Delete til last occurrence of character in line
(5 answers)
Closed 8 years ago.
This is a sentence
If the cursor is on s of This, I want to move the cursor on n of sentence
I can do $Fn
But this can't be used combining with other command like delete
e.g If the cursor is on s of This, and delete all between cursor and n of sentence
d$Fn doesn't work
Anyone knows how to do this?
Assume that you have trouble with deleting words between s and the second n in "sentence". You can do it by
d2fn
And as #kev mentioned, easymotion is a good choice, with easymotion you can do it in a more intuitive way like
d<Leader><Leader>fn
the above command will highlight n in the line and let you choose.
My JumpToLastOccurrence plugin extends the built-in f/F/t/T motions with counterparts that move to the last occurrence of {char} in the line. Your example would be d,fn.
Vim plugin easymotion can help you.
As others have said, you can use the search command / in combination with the d command to delete up to a pattern match.
But, you can also make use of search offsets to place the cursor anywhere you want in relation to that search. See :help search-offset for details, but in your case:
d/senten/e will delete up to and including the second 'n' in "sentence".
You could also use d/sentence/e-2 to do the same thing but limit the match even more.
This is very powerful in combination with incremental search and search highlighting, because then you can see exactly what you're acting on before you hit <Enter> to finish the command, or <Esc> or <C-C> to cancel the whole thing.
You could use visual mode: v$Fnd
My Vim plugin ft_improved can also help. You simply keep on typing until the match is unique.
I don’t know which “n” in “sentence” is referred to here, but you can indeed use d to do this. Simply combine it with search (/):
d/n
Or, if you want to delete up to the second “n” in “sentence”, you could make the search pattern one character more specific:
d/nc

Vim Search with "Current Occurrence / Total Occurrences" Shown at the Bottom [duplicate]

This question already has answers here:
Show Count of Matches in Vim
(11 answers)
Closed 8 years ago.
I am always using */# to search for the next/previous occurrence of the variable or function under cursor. Is there a way to display the current occurrence and the total occurrences of my the search result at the bottom of Vim. For example, it could be something like 1 of 5 when you search a word in Chrome.
It doesn't need to list all the occurrences in a window, but I guess it should be able to know all the occurrences in the background.
You can use vimgrep that will open the list in the bottom
:vimgrep foo %
:copen
:cclose to close the list.
You can use :cnext or :cprevious to navigate in the list of results.
% is an alias for current file name & path.
You can also use the option grepprg and the command :grep to use system grep.
As mentioned by sehe, lgrep or lvimgrp is another possible variable.(with associated lopen, lclose,...)
Have a look at :help grep to see what options are better for you.

Resources