39

I have a list of items where I want to replace the first two characters:

a) item 1
b) item 2
c) item 3

What is the fastest way to make this:

* item 1
* item 2
* item 3

I have an approach with visual mode (Ctr-v, jjj, but need to do two replace commands. Iirc, this replacement can be done with 1 command)

poseid
  • 493
  • 1
  • 4
  • 6
  • 1
    Like .,.+2s/^../*/ (or equivalently in a visual selection)? Why two replace commands? 2c would replace two characters. – muru Feb 20 '15 at 08:40
  • sometimes only the first line in the visual selection is changed, repeating then with . for the other lines messes things up. – poseid Feb 20 '15 at 09:10
  • 2
    @poseid are you in the habit of pressing Ctrl-C instead of Esc? This prevents, among other things, changes being applied to each line of a Visual-block selection. – tommcdo Feb 20 '15 at 10:48
  • that could be the main reason for confusion! thanks! – poseid Feb 21 '15 at 09:38

2 Answers2

44

Visual-block selection:

<C-v>
jjl
c*
<Esc>

:normal:

:,+2norm! cE*

Substitution:

:,+2s/^.\{2}/*

Dot formula:

cE*<Esc>
j.
j.
B Layer
  • 19,834
  • 2
  • 30
  • 57
romainl
  • 40,486
  • 5
  • 85
  • 117
  • 2
    Crazy approach: using textobj-word-column.vim you can do ciC*<esc> and be done. Note: this plugin is a "do what I mean" plugin so it fails in unexpected places and ways, but the plugin is still useful in some easy cases like this. – Peter Rincker Feb 20 '15 at 23:55
  • interesting the normal and dot formula! – poseid Feb 21 '15 at 09:40
  • 3
    Note that for the Visual-block solution, you will only see text being typed on the first line. Once you hit <Esc>, it will fill in the remaining lines. – tommcdo Feb 27 '18 at 12:04
  • Any suggestions for a way to remember that c will replace? Like how ctrl+D(own) – Jordan Mackie Mar 28 '19 at 16:06
  • 5
    @JordanMackie c is for "change". – romainl Mar 30 '19 at 16:24
  • For guys like me, this <C-v> is in fact <Ctrl+v> and jjl is selecting 2x down 1x right, which can be done with arrows. As commented c is for change which will replace selected rows with provided chars * (or any). <esc> will apply the change after small sleep time. – Vladimir Vukanac Jan 17 '22 at 22:15
26

Here is more efficient way of doing this:

  1. Having cursor on the first item and 1st letter (0), enter 'blockwise' visual mode Ctrl+v and select the 1st column of your current paragraph by } (if your list is finished by new line), otherwise use different methods like arrow keys (to move around the selection), or pattern search /c) where c is your last element).
  2. Move to the last symbol of your list by k and l (or using arrow keys), so two columns are selected which you want to change.
  3. Change the selected text by pressing c and type your bullet symbol (*) and finish by hitting Esc.

Works more efficiently especially in the longer lists.

kenorb
  • 18,433
  • 18
  • 72
  • 134