23

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)

  • 1
    Like .,.+2s/^../*/ (or equivalently in a visual selection)? Why two replace commands? 2c would replace two characters. – muru Feb 20 '15 at 8: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 9: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 9:38
24

Visual-block selection:

<C-v>
jjl
c*
<Esc>

:normal:

:,+2norm! cE*

Substitution:

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

Dot formula:

cE*<Esc>
j.
j.
  • 1
    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 9:40
  • 2
    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 at 16:06
  • 1
    @JordanMackie c is for "change". – romainl Mar 30 at 16:24
15

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.