1

For example, this is your starting code:

.pt-1 {padding-top: 1px}
.pt-2 {padding-top: 2px}
.pt-3 {padding-top: 3px}
.pt-4 {padding-top: 4px}
.pt-5 {padding-top: 5px}

You want to change every number from n to n0 (eg., 1px -> 10px). The result should be like this:

.pt-1 {padding-top: 10px}
.pt-2 {padding-top: 20px}
.pt-3 {padding-top: 30px}
.pt-4 {padding-top: 40px}
.pt-5 {padding-top: 50px}

I'm used to VS Code where normally you can just place multiple cursors and type everything all at the same time using ctrl+alt and Down Arrow.

How would you do this in Vim?

NOTE: This is NOT this question: Vim: insert the same characters across multiple lines (but I wish it was...)

I'm asking how to do this in the same location in multiple lines, that question is saying for the beginning of the lines.

Zack Plauché
  • 3,307
  • 4
  • 18
  • 34

3 Answers3

4

With visual-block mode

  1. Move the cursor to 1px.

  2. Enter visual-block mode:

    <C-v>
    

    See :help visual-mode.

  3. Extend the block to 5px:

    <Down><Down><Down><Down>
    

    or:

    jjjj
    

    or:

    /5p<CR>
    

    or whatever feels intuitive to you.

    See :help motion.txt for some inspiration.

  4. Append a zero:

    A0<Esc>
    

    See :help v_b_A.

With a substitution

  1. Visually select the lines.

    You can use any of the three visual modes, it doesn't matter.

  2. Run a substitution on the selection:

    :'<,'>s/px/0&<CR>
    

    Breakdown:

    • '<,'> is the range on which the command that follows will operate. This one represents the visual selection, it is automatically inserted for you when you press : while in visual mode. See :help :range.
    • s/pattern/replacement is the substitution command. See :help :s.
    • px is our pattern.
    • 0& is the replacement, where & represents the whole match. Here, we replace px with 0px. See :help s/\e.

With search and the dot command

  1. Jump to the first px:

    /px<CR>
    
  2. Change the match to 0px:

    cgn0px<Esc>
    
  3. Jump to next match:

    n
    
  4. Repeat the change:

    .
    
  5. Repeat steps 3-4 as many times as needed.

See :help gn and :help ..

romainl
  • 186,200
  • 21
  • 280
  • 313
1

Use the substitute command. You can anchor it on the px:

:%s/px/0px/
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

Here's how I mapped Ctrl+Alt+j to add cursor below and Ctrl+Alt+k to add cursor above in normal mode. (I assume you are using the VSCodeVim extension)

  1. Open Command Palette and run "Preferences: Open Keyboard Shortcuts (JSON)" to open keybindings.json.
  2. Add the following objects at the end of the array:
  {
    "key": "ctrl+alt+j",
    "command": "editor.action.insertCursorBelow",
    "when": "vim.active && editorTextFocus"
  },
  {
    "key": "ctrl+alt+k",
    "command": "editor.action.insertCursorAbove",
    "when": "vim.active && editorTextFocus"
  }
  1. Open Keyboard Shortcuts with Ctrl+K Ctrl+S. Search for ctrl + alt + j and ctrl + alt + k and delete any keybindings that conflict with Ctrl+Alt+j or Ctrl+Alt+k.

Results:

Results

AnsonH
  • 2,460
  • 2
  • 15
  • 29