rnso
rnso

Reputation: 24535

How to select text till first character?

I want to replace text before first ':' in following text:

S No. 1: This is main text having a colon (:) in it.

I want above to get converted to:

This is main text having a colon (:) in it.

I used following code in vim:

s/\v.+://

However, it converts above text to:

) in it.

How can I remove part before first colon (:)?

Upvotes: 2

Views: 891

Answers (2)

Patrick Bacon
Patrick Bacon

Reputation: 4640

Using the Any Character Character,., with a Greedy Quantifier Does not Help You

I just changed the greedy quantifier, \+, with a non-greedy quantifier, \{-1,}, which looks to match at least once in a non-greedy manner.

I just used this ex command:

:s/^.\{-1,}:/

Explanation:

^ Start of line anchor

. Any non-end of line character

\{-1,} non-greedy quantifier (at least 1 instance of pattern). If you anticipate scenarios where there might not be any occurences, the quantifier, \{-}, would be more accurate (and shorter...credit to Peter Rinkler).

enter image description here

If you look in help, :help greedy, you will find a nice explanation on how to perform non-greedy searches.


Peter Rinckler also suggested that you could just create a non-colon character class such as [^:] in lieu of . (the any character except end of line character) and use the * quantifier (0 or more instances of the character class).

This is how this substitute ex command would look:

:%s/^[^:]*://

Upvotes: 4

Kent
Kent

Reputation: 195039

In addition to :s command, you can also do it using :normal command:

:%norm! df:

Or macro, on the first line, press:

qq0df:j@qq

Then press @q

Upvotes: 3

Related Questions