Replace text when in visual-block mode

17,523

Solution 1

Why don't you select 4 columns and hit c?

<C-v>
2j
3l
cDone
<Esc>

Also, you could use :h normal:

V
2j
:'<,'>norm 7lRDone
<Esc>

Solution 2

If you want to substitute within a visual block, you can use the vis.vim plugin.

http://vim.wikia.com/wiki/Applying_substitutes_to_a_visual_block

To install:

Download vis.vba.gz from http://www.drchip.org/astronaut/vim/index.html#VIS

# Open vis.vba.gz in Vim 7.1 or later.
vim vis.vba.gz

# Source the open file.
:so %

# Quit Vim
:q

Once installed:

ctrl-v to enter visual-block mode
Move to select the block you want
:B s/pattern/newtext/

Even better, you can perform any Ex command on the visual block, not just substitutions. For example :B !sort.

Share:
17,523

Related videos on Youtube

Thor
Author by

Thor

Updated on September 18, 2022

Comments

  • Thor
    Thor almost 2 years

    Maybe I'm searching the wrong way, but I've failed to find out how to do this.

    Here's the scenario: I have a list in a text file where I want to insert the same word on multiple lines, here's an example text:

    12.1           Item1
    12.2           Item2
    12.3           Item3
    

    Now I visual-block mark the 8th column, indicated with underscores below, and intend to replace part of the whitespace with Done without moving the item column:

    12.1   _       Item1
    12.2   _       Item2
    12.3   _       Item3
    

    I expected that I could go into replace mode, but I can only enter insert (I), append (A), or change-mode (c). The change-mode comes close to what I want: if I know the length of the word the block can be set to the same width.

    Is there something I'm missing here?

  • Thor
    Thor almost 12 years
    That works for Done, but what if I want to write something else, I want to be free from counting characters, if possible.
  • romainl
    romainl almost 12 years
    :norm is your friend, then.
  • Thor
    Thor almost 12 years
    Yes the second solution can be used for a convenient mapping. Thank you.
  • Thor
    Thor over 8 years
    Nice plugin, I will be using it :-). But how can I use it to replace arbitrary columns?
  • Christian Long
    Christian Long over 8 years
    Right, you still have the problem of quickly setting the visual block width to match your arbitrary-width columns. I usually do <count> w or e to select several columns quickly, then adjust with h or l.