gunererd
gunererd

Reputation: 661

change last comma to tab character for each line in Vim

Can you help me changing

abc,abc,12345

to

abc,abc    12345

in vim?

Upvotes: 0

Views: 1105

Answers (3)

SergioAraujo
SergioAraujo

Reputation: 11810

:%s/,\([^,]*\)$/ \1

Explanation

: ........ command
% ........ entire file
/ ........ start searching
, ........ comma 
\( ....... opening regex group
[^,]* .... anything but comma
\) ....... closing regex group
$ ........ at the end of line
/ ........ end search
Ctrl-v TAB .... to insert tab
\1  ...... back reference to regex group

Upvotes: 1

Andris Leduskrasts
Andris Leduskrasts

Reputation: 1230

Wasn't so hard to google the wiki.

:%s/.*\zs,/\t/ or :%s/.*\zs,/^I/

:s/old/new/ is the syntax, but vim has aditional features for searching and replacing in particular, \zsfoobar\ze let's you select foobar for replacing.

In your example .*, like in any regex, means any character any number of times, or just "some text that is as long as possible", followed by , that is selected with the \zs feature. Since it's already the end of the pattern, you don't need to use \ze. Then you replace the selected pattern with \t AKA the ascii tab symbol.

If you didn't select anything, the whole pattern (including the "some text" part) would be replaced. You can do fine without it and some other regexes, but if you're using a tool or language, might as well use its features.

Upvotes: 6

romainl
romainl

Reputation: 196566

Fo the sake of diversity:

:%norm $F,r<C-v><Tab>
  • move to the end of the line
  • jump to first comma to the left
  • replace it with a <Tab>.

Upvotes: 3

Related Questions