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 a
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
, and
put in a doublequote. Pressing <esc>
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?
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.
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;
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;
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;
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;
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;
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.
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;
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;
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
.
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;]
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>
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)
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.
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
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:
V12j
<leader>n
<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.