Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Support vim-exchange plugin

Open davidnorthetal opened this issue 7 years ago • 10 comments

Is your feature request related to a problem? Please describe. vim-exchange solves the problem of swapping two regions of text and having it be undone with a single undo https://github.com/tommcdo/vim-exchange

Describe the solution you'd like Support vim-exchange Mappings https://github.com/tommcdo/vim-exchange#mappings

Additional context http://vimcasts.org/episodes/swapping-two-regions-of-text-with-exchange-vim/

Thanks for consideration

davidnorthetal avatar Aug 05 '18 08:08 davidnorthetal

Big 👍 from me on this one. I use vim-exchange every day. The binding I use most frequently is cx<movement>. If I want to exchange two words I just move the cursor to the first word, hit cxiw, and then move the cursor to the second word, and hit . (repeat). The words now change places and can be undone with one u. Depending on the situation I'll use multiple movements, but in most circumstances repeating with . is sufficient.

knpwrs avatar Nov 25 '18 01:11 knpwrs

I have a proof of concept of this working. It has cool properties:

  • single undo
  • works with dot, so can do "cxiw move cursor ." to exchange two words

Questions @ users:

  • do you prefer it to work with "cx"?
  • or is overwriting the current "gx" an option?

Two questions @J-Fields :

  • I need "keyspace" for this operation.
  • I can provide this "internally" under "gz" and users would need to "nnoremap cx gz". Ok?
  • How would one do the linewise thing, that cxx works? There has to be some magic that "cc", "dd" etc. work. I could not find that in the code.

The thing is: I cant write an operator with keys = [c x].

sql-koala avatar Jul 26 '20 10:07 sql-koala

I would definitely prefer cx if that's at all possible.

knpwrs avatar Jul 26 '20 11:07 knpwrs

It will be possible. You would just need to create two mappings: nnoremap cx gz, nnoremap cxx gzgz

I am wondering, how does the plugin do the following: Lets say I have marked my text (first call to cx) ... can I correct or reset that?

My solution could not do that right now... I would have to "overload" undo for that, raar :>

sql-koala avatar Jul 26 '20 14:07 sql-koala

After PR #4995 gets merged it will be possible to make that operator with a key like <Plug>(Exchange) with a default remap of cx -> <Plug>(Exchange)

berknam avatar Jul 26 '20 16:07 berknam

@berknam , how did you find this? :> Can you help me with my question? There must be some way, that "dd" or "cc" get converted to a call of Delete/Change operator with parameters ( start, end ) being the current line. I cant find how it's done.

Other than that, I think I have this working. edit: "solved", works out of the box with my new operator.

sql-koala avatar Jul 26 '20 20:07 sql-koala

That is done through the doesRepeatedOperatorApply and runRepeat from BaseOperator. Then on executeOperator there is a check that runs the repeat version when you have more than one operator and they are the same.

berknam avatar Jul 26 '20 22:07 berknam

@berknam Thanks, I did not look that up yet, but I will. I looked at these Plug-mappings. Cool stuff, easy to adept.

some design decisions (open for feedback):

  1. if selected ranges overlap, plugin does no change
  2. decided against overloading undo (would be possible), but a plugin should not do this (imo)
  3. but if you messed up your selection (first part for exchange), see 1) you can just press ".", which does a "no change" and resets the state
  4. decided not to move the cursor
  5. no multicursor support for now

Here is the branch: https://github.com/sql-koala/Vim/tree/FeatureExchangePlugin (plugin/vimExchange.ts)

sql-koala avatar Jul 27 '20 18:07 sql-koala

@sql-koala are there any updates on this. I'm interested and I could help with testing/development if you need help.

nilehmann avatar Feb 06 '21 16:02 nilehmann

Hi guys. Any news on this?

thanhnguyen2187 avatar Jul 02 '22 13:07 thanhnguyen2187