Visual Block Edit in Vim

Visual block edit in vim

After pressing I in visual block mode, vim appears to have entered insert mode. But when you exit the insert mode and go to normal mode again, you will see that the keys you pressed in insert mode have taken effect in the entire selected block.

A demo is available at this link

Vim: How to insert in visual block mode?

Try this

After selecting a block of text, press Shift+i or capital I.

Lowercase i will not work.

Then type the things you want and finally to apply it to all lines, press Esc twice.






If this doesn't work...

Check if you have +visualextra enabled in your version of Vim.

You can do this by typing in :ver and scrolling through the list of features. (You might want to copy and paste it into a buffer and do incremental search because the format is odd.)

Enabling it is outside the scope of this question but I'm sure you can find it somewhere.

Vim: insert the same characters across multiple lines

  1. Move the cursor to the n in name.
  2. Enter visual block mode (Ctrlv).
  3. Press j three times (or 3j) to jump down by 3 lines; G (capital g) to jump to the last line
  4. Press I (capital i).
  5. Type in vendor_. Note: It will only update the screen in the first line - until Esc is pressed (6.), at which point all lines will be updated.
  6. Press Esc.

mini-screencast demonstrating the method

An uppercase I must be used rather than a lowercase i, because the lowercase i is interpreted as the start of a text object, which is rather useful on its own, e.g. for selecting inside a tag block (it):

mini-screencast showing the usefulness of the 'it' text object

How to edit blocks in vim without visual block mode

What's so bad about blockwise visual mode?! There's no practical alternative to it.

  • You could use :substitute with atoms like \%>v, \%<v, \%>l and \%<l to limit the pattern match to a rectangular block, but that's very tedious.
  • There are some multi-edit plugins (inspired by other editors) that allow you to select some areas, and then simultaneously edit them all.
  • For special purposes, you could write a scriptlet / mapping with getline() / setline() and String manipulation in Vimscript.

How do you select a whole column in visual block mode?

G goes to the last line, but moves the cursor to the first non-blank position if the startofline or compatible (which enables startofline) options are set. If you want to keep the same column, set nosol before going into visual block mode, and then hit G.

From the manual entry for startofline:

When "on" the commands listed below move the cursor to the first non-blank of the line. When off the cursor is kept in the same column (if possible). This applies to the commands: CTRL-D, CTRL-U, CTRL-B, CTRL-F, "G", "H", "M", "L", gg, and to the commands "d", "<<" and ">>" with a linewise operator, with "%" with a count and to buffer changing commands (CTRL-^, :bnext, :bNext, etc.).

How to insert after the cursor in Vim visual block mode

Just like i inserts before the cursor and a inserts after the cursor in normal mode, Shift+a inserts after the cursor in visual block mode.

Visual block insert in redhat vim

Solution to your updated question:

  1. Go between "hello" & "world" on first line
  2. Press Ctrl+v to enter visual block mode.
  3. Go down using 2j to select that column
  4. Press I #An uppercase I
  5. Press 4 spaces to get the desired output.
  6. Press Esc

Here's a small demo:

demo gif



Related Topics



Leave a reply



Submit