0

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.

  • insert a comma , after first integer value field.

  • insert single quote '' for the second alphabetic string,

  • insert an comma to separate string value field to the last integer value field.

    (1     a        80
    (1     b        50
    (2     a        90
    (2     b        120
    (3     a        200
    (3     b        140
    (4     a        110
    (4     b        430
mattb
  • 2,787
  • 2
  • 6
  • 20
jian
  • 4,119
  • 1
  • 17
  • 32
  • Your "problems" are stated ambiguously. Add an "after" sample that shows exactly what you want to obtain and show us what you tried. Also, Vim comes with a very thorough yet very easy to follow built-in tutorial, `:help user-manual`. Follow it instead of convincing yourself that you are learning Vim via stack exchange answers, you are not. – romainl Dec 19 '21 at 17:52
  • Please do not include pictures of text - rather include the text as text, so people can copy it and try out answers/methods. – mattb Dec 19 '21 at 17:54
  • @mattb. I attached text. – jian Dec 20 '21 at 03:38
  • @Mark - thanks, can you also add text of what it should look like afterwards? And also remove the image. Cheers – mattb Dec 20 '21 at 09:05

2 Answers2

1

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/

Vedant36
  • 318
  • 1
  • 6
0

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.

chipfall
  • 300
  • 2
  • 6
  • of course, if they are spaces and you don't care, select the columns after or before and hit r(comma,quote) to simply replace the space with appropriate punctuation. – chipfall Dec 20 '21 at 05:05