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:
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?
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:
Hit
<C-v>
to enter visual-block mode and expand your selection toward the bottom:Hit
I"<Esc>
to obtain:Put the cursor on the last char of the third word:
Hit
<C-v>
to enter visual-block mode and expand your selection toward the bottom:Hit
A"<Esc>
to obtain:With visual-block mode and Surround.vim.
Put the cursor on the second word:
Hit
<C-v>
to enter visual-block mode and expand your selection toward the bottom and the right:Hit
S"
to surround your selection with ":With visual-line mode and
:normal
.Hit
V
to select the whole line and expand it toward the bottom:Execute this command:
:'<,'>norm ^wi"<C-v><Esc>eea"<CR>
to obtain::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 linew
moves to the next wordi"
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 modeee
moves to the end of the next worda"
appends a"
after the cursor<CR>
executes the commandUsing Surround.vim, the command above becomes
if you use the "global" command, you can repeat what you can do on one online an any number of lines.
example:
The above command finds all lines that have foo, and replace all occurrences of bar on that line with baz.
will do on every line
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 pressI
, 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 withllll
, enter insert mode witha
, put down a doublequote, exit insert mode, and go back to the beginning of your row with<home>
(or equivalent). Pressj
to move down one row. Stop recording withq
. And then replay the macro with@a
. Several times.Does any of the above approaches work for you?
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.
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):qa
: Record macro toa
register.0w
:0
goto start of line,w
jump one word.i"<Esc>
: Enter insert-mode, insert a"
and return to normal-mode.2e
: Jump to end of second word.a"<Esc>
: Append a"
.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 like99@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):visual-block-mode
, select the lines you want to affect,G
to go to the bottom of the file.I"<Esc>
."
..
will suffice.