197

I started to use gvim, and I can't quite understand how the multiline edit works in gvim.

For example:

Original text:

asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;

ctrl+q, jjjjjj , $ everything is selected, then i press I to do a multiline insert.

My intention is to insert quotes like in the first line, and then to press Esc:

asd "asd asd" asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;
asd asd asd asd asd;

What happened? I expected a behavior similar to sublimetext's one:

enter image description here

If you don't know how that works, it just repeats the actions for every line. How can achieve that? And what is vim doing here?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
AlfredoVR
  • 4,069
  • 3
  • 25
  • 33

8 Answers8

306

Do yourself a favor by dropping the Windows compatibility layer.

The normal shortcut for entering Visual-Block mode is <C-v>.

Others have dealt with recording macros, here are a few other ideas:

Using only visual-block mode.

  1. Put the cursor on the second word:

    asd |a|sd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    
  2. Hit <C-v> to enter visual-block mode and expand your selection toward the bottom:

    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    asd [a]sd asd asd asd;
    
  3. Hit I"<Esc> to obtain:

    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    
  4. Put the cursor on the last char of the third word:

    asd "asd as|d| asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    asd "asd asd asd asd;
    
  5. Hit <C-v> to enter visual-block mode and expand your selection toward the bottom:

    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    asd "asd as[d] asd asd;
    
  6. Hit A"<Esc> to obtain:

    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    

With visual-block mode and Surround.vim.

  1. Put the cursor on the second word:

    asd |a|sd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    asd asd asd asd asd;
    
  2. Hit <C-v> to enter visual-block mode and expand your selection toward the bottom and the right:

    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    asd [asd asd] asd asd;
    
  3. Hit S" to surround your selection with ":

    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    

With visual-line mode and :normal.

  1. Hit V to select the whole line and expand it toward the bottom:

    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    [asd asd asd asd asd;]
    
  2. Execute this command: :'<,'>norm ^wi"<C-v><Esc>eea"<CR> to obtain:

    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    asd "asd asd" asd asd;
    
    • :norm[al] allows you to execute normal mode commands on a range of lines (the '<,'> part is added automatically by Vim and means "act on the selected area")

    • ^ puts the cursor on the first char of the line

    • w moves to the next word

    • i" inserts a " before the cursor

    • <C-v><Esc> is Vim's way to input a control character in this context, here it's <Esc> used to exit insert mode

    • ee moves to the end of the next word

    • a" appends a " after the cursor

    • <CR> executes the command

    Using Surround.vim, the command above becomes

    :'<,'>norm ^wvees"<CR>
    
stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 10
    For steps 4 and 5 I would recommend hitting `gv` to get your previous visual selection back, and *then* move horizontally, perhaps with `2e`. (The more lines, the more valuable `gv` is!) – joeytwiddle Jan 16 '15 at 23:30
  • 4
    surround.vim uses a small `s` in normal mode but a big `S` in visual mode, so in step 3 of the surround.vim method, the keystrokes should be `S"` – joeytwiddle Jan 16 '15 at 23:31
  • 1
    Point 3 of visual-block + Surround.vim should be `S"`, not `s"` – zool Aug 17 '15 at 22:04
  • 1
    Instead of visual-block mode steps 5 and 6 you can simply put cursor after the third word and press `.`. – Jonas Nov 18 '15 at 15:01
  • 2
    This is cool, but this only works in some cases where the characters you want to edit on the different lines are at the exact same horizontal position on each line, and the edited lines are right one after another. Cant we cherry-pick where to enter in edit mode on which line in Vim ? We can in Sublim... **EDIT :** Actually the second answer is what I was looking for – Alexandre Bourlier Jun 15 '16 at 14:43
  • 1
    Why do we need to use `I` instead of `i` when using only visual-block mode? http://stackoverflow.com/questions/28341241/why-lowercase-i-does-not-work-in-visual-block-mode – Shaun Luttin Sep 12 '16 at 00:31
  • `With visual-line mode and :normal. ` is complicated than what I am writing. – sfy Nov 03 '20 at 19:18
  • `Using only visual-block mode` part, step 5-6 can use `.` to repeat previous operation. – Ryan Yan Nov 09 '20 at 07:52
  • Could you explain what `` is? I assume it means Ctrl+v but this is not obvious. – Andrew Bobulsky Jan 24 '21 at 21:23
  • 2
    @AndrewBobulsky, your assumption is correct. This is the standard notation used in mappings and the de-facto standard when writing about Vim. See `:help key-notation`. – romainl Jan 25 '21 at 08:03
47

There are several ways to accomplish that in Vim. I don't know which are most similar to Sublime Text's though.


The first one would be via multiline insert mode. Put your cursor to the second "a" in the first line, press Ctrl-V, select all lines, then press I(capital i), and put in a doublequote. Pressing Escape will repeat the operation on every line.


The second one is via macros. Put the cursor on the first character, and start recording a macro with qa. Go the your right with llll, enter insert mode with a, put down a doublequote, exit insert mode, and go back to the beginning of your row with <home> (or equivalent). Press j to move down one row. Stop recording with q. And then replay the macro with @a. Several times.


