Haiyuan Zhang
Haiyuan Zhang

Reputation: 42832

How to match a decimal letter and blank in vim?

I need to change

1  A
2  B
3  C
4  D

to

A
B
C
D

which means the decimal letter at the begining of every line and the following one or more blank should be deleted .

I'm only familiar with Reqex in Perl, so I try to use :%s/^\d\s+// to solve my problem, but it does not work. so does anyone of you can tell me how to get the work done using vim ?

thanks.

Upvotes: 3

Views: 2324

Answers (8)

paxdiablo
paxdiablo

Reputation: 882166

One way is to use the global command with the search-and-replace command:

:g/^[0-9]  */s//

It searches for the sequence:

  • start of line ^
  • a digit [0-9]
  • a space <space>
  • zero or more spaces <space>*

and then substitutes it for nothing (s//).

While you can do a similar thing with just the search-and-replace command on its own, it's useful to learn the global command since you can do all sorts of wonderful things with the lines selected (not just search and replace).

Upvotes: 3

Brian Rasmussen
Brian Rasmussen

Reputation: 116471

Vim needs a backslash for +, so try

:%s/^\d\s\+//

Upvotes: 8

Nope
Nope

Reputation: 36000

If it's in a column like that you could go into the column visual mode by pressing:

esc ctrl+q

then you can highlight what you want to delete

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 994121

If you still want to use Perl for this, you can:

:%!perl -pe 's/^\d\s+//'

Vim will write the file to a temporary file, run the given Perl script on it, and reload the file into the edit buffer.

Upvotes: 1

joeslice
joeslice

Reputation: 3464

You can also use the Visual Block mode (Ctrl+V), then move down and to the right to highlight a block of characters and use 'x' to remove them. Depending on the layout of the line, that may in fact be quicker (and easier to remember).

Upvotes: 1

P Shved
P Shved

Reputation: 99354

You should use instead

:%s/^\d\s\+//

Being a text editor, vim tends to treat more characters literally‒as text‒when matching a pattern than perl. In the default mode, + matches literal +.

Of course, this is configurable. Try

:%s/\v^\d\s+//

and read the help file.

:help magic

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186662

Escape the plus sign:

:%s/^\d\s\+//

Upvotes: 0

pavium
pavium

Reputation: 15118

Use the following

:%s/^[0-9]  *//

Upvotes: 2

Related Questions