Vim multi-line editing like in sublimetext?

I started using gvim and I can't figure out how multi-line editing works in gvim.

For example:

Source 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 multi-line insertion.

My intention is to insert quotes, as in the first line, and then 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 was expecting behavior similar to sublimetext one:

enter image description here

If you do not know how this works, it simply repeats the steps for each line. How can this be achieved? And what does vim do here?

+73
vim sublimetext text-editor
Aug 02 '12 at 19:24
source share
6 answers

Do yourself a favor by dropping the Windows compatibility level. The usual shortcut to enter Visual-Block mode is <Cv> .

Others deal with macro recording, here are a few other ideas:

Use only the visual block mode.

  • Place 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; 
  • Press <Cv> to enter visual block mode and expand your selection at 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; 
  • Press I"<Esc> to get:

     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; 
  • Place 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; 
  • Press <Cv> to enter visual block mode and expand your selection at 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; 
  • Press A"<Esc> to get:

     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 .

  • Place 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; 
  • Press <Cv> to enter visual block mode and expand your selection in the lower and right parts:

     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; 
  • Press s" to get:

     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 mode and :normal .

  • Press V to select the entire line and expand it at 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;] 
  • Run the following command:: :'<,'>norm ^wi"<Cv><Esc>eea"<CR> to get:

     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 in a range of lines (the '<,'> automatically added by Vim and means "act in the selected area")

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

    • w goes to the next word

    • i" insert " before the cursor

    • <Cv><Esc> is Vim's way of entering a control character in this context, here it is <Esc> used to exit insert mode

    • ee moves to the end of the next word

    • a" adds " after the cursor

    • <CR> executes the command

    Using Surround.vim, the command above becomes

     :'<,'>norm ^wvees"<CR> 
+118
Aug 03 2018-12-12T00:
source share

Here are some of the good solutions given above, but we can also try some plugins that provide multiple cursors such as Sublime.

I think this looks promising:

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

This is quite powerful, although it took me a little time to get used to the thread (which is pretty similar to Sublime, but still modal like Vim).

In my experience, if you have many other plugins installed, you might run into some conflicts!

There are other attempts to this function:

Please feel free to edit if you notice that any of them are improving.

+23
Jun 13 '14 at 10:46
source share

There are several ways to do this in Vim. I don't know which ones look like Sublime Text though.




The first of these will be through multi-line insert mode. Hover over the second “a” in the first line, press Ctrl-V , select all the lines, then press I and put in a double quote. Pressing <esc> will repeat the operation on each line.




The second is macros. Place the cursor on the first character and start recording the macro with qa . Go right with llll , enter insert mode with a , set aside the double quotation mark, insert insert mode and go back to the beginning of your line with <home> (or equivalent). Press j to move down one line. Stop recording with q . And then play the macro with @a . Repeatedly.




Does any of the above work?

+14
Aug 2 '12 at 20:12
source share

if you use the global command, you can repeat what you can do in one online, with 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 replaces all occurrences of a line in that line with baz.

 :g/.*/ 

will do on every line

+6
Aug 2 '12 at 19:33
source share

I'm not sure what vim does, but it is an interesting effect. The way you describe what you want is more like macros ( :help macro ). Something like this will do with macros (starting from normal mode):

  • qa : write a macro to register a .
  • 0w : 0 go to the beginning of the line, w go to one word.
  • i"<Esc> : Enter Paste Mode, Paste " and return to normal mode.
  • 2e : Go to the end of the second word.
  • a"<Esc> : Add " .
  • jq Move to the next line and complete macro shooting.

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

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

Here's how to achieve what you want with visual-block-mode (starting from normal mode):

  • Go where you want the first quote to be.
  • Enter visual-block-mode , select the lines you want to affect, G to go to the bottom of the file.
  • Press i"<Esc> .
  • Continue to the next place you want to insert. "
  • You want to repeat what you just did, simple enough . .
+5
Aug 02 2018-12-12T00:
source share
 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) 
+2
Sep 15 '17 at 18:04 on
source share



All Articles