114
votes
Accepted
How to find and replace in Vim without having to type the original word?
I actually have a pretty similar workflow to yours, (copying and pasting blocks that are similar, then using :s to change variable names) especially when I'm writing lots of lines that are similar ...
99
votes
Vim search replace all files in current (project) folder
Let's say we have a simple project structure like this:
greeting.txt looks like
and info/age.txt looks like
Let's say we want to replace all occurrences of Sam with Bob. Here's what we would do:
1. ...
38
votes
Is there a way to count the number of occurrences of a word in a file?
Nowdays (meaning Vim 8.1.1270) you can use:
set shortmess-=S
to enable a count of [x/y] showing in the bottom right corner every time you do a / or ? search.
Relavant section from the Vim help
Note ...
28
votes
How can I perform a search when vim displays content using "more" pager?
Vim 8 has a execute() command the allows you to get the output of a command. To see it to the current buffer do:
:put =execute('let')
For Vim 7 you can use capture.vim. It is a plugin that wraps the ...
27
votes
How to find and replace in Vim without having to type the original word?
The c_CTRL-R family of maps can improve your workflow quite a bit:
You need not type variable names when using :s/, just use CTRL-R CTRL-W to insert the word under the cursor into the command line (...
26
votes
How to show only matching lines?
[d]elete all lines not(!) matching patterns:
:g!/pattern/d
or even simpler (thanks for the comments by 'B Layer'):
:v/pattern/d
20
votes
How to replace a pair of brackets
With tpope/vim-surround, this is a simple
cs)]
away in Normal mode.
cs is the "change-surrounding" operator
) is the "target"
] is the replacement
19
votes
Accepted
What's the difference between the '\zs' and '\@<=' atoms in Vim regex?
This looks like you indeed found an obscure bug. I have implemented the gn textobject back in 2012 for Vim 7.3 something. It basically works in the following way:
1) It searches backwards for the ...
18
votes
Delete from cursor till nth occurence of character x
You can do this by typing
<n>df<x>
where:
<n> is the number of occurrence of particular character
df<x> means delete till you find the occurrence of character x
18
votes
How to search for any repeating character, X number of times
There's two different regex features that would be helpful. First off, you can use capturing groups. If you put something in parentheses, you can refer to whatever matched inside. So
/\(.\)\1
Will ...
17
votes
Accepted
How can I automatically center first search result?
You can use the following mapping:
cnoremap <expr> <CR> getcmdtype() == '/' ? '<CR>zz' : '<CR>'
Which can be explained like this:
cnoremap Create a mapping in the ...
17
votes
How to find and replace in Vim without having to type the original word?
There are a couple more methods that (to my surprise!) haven't been mentioned yet.
Using the gn Command
gn works like the n command, except that in addition to jumping to the match, it enters visual ...
15
votes
Accepted
How to interactively search/grep with VIM?
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 ...
15
votes
How to display in the statusline the number of matches from a search?
vim 8.1.1270 (May 2019) :set shortmess-=S
Since May 2019 vim offers the possibility to show the count in the statusline (as long as it less than 99) which however is disabled by default. Add to your ...
14
votes
How to automatically turn off "hlsearch" after I'm done searching?
vim-cool is a simple plugin that does just this.
Vim-cool disables search highlighting when you are done searching and
re-enables it when you search again.
It works without remapping by using ...
14
votes
Accepted
Percent key ( % ) matching behavior for angle brackets ( < > )
'matchpairs' controls what characters form pairs which % will work upon. You can add angle brackets by doing the following command as suggested by :h 'matchpairs':
:set matchpairs+=<:>
Since '...
14
votes
Accepted
How to replace a pair of brackets
You can do it in a single substitute command like this
:s/(\(.*\))/[\1]/
The \( and \) mark the regex subexpression that matches everything inside the ( and ) in the input line. In the replacement, ...
13
votes
Accepted
different search patterns per window / buffer
You can use :autocmds to switch the current search pattern (register /) as you move along windows and buffers, but Vim will never highlight different patterns at the same time, as this still is a ...
12
votes
Accepted
search line which contains forward slashes
Use backward search :h ?. This way you type ? instead of / to start search, but this also allows to use / in search pattern.
12
votes
Accepted
Is there an incsearch for the replace command?
After a bit of searching, I found this NeoVim issue, which in turn was closed by this PR, which brought me to the inccommand setting. Adding this to my config file:
set inccommand=nosplit
makes ...
12
votes
Faster way to search for whole words
There are two different ways to do this.
You can insert those and the word currently under your cursor with *. So for example, if your cursor is on "hello", and you press asterisk, vim will treat ...
12
votes
Accepted
Case-sensitive search with `smartcase` set
You can use \C to force the pattern to be case-sensitive.
See :help /\C, which explains it alongside \c:
\C does the opposite: Force matching case for the whole pattern.
In your specific case: /\...
10
votes
Accepted
search for lines which contain a pattern but do not contain a second pattern
And there is also another way with :global command, as follows:
:g/This\C/v/red/p
and the first :g command filter the lines contains This, and then passed into the second :v command to show lines ...
10
votes
How to show search results for all open buffers
Commands :bufdo and :vimgrepadd
This answer is based on https://stackoverflow.com/a/11976158/1057593.
However, I wanted add a few bits. Again the basic command is
:bufdo vimgrepadd pattern %
If your ...
10
votes
Is there a way to count the number of occurrences of a word in a file?
Thanks to this PR https://github.com/vim/vim/pull/4317 , each search command shows search statistics (like current match position, and number of matches) in vim 8.1.1270. Make sure to remove S from ...
10
votes
Accepted
Is there a key to go to the next search result that is not on the screen currently?
Ctrl+F n
Scroll one page forward (downward) in the buffer, then find next match.
It's not the most elegant solution, but it is simple, and it doesn't require you to think of the appropriate count ...
10
votes
What's the difference between the '\zs' and '\@<=' atoms in Vim regex?
Christian has completely addressed the question of buggy behavior of gn, but there still are fundamental differences between \zs and \@<=. One of these being \@<= modifies a preceding atom, ...
10
votes
Accepted
What's the difference between `/x` and `:/x`
/xx place cursor at xx, it's a :h / .
:/xx is a :h :[range] , not :h :range , it's a valid ex command by itself, it's behavior is affected by :h 'startofline' .
If 'startofline' is on (default), :/...
9
votes
How to replace a pair of brackets
You can use this simple :s command (or better two :))
:s/(/[/g
:s/)/]/g
You can even do it in one run, you you define your replacements first like this:
:let replace={'(':'[', ')':']'}
:s/[()]/\=...
9
votes
Show exact number of matches even for large (>99) numbers
If your Vim binary includes the patch 8.2.0877, you can get the search statistics via the searchcount() function. And the latter is not limited to 99 matches.
You can invoke it right after a / search ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
search × 494regular-expression × 91
replace × 73
vimscript × 38
highlight × 38
key-bindings × 28
substitute × 25
command-line × 21
vimrc × 18
neovim × 17
cursor-movement × 14
register × 12
cut-copy-paste × 10
buffers × 9
normal-mode × 9
functions × 9
global-command × 9
navigation × 8
syntax-highlighting × 7
autocmd × 7
cursor-motions × 7
quickfix × 7
multiple-files × 7
range × 7
find × 7