Skip to main content
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 ...
DJMcMayhem's user avatar
  • 17.9k
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. ...
Niko Bellic's user avatar
  • 1,834
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 ...
ruohola's user avatar
  • 676
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 ...
laktak's user avatar
  • 2,983
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 (...
Mass's user avatar
  • 14.4k
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
Kevin Liu's user avatar
  • 359
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
D. Ben Knoble's user avatar
  • 27.7k
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 ...
Christian Brabandt's user avatar
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
kapil's user avatar
  • 438
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 ...
DJMcMayhem's user avatar
  • 17.9k
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 ...
statox's user avatar
  • 50.5k
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 ...
Rich's user avatar
  • 32.6k
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 ...
user9433424's user avatar
  • 6,198
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 ...
Hotschke's user avatar
  • 4,865
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 ...
Jon Parise's user avatar
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 '...
Peter Rincker's user avatar
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, ...
AndyB's user avatar
  • 256
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 ...
Ingo Karkat's user avatar
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.
grodzik's user avatar
  • 4,738
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 ...
kikito's user avatar
  • 361
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 ...
DJMcMayhem's user avatar
  • 17.9k
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: /\...
filbranden's user avatar
  • 29.9k
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 ...
Troy Daniel's user avatar
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 ...
Hotschke's user avatar
  • 4,865
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 ...
Sven's user avatar
  • 201
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 ...
ZeroKnight's user avatar
  • 1,089
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, ...
Mass's user avatar
  • 14.4k
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), :/...
dedowsdi's user avatar
  • 6,358
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/[()]/\=...
Christian Brabandt's user avatar
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 ...
user938271's user avatar
  • 6,032

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