Does any of the above approaches work for you?

unrealapex
  • 578
  • 9
  • 23
Rook
  • 60,248
  • 49
  • 165
  • 242
39

Those are some good out-of-the box solutions given above, but we can also try some plugins which provide multiple cursors like Sublime.

I think this one looks promising:

It seemed abandoned for a while, but has had some contributions in 2014.

It is quite powerful, although it took me a little while to get used to the flow (which is quite Sublime-like but still modal like Vim).

In my experience if you have a lot of other plugins installed, you may meet some conflicts!

There are some others attempts at this feature:

Please feel free to edit if you notice any of these undergoing improvement.

unrealapex
  • 578
  • 9
  • 23
joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
20

if you use the "global" command, you can repeat what you can do on one online an any number of lines.

:g/<search>/.<your ex command>

example:

:g/foo/.s/bar/baz/g

The above command finds all lines that have foo, and replace all occurrences of bar on that line with baz.

:g/.*/

will do on every line

srini.venigalla
  • 5,137
  • 1
  • 18
  • 29
  • 1
    This is so useful, I didn't know it. Seems so powerful and clever combination of commands. – 0xc0de Mar 12 '19 at 06:39
  • 1
    For convenience, I wrote a little function that takes the word under the cursor (or the visually selected word), prompts for a replacement, and then runs the `:g` command described here. I like to use `/gc` to ask for "confirmations" so I can hit `y` or `n` for each replacement, or `a` to change all of them. – joeytwiddle Jul 19 '19 at 04:10
17
Ctrl-v ................ start visual block selection
6j .................... go down 6 lines
I" .................... inserts " at the beginning
<Esc><Esc> ............ finishes start
2fdl. ................. second 'd' l (goes right) . (repeats insertion)
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • 3
    In step 3, `I` can not enter the insert mode, do you know why this happens? – Ninja Nov 05 '18 at 03:24
  • 1
    Try loading vim with no plugins: `vim -u NONE` and try to perform the desired action again, it could be some map or plugin changing the behavior of your vim. – SergioAraujo Nov 05 '18 at 21:36
7

I'm not sure what vim is doing, but it is an interesting effect. The way you're describing what you want sounds more like how macros work (:help macro). Something like this would do what you want with macros (starting in normal-mode):

  1. qa: Record macro to a register.
  2. 0w: 0 goto start of line, w jump one word.
  3. i"<Esc>: Enter insert-mode, insert a " and return to normal-mode.
  4. 2e: Jump to end of second word.
  5. a"<Esc>: Append a ".
  6. jq Move to next line and end macro recording.

Taken together: qa0wi"<Esc>2ea"<Esc>

Now you can execute the macro with @a, repeat last macro with @@. To apply to the rest of the file, do something like 99@a which assumes you do not have more than 99 lines, macro execution will end when it reaches end of file.

Here is how to achieve what you want with visual-block-mode (starting in normal mode):

  1. Navigate to where you want the first quote to be.
  2. Enter visual-block-mode, select the lines you want to affect, G to go to the bottom of the file.
  3. Hit I"<Esc>.
  4. Move to the next spot you want to insert a ".
  5. You want to repeat what you just did so a simple . will suffice.
Thor
  • 45,082
  • 11
  • 119
  • 130
  • 4
    No need for 99@a. If you wish to execute a macro on every line of a file try :%norm! @a. – Rook Aug 02 '12 at 23:39
  • Without the dots (they're just ends of sentences, not part of the commands) – Rook Aug 02 '12 at 23:40
  • `9999@a` may be ugly but it's much easier to type than `:%norm! @a` and it doesn't re-run on the line you just edited. Still good to know it anyway. :) – joeytwiddle Jan 16 '15 at 23:45
3

If somebody likes me found this old topics:

A great plugin : https://github.com/mg979/vim-visual-multi/

It is really close to the SublimeText multi line editing experience and works well.

user3240484
  • 209
  • 1
  • 4
0

My solution is to use these 2 mappings:

map <leader>n <Esc><Esc>0qq
map <leader>m q:'<,'>-1normal!@q<CR><Down>

How to use them:

  1. Select your lines. If I want to select the next 12 lines I just press V12j
  2. Press <leader>n
  3. Make your changes to the line
  4. Make sure you're in normal mode and then press <leader>m

To make another edit you don't need to make the selection again. Just press <leader>n, make your edit and press <leader>m to apply.


How this works:

  • <Esc><Esc>0qq Exit the visual selection, go to the beginning of the line and start recording a macro.

  • q Stop recording the macro.

  • :'<,'>-1normal!@q<CR> From the start of the visual selection to the line before the end, play the macro on each line.

  • <Down> Go back down to the last line.


You can also just map the same key but for different modes:

vmap <leader>m <Esc><Esc>0qq
nmap <leader>m q:'<,'>-1normal!@q<CR><Down>

Although this messes up your ability to make another edit. You'll have to re-select your lines.