jian
jian

Reputation: 4824

How to Insert character based on position VIM

So far what I know. the '^' character is a special search character denoted for the start of line.
and g denoted to global change. Based on How to add text at the end of each line in Vim? and How to insert text at beginning of a multi-line selection in vi/Vim

: - enter command mode
% - for every line
s/ - substitute
$ - the end of the line
/ - and change it to
, - a comma

Below there is: 3 fields: integer, string/alphabetic, integer.
I have been trying to solve the following problems.

    (1     a        80
    (1     b        50
    (2     a        90
    (2     b        120
    (3     a        200
    (3     b        140
    (4     a        110
    (4     b        430

Upvotes: 0

Views: 680

Answers (2)

chipfall
chipfall

Reputation: 360

Because your data is aligned, visual mode would make this easy. Assuming you don't want to touch the whitespace, which could be tabs or spaces:

CTRL-v           # enter visual mode with cursor on first int in first line
j or down arrow  # to the last line/last int
shift-i , ESCAPE # to insert comma and apply to visual range

The single quote problem is almost the same, except hit shift-a to append the trailing single quote. For adding a comma to the end, select on any column to end and hit:

$A,

which will append to the end of the line. I am also assuming here there is no trailing whitespace else it will be at the end of that.

Upvotes: 0

Vedant36
Vedant36

Reputation: 328

if you really want to use Ex, then

:%s/\((\d\+\)\s\+\(\w\+\)\s\+\(\d\+\)/\1, '\2', \3

and press enter. The \( \) blocks are the vim regex equivalent of regex groups. To learn more about vim regex, which is a lot different from normal regex: http://www.vimregex.com/

Upvotes: 1

Related Questions