Home

Awesome

MultiCursor

MultiCursor USER MANUAL - by Daniel Thau

If you'd like to skip the whole "reading" nonsense and watch a nice, wholesome video which explains MultiCursor instead, see here.

Description

This plugin will allow Vim to utilize multiple cursors simultaneously. This can be used to do things such as refactor many lines at the same time.

Setup

MultiCursor currently requires Vim 7.3 for undotree().

MultiCursor can be installed like most other Vim plugins. On a Unixy system without a plugin manager, the multicursor.vim file should be located at:

~/.vim/plugin/multicursor.vim

On a Unixy system with pathogen, the multicursor.vim file should be located at:

~/.vim/bundle/multicursor/plugin/multicursor.vim

On a Windows system without a plugin manager, the multicursor.vim file should be located at:

%USERPROFILE%\vimfiles\plugin\multicursor.vim

On a Windows system with pathogen, the multicursor.vim file should be located at:

%USERPROFILE%\vimfiles\bundle\multicursor\plugin\multicursor.vim

If you are using a plugin manager other than pathogen, see its documentation for how to install MultiCursor - it should be comparable to other plugins.

If you would like the documentation to also be installed, include multicursor.txt into the relevant directory described above, replacing "plugin" with "doc".

There are a few ways to access MultiCursor's functionality, none of which have mappings out of the box; you will have to create your own mappings.

If you would like to manually place cursors by moving your cursor above each location and pressing a keybinding, set the keybinding like so, setting {keys} as desired:

nnoremap {keys} :<c-u>call MultiCursorPlaceCursor()<cr>

To actually utilize these manually placed cursors you will need to call another mapping:

nnoremap {keys} :<c-u>call MultiCursorManual()<cr>

If you would like to cancel manually placed cursors without utilizing them:

nnoremap {keys} :<c-u>call MultiCursorRemoveCursors()<cr>

You can also create cursors from visual mode:

xnoremap {keys} :<c-u>call MultiCursorVisual()<cr>

This will create one cursor per visually selected line. Moreover, with this mapping, you can prepend {keys} with a number. This number will tell MultiCursor to only create one cursor per that many lines. For example, running 2{keys} will create a cursor on every other line of the visually selected area.

Finally, you can create cursors by searching the buffer via regular expressions. To have MultiCursor prompt you for the search pattern:

nnoremap {keys} :<c-u>call MultiCursorSearch('')<cr>

If the argument to the function in the above mapping is a non-empty string, that will be the pattern which will be searched for. This can be utilized to do things such as create a cursor at every word like the word under the cursor:

nnoremap {keys} :<c-u>call MultiCursorSearch('<c-r><c-w>')<cr>

Or every group of characters like those visually selected:

xnoremap {keys} "*y<Esc>:call MultiCursorSearch('<c-r>=substitute(escape(@*, '\/.*$^~[]'), "\n", '\\n', "g")<cr>')<cr>

Some of the above magic was borrowed from the SearchParty plugin.

Finally, you should set a keybinding to stop using multiple cursors (and fall back to the normal single cursor) like so:

let g:multicursor_quit = "{keys}"

This will quit multicursor from normal mode (ie, if pressed in another mode such as insert or visual it will act as though there is no special mapping). This functions somewhat like mapleader, except that it is limited to what can be provided by a single getchar() (after it has been run through nr2char()). If g:multicursor_quit does not seem to work, you can fall back to ctrl-c.

In addition to creating mappings, you can override the color scheme used by MultiCursor for the cursors by setting the "MultiCursor" highlight group. See :highlight.

Usage

To utilize multiple cursors, you must first create the extra cursors. There are several methods to do so. The setup method for each is described above in multicursor-setup. They are summarized here as well.

Once the cursors are created and being used, most commands entered into Vim will be applied at each of the cursor positions. For example, "diw" will delete the word under each of the cursors. You can stop using multiple cursors by typing g:multicursor_quit or, if that is not working, fall back to ctrl-c.

Known Issues

Debug

If you would like to hack at MultiCursor, the debug mode can be turned on by placing the following in your vimrc, or running it in the cmdline in a running Vim before calling MultiCursor mappings:

let g:multicursor_debug = 1

Enabling debug results in two things. First, in the bottom line you will see something along the lines of the following:

I:"" M:"n" U:"0v0,0"

The quoted area after "I:" indicates the In-progress command; this is what MultiCursor normally outputs at the bottom without debug. This will be blank if the last key you've entered into Vim was acted upon.

The quoted area after "M:" indicates the current Mode you are in. If it is empty, this means your current in-progress command cannot be executed as it is for some reason. This could indicate operator-pending mode, but not always - it also comes up for custom mappings/plugins that aren't technically operator-pending mode.

The quoted section after "U:" is used for debugging undo history and contains three fields. The first indicates the undo level just prior to running the previous command. The second indicates the current undo level. If you have just run a command which should not create an undo point, these should should be identical. The third field indicates whether MultiCursor forced an undo for the last keystroke. This should occur when both conditions are true:

The second result of enabling debug is that the try/catch block is disabled. The try/catch block is typically used to ensure that if the user attempts to stop MultiCursor with ctrl-c, MultiCursor will also clean up. However, the try/catch block will also hide errors - if an error occurs (without debug), MultiCursor will still remove the cursors and exit cleanly. The down side to this is that it makes debugging difficult, as errors are hidden. Hence, with debugging on, the try/catch block is disabled.

Changelog

0.2 (2012-12-11):

0.1 (2012-12-11):