Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM Blockwise Insert

Tags:

vim

ruby

I would like to insert a hash at the beginning of a selected block of text in VIM (ruby comment). I selected the lines in Visual Mode, but how do I perform the same operation to all lines?

like image 248
tesserakt Avatar asked May 20 '10 14:05

tesserakt


People also ask

How do I enter insert mode in Vim?

To go into INSERT mode from COMMAND mode, you type i . To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.

How do you enter a visual block in Vim?

On bottom left of your Vim window, you will see either -- VISUAL -- , -- VISUAL LINE -- , or -- VISUAL BLOCK -- to indicate which visual mode you are in. While you are inside a visual mode, you can switch to another visual mode by pressing either v , V , or Ctrl-v .


2 Answers

You better use this.

COMMAND MODE with set number to see lines

:10,50s/^/#/g

First number before comma is the start line and second number after comma is the end line. Both are included.

like image 104
Dez Avatar answered Sep 19 '22 16:09

Dez


You have two primary options:

  • Select in block visual mode (ctrl-v), then use I to insert the same thing along the left side of the entire block. Similarly A appends; see blockwise operators.

  • Select the lines in normal visual (v) or visual line (V) mode, then run the same command on all of them, for example s/^/# / or normal I#. Typing : while you have a visual selection automatically uses the visual selection as the line range (denoted by '<,'>).

like image 30
Cascabel Avatar answered Sep 18 '22 16:09

Cascabel