2

I have text like this:

    w ky,
    wyz,
    wyy,
    wj,
    w w,

and I want to change it to this:

    "w ky",
    "wyz",
    "wyy",
    "wj",
    "w w",

I do it now using:

  1. record a macro to insert double quota in one line, then go to next line qa0wi"$i"j
  2. then just type 4@a

Yes, it works, but is there an easier way to do this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
roger
  • 9,063
  • 20
  • 72
  • 119
  • The first result when typing your question into google https://stackoverflow.com/questions/11784408/vim-multiline-editing-like-in-sublimetext – J. Knight May 25 '17 at 04:24
  • @J.Knight but that answer just work when every line is the same length – roger May 25 '17 at 05:07
  • instead of `0w`, you can use `^`... not sure what you mean by easy.. but you can also use substitution... `s/\S[^,]*/"&"/` – Sundeep May 25 '17 at 05:22
  • @Sundeep I know `ggCtrl-vG$` will select all word, but can I select every line just until last comma? – roger May 25 '17 at 05:42
  • Just to add to @Sundeep's comment. `:%s/\S[^,]*/"&"/g` will replace it all at once – sudo bangbang May 25 '17 at 05:42
  • @sudobangbang yeah.. and `5V:s/\S[^,]*/"&"/` for selective lines only or any other address range as required.. – Sundeep May 25 '17 at 05:45
  • @roger your given sample has only single `,` so the regex solution should work same as your macro – Sundeep May 25 '17 at 05:47

3 Answers3

0

There is an easier way. You can use the norm command. I would recommend this:

  1. Visually select all of the lines

  2. Type

    :norm I"<C-v><esc>$i"<cr>
    

    When you actually type this (before hitting enter), the text that should be shown in your command line is:

    :'<,'>norm I"^[$i"
    

The norm command tells vim to simulate a set of normal mode keystrokes on certain lines. In this case, the command is:

:'<,'>      " On every line in the visual selection:
norm        " Do the following as if typed in normal mode:
I"<esc>$i"  " Insert an '"', escape, then insert a '"' at the end (before the comma)

You can also do this without using a visual selection, by typing <n>:norm ..., and the command will apply the current n lines. (the current line and the next n-1 lines)

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
0

Doing a "very magic search \v" we can search...

\v(\w ?\w+),
\v ........... starts very magic search mode
(  ........... starts group one
\w ........... any word
 ? ........... optional space
+  ........... one or more
)  ........... ends group one

After testing the search we can pass into our command or use the last search by placing two slashes on the search pattern

%s/\v(\w ?\w+),/"\1",/g
%s//"\1",/g

everything matched on group one can be referred by \1.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
0

Do a basic multi-line edit at the front then do a string replace on , with ",.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
J. Knight
  • 131
  • 9