Skip to main content
39 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.6k
22 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.2k
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.2k
18 votes
Accepted

How to copy first four characters of every line to the end of that same line?

:% s/\v^(\d{4})(.*)$/\1\2 \1/ is one way to do it \v magic option, to avoid having to escape grouping () ^ start of line \d{4} match exactly four digits .* rest of line \1 \2 has the matched ...
Sundeep's user avatar
  • 1,086
18 votes

Replace selection with output of external command

You could delete the text and insert the output of a command using CtrlR-=: CTRL-R {0-9a-z"%#*+:.-=} i_CTRL-R Insert the contents of a register. ...
muru's user avatar
  • 24.9k
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
  • 24.9k
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
  • 28.8k
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
  • 32k
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
  • 32k
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
  • 49.9k
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,580
11 votes

How to copy first four characters of every line to the end of that same line?

And a solution with a macro: qqyiwA <Esc>pj0q Which means: qq Record the macro in the register q yiw Yank the text described by the text object iw (inner word): The date A <Esc> ...
statox's user avatar
  • 49.9k
11 votes

Replace current selection and all identical occurrences in a file

You could execute the following command: :%s/is a/is not a : Select and yank is a with your favorite method (visual selection and y, y2w with cursor on is, etc...) Enter command mode with : Begin to ...
statox's user avatar
  • 49.9k
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,770
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
  • 49.9k
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
  • 24.9k
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
  • 20.7k
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.2k
8 votes
Accepted

Change/delete matching brackets at the same time

Surround does what you want and more: ds( delete first surrounding pair of parentheses 2ds( delete second surrounding pair of parentheses cs[{ change surrounding pair of square brackets into a ...
romainl's user avatar
  • 40.7k
8 votes

Replace current selection and all identical occurrences in a file

Using gn and . for quick replacements You can use gn motion to make replacing matches quicker. Simply search for your text /foo Then change the current match with the change operator, c, and the gn ...
Peter Rincker's user avatar
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,746
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.1k
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,750
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,750
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.6k
8 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
  • 20.7k

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