2

I have some text in the following format:

column1 DATE,
column2 VARCHAR2(6),
column3 VARCHAR2(15)

(It is the format of Oracle column definitions in case you are wondering.)

I’ve been trying to figure out how to replace everything past the space in each line with VARCHAR2(255),, but haven’t been successful yet.

I know I can search the occurrences of space via /\s, but I can’t figure out how to get the rest of the string on the line. When I use /\s*, it highlights all the text. I attempted to use /\s.+, but I get a “Pattern not found” error.

How can I get all text past the space in each line and replace it globally with another string?

ib.
  • 27,830
  • 11
  • 80
  • 100
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104

4 Answers4

2

Easiest way:

:%s/ .*/ VARCHAR(255)

VIM uses a slightly weird regex syntax so if you wanted to do \s+ you'd have to use \s\+

KernelM
  • 8,776
  • 2
  • 23
  • 16
2

How about

:%s/\s\S\+$/ VARCHAR(255)/
hochl
  • 12,524
  • 10
  • 53
  • 87
  • Why didn't you use `qq0f C VARCHAR(255)q`, then `:%norm!@q`? [Aren't substitution one-liners tedious to think?](http://stackoverflow.com/questions/9903660/vi-how-to-generate-a-number-sequence#comment12636451_9903819) ;-) – ib. Mar 29 '12 at 03:24
  • Thanks for pointing this out, of course it is a viable solution which you should have added as an answer. I will upvote on it if you post it as one, which is more than I got for my valid solution ^^ – hochl Mar 29 '12 at 08:19
  • It happens sometimes. I think, this commands deserves at least one up vote. +1 – ib. Mar 29 '12 at 09:17
1

One way:

:%s/\(\s\+\).*$/\1VARCHAR(255)/

For every line (%), find the first run of whitespace characters (\(\s\+\)), save them in match group 1 (to be referenced as \1 in the replacement string), match the rest of the line (.*$), and replace it with the desired literal string.

(You must escape the + character in the regexp in magic mode. You can use the very magic mode with \v at the beginning of the regexp. See :help magic for details.)

ib.
  • 27,830
  • 11
  • 80
  • 100
Birei
  • 35,723
  • 2
  • 77
  • 82
1

As an alternative to using substitution commands (which have already been proposed in other answers), one can repeat a sequence of Normal-mode commands on every line using the :normal Ex command:

:%norm!f C VARCHAR(255)
ib.
  • 27,830
  • 11
  • 80
  • 100