lenzinho
lenzinho

Reputation: 401

how to automatically delete patterns of the type [*] with vim

I have a text document with a bunch of references [1], [2], ..., [70], etc. How can I use vi to automatically delete them? That is, delete everything that matches the pattern: [*].

Upvotes: 1

Views: 61

Answers (3)

Kent
Kent

Reputation: 195209

%s/\[[^]]*\]//g

or non-greedy:

%s/\[.\{-}\]//g

both will do what you wanted : delete everything that matches the pattern: [*].

Upvotes: 0

zmo
zmo

Reputation: 24802

just to develop @user156213's very good answer:

:
%     —→ make the regex match the whole file range (from line 1 to last line)
s     —→ use the substitute regex command
/     —→ separator for the regex to match against
\[    —→ look for a [ character on a line
[0-9] —→ look for any digit
\+    —→ look for 1 or more occurence of thee the previous pattern
\]    —→ look for a ] character
/     —→ separator between the match regex and the replacement string
/     —→ end of the replacement string (i.e.: nothing)
g     —→ apply the match multiple times each line

Here's the same regex shown as an automaton:

Regular expression visualization

Upvotes: 1

user156213
user156213

Reputation: 766

try running this command:

:%s/\[[0-9]\+\]//g

It finds all patterns of numbers in brackets \[[0-9]\+\], and substitutes :s all occurances g on all lines % with an empty string.

In order to run it, start out in normal mode, and type the command above, including the colon.

Upvotes: 3

Related Questions