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?
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.
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 .
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.
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 '<,'>
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With