Vim: How to insert in visual block mode?
Asked Answered
N

5

233

How can you insert when you are in visual block mode (by pressing ctrl-V) in Vim?

Noshow answered 13/9, 2012 at 4:49 Comment(1)
possible duplicate of VIM Blockwise InsertBogle
N
351

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.

Noshow answered 13/9, 2012 at 4:49 Comment(19)
You also have to press escape to exit insert mode before the text will be duplicated.Rask
Has anyone ever seen this not work? I have a non-modified vim setup on CentOS. I press ctrl+v, press j a few times, and I see the cursor highlight the first character of a few lines. I press shift+i. The cursor is moved to the first character of the first line, but I'm back to normal mode. I know this because if I press "a", it moves the character to the right, and it says "-- INSERT --". If I type a few characters and press escape, it's not applied to the other lines.Padus
@TylerCollier I'm experiencing the problem on Ubuntu 14.04 on digitalocean. Please comment if you've found a solution.Orpiment
@ZacharyBurt Sorry, no solution.Padus
@TylerCollier I'm having the same problem on Ubuntu 12.04.5. For my particular use case I was able to use "c" after opening the visual block per Nishant's answer below.Staffan
@Staffan I've never had this issue on Ubuntu. Unfortunately, I can't try your answer on the CentOS box I originally mentioned. I don't have access to any CentOS machines any more.Padus
@ZacharyBurt The default vim installed with Ubuntu 14.04 (installed as vi) does not include the visualextra feature, so block insert won't work there. sudo apt-get install vim brings in a more full-featured version, on which visualextra is activated. @TylerCollier This may be the same issue on your CentOS install too: check the output of vim --version to see the full list of activated features.Clingy
@GabrielGrant Unfortunately, it says +visualextra for me. Is there a dotfile configuration I have to make to activate it?Orpiment
@ZacharyBurt hmm, strange. Did you already manually install vim? Once I did, visual block insert worked immediately without any dotfile change.Clingy
@Honghe.Wu because i and a are used to extends the selection to text objects, read my answer to this question or type :help visual-operators.Cystitis
@Zachary Burt,Just as Broandon Joyce sayed, Maybe you fogot to press the ESC after you insert chars to the first selected row, this action take effects. see this link #1677132Biting
... and also don't forget r (great for ascii art)Blew
@TylerCollier I also encountered this question, and I post a question here and get the answer, check the https://mcmap.net/q/119750/-visual-block-edit-in-vim/6521116Anglocatholic
If any poor souls like me are wondering why this concept doesn't work when you want to just delete stuff, well that's because you can just block select and hit the delete key. Or else see Nishan's answer and use the 'c' key which deletes and puts you in insert mode, then you can either add in new text or just hit escape and you're doneMetternich
I'm on a mac and this did not work. What did work for me was this answer: https://mcmap.net/q/117249/-vim-how-to-insert-in-visual-block-mode . In summary, press escape twice after you edit.Zennie
Double escape does not propagate changes to other lines, esc double j does.Liquidation
What is the reason for hitting escape twice instead of once? Seems to be working for me with just once.Camass
@TylerCollier you have to press <esc> twice for the changes to be applied to all the linesBathurst
Link to exact time in vim visual editing video tutorial showing this: youtu.be/PHskKxkM-nA?t=76Illassorted
N
77
  1. press ctrl and v // start select
  2. press shift and i // then type in any text
  3. press esc esc // press esc twice
Newkirk answered 28/3, 2017 at 17:42 Comment(4)
After pressing ctrl + v, you can press j to quickly select line(s) below the current position, or press k to quickly select line(s) above the current position. You can also use the shift key with the up or down errors to do the selection.Sym
Of all the answers, this is the only one that worked for me. I'm on a mac using the brew installZennie
I've been complaining for years that I couldn't easily insert (like change) in block select. So it's with a capital i, I'm amazed. I've been using :normal iINPUT<CR> to input at the beginning of lines. Thanks a bunch for this!Inclose
I don't think you need to press esc twice. If you press anything after esc, it would automatically update the text. If not, it will do it in like half a second or so.Mechanize
T
39

You might also have a use case where you want to delete a block of text and replace it .

Like this

Hello World
Hello World

You can visual block select before "W" and hit Shift+i - Type "Cool" - Hit ESC and then delete "World" by visual block selection .

Alternatively, the cooler way to do it is to just visual block select "World" in both lines. Type c for change. Now you are in the insert mode. Insert the stuff you want and hit ESC. Both gets reflected with lesser keystrokes.

Hello Cool 
Hello Cool
Taite answered 3/7, 2014 at 9:38 Comment(3)
the final step is helpful for me, you must hit ESC then all the marked line will show the char which you type in.Sapotaceous
great stuff, it helps a lotAltamira
Good example. Any idea how to delete columns? I'm trying to remove some inconsistent indentation but I'm not convinced it can be done to remove columns, only add.Aquarium
D
25

if you want to add new text before or after the selected colum:

  • press ctrl+v
  • select columns
  • press shift+i
  • write your text
  • press esc
  • press "jj"
Dianetics answered 26/10, 2017 at 8:11 Comment(6)
Tank you, jj to propagate the changes to all lines below was the last bit I was missing!Formalism
jj is a killer!Hudnall
Could someone please explain jj? I could not manage to find anything about it and seems unnecessary to me. 🤔Vacuva
jj is useful to propagate your change to all other lines...if in your case it is not needed, maybe it could be because something has been changed in newer versions of VIMDianetics
Yeah my version didn't need it either but good to know just in case.Aquarium
My version does not need jj either. You can just write and it will appear in all lines. Makes also sense that this was changed since that should be the normal use case for using Visual Block mode.Birchard
O
7

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 03 2023 07:45:49)

CTRL + q  - ENTER VISUAL MODE
j or k    - SELECT LINES BELOW OR ABOVE
SHIFT + i - ADD CONTENT TO THE FIRST LINE
ESC       - EXIT INSERT MODE
j         - PROPAGATE CHANGES TO THE OTHER LINES
Ovolo answered 15/4, 2023 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.