Multiple selections in VIM

VimSelection

Vim Problem Overview


Is it possible to select multiple non-consecutive lines (or sections) in VIM's visual mode? If so, how?

Vim Solutions


Solution 1 - Vim

No, this is not possible without plugins.

But you can copy multiple lines into the same buffer, if that solves your problem.

  • To start the 'Accumulation Buffer':
  • mark a section to copy in visual mode,
  • press "a to operate on the buffer a with the next command and
  • yank it as usual (y).
  • To add to that buffer:
  • mark the next section and
  • press "A (capitalizing the buffer name means "do not overwrite the buffer, append to it instead")
  • and yank again using y.
  • You can then paste the accumulated buffer a at any time using "ap.

Solution 2 - Vim

You have to install the multiselect plugin to get this capability. Find it here: http://www.vim.org/scripts/script.php?script_id=953

Solution 3 - Vim

A more up-to-date answer is this plugin.

(disclaimer: I personally don't actually use it, it interferes too much with the rest of my vim setup. If your vim is relatively clean and you are moving over from sublime, this may certainly be your cup of tea.)

I would also like to point out the record/replay functionality of vim (the q key). Quite often recording is also unnecessary, I can do the tasks normally done with sublime's multi-select by doing it iteratively (e.g. search for something, perform the fix on the first instance of it, and then subsequent repeats are achieved by hitting n and N to move around and . to repeat the edit operation).

I do have my , comma key nnoremap'd to @q, this repeats the sequence that is recorded by pressing qq (record into q register).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcinView Question on Stackoverflow
Solution 1 - VimsoulmergeView Answer on Stackoverflow
Solution 2 - VimMichael DillonView Answer on Stackoverflow
Solution 3 - VimSteven LuView Answer on Stackoverflow