terryma / vim-multiple-cursors

True Sublime Text style multiple selections for Vim
MIT License
7.95k stars 251 forks source link

Align multiple selections #168

Closed rehno-lindeque closed 8 years ago

rehno-lindeque commented 9 years ago

It would be nice and simple/flexible to align multiple selections using this plugin directly instead of relying on other packages. E.g.

Before:

abcdef [zz]z ghij
klm [zz] opq
rstuvw
xy [zz]

After:

abcdef [zz]z ghij
klm    [zz] opq
rstuvw
xy     [zz]

What do you think?

rodfersou commented 8 years ago

+1 on this feature, currently I always need to:

rehno-lindeque commented 8 years ago

Sorry, I meant to make this more clear: the [zz] is intended to indicate that the string "zz" is visually selected by multiple cursors. That way you can just select what you want, ctrl+n a couple of times and hit align!

EDIT: then again, I guess that was clear after all

faceleg commented 8 years ago

I'm not sure alignment is something this plugin should be concerned with. Is your workflow possible with some combination of plugins?

hallzy commented 8 years ago

The default way that the Tabular plugin works, would probably work reasonably well... I tried it with your above example, and the result is the same except with a space added between the highlighted zz and the other z. You may be able to tinker with a Tabular mapping so that it does not add the space.

rehno-lindeque commented 8 years ago

I'll attempt another motivating example:

foo = [ "heading -- subheading" -- I suspect that interactively selecting
       , "--------------------" -- and then aligning these comments
       , "text body..." -- might be significantly easier
       , "-- author" -- with multiple cursors
       ]

That is, :s/../.. is pretty powerful, but multiple cursors is still very convenient none-the-less.

balta2ar commented 8 years ago

That's very degenerate case you've provided. I really doubt there are many users who experience such problem on a daily basis. I agree with @faceleg, this does not look like a job for this plugin. I can think of an extension to this plugin, where, for example, vim-multiple-cursors selects N cursors and provides an API that exposes information about cursor positions, geometry, and some other plugin uses that to perform an alignment. But that's another story.

hallzy commented 8 years ago

I agree that I think it would a nice thing to be able to do this sort of thing with multiple cursors because I think it would be more intuitive to line up the cursors. But if that is not going to be done here, I think the API that @balta2ar mentioned above would be a good idea.

That being said, the plugin vim-easy-align does a good job of making it easy to align the comments in your above example... For example, by entering the easyalign interactive mode by entering :EasyAlign<cr>, then press -, which tells EasyAlign to only worry about the last match, then enter regex mode by pressing <C-X>, and type your --, and then press enter... This aligns the comments.

Below is a series of key presses to accomplish this with the plugin mentioned above, starting in visual mode with your text selected:

:EasyAlign<cr>-<C-X>--<cr>