Skip to main content
110 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.7k
98 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,824
42 votes

How to automatically turn off "hlsearch" after I'm done searching?

There is a much more convenient way. Rather than using :set nohlsearch which actually turns the hlsearch setting off, use :nohls This will only turn hlsearch off until you search again. From :...
DJMcMayhem's user avatar
  • 17.7k
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
  • 666
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,953
27 votes
Accepted

Is there search and replace history in Vim?

Yes, you have the history command: :history / Note that it can be used for /, :, =, >, ?, @, all, cmd, debug, expr, input and search. Alternatively you can use the q:, q/ and q? commands to see ...
nobe4's user avatar
  • 16.1k
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
26 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.2k
24 votes
Accepted

When to use :grep and :vimgrep in vim?

As the doc say: Vim has two ways to find matches for a pattern: Internal and external. In a nutshell, :vimgrep uses Vim's grep mechanism to read and find in files. :grep uses an external commands to ...
nobe4's user avatar
  • 16.1k
19 votes
Accepted

Duplicate lines beginning with "x", replacing "x" in the duplicate with "y"?

Here you go: :g/foo/t.|s//bar Decomposing: :g/foo/ " start a global command applied on all lines matching 'foo' t. " duplicate the current line (the cursor is now on the new line) | ...
nobe4's user avatar
  • 16.1k
19 votes
Accepted

Ignoring diacritics/accents when searching

As @muru mentioned in the comment, you could use an equivalence class (described in :help /[[) which seems to be a character class expression evaluated as a set of similar characters (i.e. are the ...
user9433424's user avatar
  • 6,148
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
19 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
  • 26.6k
18 votes
Accepted

Visual select current search result

You can use the gn motion for that, it selects the next searched element. You can use it like so: /foo<CR> gn -> select the next "foo" sbar<Esc> -> (optional) ...
nobe4's user avatar
  • 16.1k
17 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
  • 428
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.1k
17 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.7k
16 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.2k
15 votes
Accepted

Jump to next occurrence of the word I just changed

The text you just changed with cw was cut into the unnamed register so you can reuse it with: /<C-r>"<CR> to jump to the next occurrence of the text covered by your last change.
romainl's user avatar
  • 41.1k
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,148
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,790
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

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

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
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
  • 17.9k
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.7k
11 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,658
11 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
  • 29k
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

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