tags:

views:

102

answers:

8

Hi Guys,

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.

+2  A: 

Use the following

:%s/^[0-9]  *//
pavium
+6  A: 

Vim needs a backslash for +, so try

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

Brian Rasmussen
Brian, hope you don't mind, I added the ":%" to ensure it's an ex command and works across all lines, not just the current one.
paxdiablo
Not at all. I figured the OP knew this, so my point was just that Vim is funny when it comes to +, but it is good to have the complete command for reference. Thanks.
Brian Rasmussen
+2  A: 

One way is to use the global 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//).

paxdiablo
+1,this global search feature is very interesting, ":s" has done everything I needed so I never looked further into the search features
Fire Crow
@Fire Crow: If i were using global feature, I'd write `:%g/^\d/norm ^dW`
Pavel Shved
The :s command is really just a shorthand way of doing :g with a substitute command (and across the whole file). Its power lies in the fact that it's not limited to substitution - you can use it to execute any colon command on the lines/patterns found.
paxdiablo
A: 

Escape the plus sign:

:%s/^\d\s\+//
meder
+1  A: 

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
Pavel Shved
A: 

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).

joeslice
A: 

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.

Greg Hewgill
A: 

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

Casey