Skip to main content
40 votes
Accepted

Replace a pattern with current line number

Yes, vim can do this! Do a global search for x, and replace it with \=printf("%d", line('.'). For example: :%s/x/\=printf("%d", line('.')) You can also change x to another search, if you need to. ...
DJMcMayhem's user avatar
  • 17.7k
23 votes
Accepted

Select multiple words, one at a time, then replace them all

I’m assuming all the things to change are the same. If not, I would probably proceed with either multiples of the steps below, or combine all the patterns into a single substitute pattern. So, not ...
D. Ben Knoble's user avatar
  • 26.8k
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.8k
18 votes

Replace a pattern with current line number

Since you mentioned vertical select and replace, you can do that too. Use CtrlV to do select the lock of text you want to replace, then c and type 0, to replace that with 0s. Then, select those 0s: ...
muru's user avatar
  • 25k
18 votes

Simultaneous find and replace

Yes, you can do that by using an expression. For this particular case, you can use Vim arithmetic and calculate 1-x, which will turn 0 into 1 and 1 into 0. :s/\d/\=1-submatch(0)/g For a more general ...
filbranden's user avatar
  • 29.1k
17 votes

Simultaneous find and replace

There's a bunch of excellent answers here already, but for the sake of completeness I feel like I should point out that, in most practical respects, if you concatenate multiple ex commands with the ...
Rich's user avatar
  • 32.3k
16 votes
Accepted

How to replace string with register content?

N.B. In all the below, I presume that your question is only about the method of accessing the register's contents from an ex command, and that the rest of your :substitute command (which replaces only ...
Rich's user avatar
  • 32.3k
15 votes
Accepted

Perform previous substitution for different selection with single keystroke

I think you want to do @: which replays the last ex command. See :h @: Note that this command can be called from normal mode and from visual mode. Also in this case it is not useful but you can also ...
statox's user avatar
  • 50.2k
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

Concatenating continuation lines

For every line beginning with a space, join it with the previous one: :g/^ /-j References :help :global :help pattern :help [range] :help :join
Antony's user avatar
  • 2,590
9 votes
Accepted

How do I paste the same line of code X times but change one word in the line each time?

For what you want to do, emmet-vim, as nobe4 suggested, is your best bet. In insert mode, li{item $}*10<c-y>, will expand 10 <li>s with item N prefilled as the text. You'll start editing ...
Tommy A's user avatar
  • 6,790
9 votes
Accepted

Concatenating continuation lines

You can replace the new lines plus space by a space: :%s/\n\s/ / This is an ex command i.e. a command used in the command line mode. To enter the command line mode, press :. Then insert the command ...
statox's user avatar
  • 50.2k
9 votes
Accepted

Replace ONE occurrence of something with MULTIPLE instances of something

You are probably best off just hitting dat Tab key 5 times in your example: :%s/\s\(item.\)/\r    TabTabTabTabTab    \1/g (The tabs display as ^I in the Command-line.) More elegantly, you can have vim ...
Aaron Thoma's user avatar
9 votes
Accepted

vim search for arbitrary combination of spaces and tabs

If you're looking for a substitute command: :s/a[ \t]\+b/c/ The default regex mode doesn't see + as an operator. You have to escape it to make it special. You can also use: :s/a\s\+b/c/ \s is Vim'...
muru's user avatar
  • 25k
9 votes
Accepted

Trailing characters error in a substitution command

You don't need %, as you already have another range ('<,'>) for your command s. So it must be simply '<,'>s/SomeStuff/stuff/gI.
Matt's user avatar
  • 21k
9 votes
Accepted

Select WORD under cursor

I would then like to use the selection for a replace, something like :%s//foo/g :%s/^R^A/foo/g See :h c_CTRL-R_CTRL-A
Matt's user avatar
  • 21k
9 votes

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

The substitution is easier to read with word-boundaries in my opinion, and very magic mode makes typing easier: :%substitute/\v>\s+</_/g
D. Ben Knoble's user avatar
  • 26.8k
8 votes
Accepted

Pass word one-by-one and replace (Search and replace with confirmation)

I think you're looking for the substitute command with confirmation: :%s/word1/word2/gc This will substitute word2 for word1, but every time it will take you to the match and let you decide what to ...
Tumbler41's user avatar
  • 7,766
8 votes
Accepted

How do I replace only the current instance of the search pattern without losing the pattern?

To change the current match (or next match, if the cursor is not on a match) use cgn or gnc. Alone, gn visually selects the match.
Mass's user avatar
  • 14.2k
8 votes
Accepted

Delete block of code if a pattern is (not) detected in one of the lines

To delete the blocks containing 2.58000e+02, you can use following global command :g/\v^E(.*$\n){5}\s+.{-}2.58000e\+02/,+7d_ This breaks down as :g/ starts a global ...
Lieven Keersmaekers's user avatar
8 votes
Accepted

Using global command on Quickfix entries

Update: New official vim plugin cfilter Since 21.8.2018 (patch: 8.1.0311) the plugin cfilter is distributed with vim in $VIMRUNTIME. It is documented under :h cfilter-plugin. Load plugin cfilter ...
Hotschke's user avatar
  • 4,800
8 votes

Delete from end of line in visual block mode

One way would be to select the lines (using any of the visual modes: block, line, or regular characterwise) and then type the command: :norm! $xxx Vim will pre-populate the command line with a range ...
Rich's user avatar
  • 32.3k
8 votes

How do I use 'cgn' for word under cursor?

Make * stay at the cursor position nnoremap * m`:keepjumps normal! *``<cr> https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump Plugins for *: vim-asterisk, vim-...
Hotschke's user avatar
  • 4,800
8 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
8 votes
Accepted

Can `r` be used for multiple letters?

There are 3 slightly different ways to do this. R will enter "Replace mode". This is like insert mode except each character overwrites the character currently under the cursor. For example testing ^ ...
DJMcMayhem's user avatar
  • 17.7k
8 votes

Simultaneous find and replace

In your specific use case, i think what you can do is this handy oneline: %s/0\|1/\=!submatch(0)/g That basically inverts the match.
Christian Brabandt's user avatar
8 votes
Accepted

Date change question: Find and replace with wildcards?

:%s,\v(\d+)/(\d+)/(\d+),\3/\1/\2,g s is the substitution command, the % before it means that all lines of the file will be considered. , is the separator. It is common to choose / as the separator, ...
Quasímodo's user avatar
  • 2,466
7 votes
Accepted

Nested matching group in regex

Edit It is possible to do this in one expression if we use a "sub-replace-expression." See bottom for info on that. /Edit The problem here is that you want to do two different things. Operate on ...
Tumbler41's user avatar
  • 7,766
7 votes

Searching/replacing exact word in vim by default

There isn't an option to treat the whole pattern as word bounded as far as I know. Since you want to replace, that means the text is already in the buffer. You could use * or # on the word in normal ...
Tommy A's user avatar
  • 6,790
7 votes
Accepted

How to add accent to letters in normal mode?

If you set 'digraph', (IOW makes sure :set digraph is in your .vimrc) it could become as simple as: nnoremap <silent> µ s<c-r>"<bs>'<esc> The key points are: s<c-r>" ...
Luc Hermitte's user avatar
  • 17.6k

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