zed icon indicating copy to clipboard operation
zed copied to clipboard

Add command to exchange beginning/end of region

Open failable opened this issue 1 year ago • 4 comments

Check for existing issues

  • [X] Completed

Describe the feature

For example, * denotes beginning of region, | denotes end of region (cursor in this case), running the command would turn

111
*222
333|
444

into

111
|222
333*
444

Running the command again would go back to the original state.

  • This could be helpful for adjusting the (direction of) selections. (For example if one also want to select the 111 line without canceling and selecting again).

  • Also, if no region is selected, calling the command will select / activate the region between current location and last position.

A related command in Emacs is call exchange-mark-and-point.

C-x C-x is useful when you are satisfied with the position of point but want to move the other end of the region (where the mark is). Using C-x C-x a second time, if necessary, puts the mark at the new position with point back at its original position. Normally, if the mark is inactive, this command first reactivates the mark wherever it was last set, to ensure that the region is left highlighted. However, if you call it with a prefix argument, it leaves the mark inactive and the region unhighlighted; you can use this to jump to the mark in a manner similar to C-u C-<SPC>.

If applicable, add mockups / screenshots to help present your vision of the feature

Notice how:

  1. Firstly select two lines manually
  2. Exchange beginning / end of region (In Emacs terminology they are call 'point' and 'mark')
  3. Select one more line up
  4. Cancel everything
  5. Reactivate the region (between 'point' and 'mark')

https://github.com/zed-industries/community/assets/5475262/deeae9fb-b7cf-4b7b-9f4f-02deb7588f47

failable avatar Jun 15 '23 06:06 failable

Interesting feature request. I've never used something like this. Seems pretty cool.

JosephTLyons avatar Jun 18 '23 23:06 JosephTLyons

This is already possible by utilizing the vim::OtherEnd action.

If you don't use vim mode, you'll have to bind it to some hotkey. It works even if the selection wasn't made using vim mode. If you do use vim mode the default keybinding for it is o while in visual mode

quarkw avatar Apr 12 '24 09:04 quarkw

@quarkw Does it work for the preview version? I just tried but it did not work without vim mode (it did work in vim mode)

  {
    "context": "Editor",
    "bindings": {
        "ctrl-x ctrl-x": "vim::OtherEnd"
// Or:  "ctrl-c ctrl-b": "vim::OtherEnd"
    }
}

failable avatar Apr 12 '24 09:04 failable

@failable

I'm on the preview version, but ah sorry, it looks like when vim mode is not enabled (settings config), none of the vim commands are available. I tested it in vim mode by selecting with the mouse and it worked. I didn't try disabling vim entirely.

With vim mode disabled in the settings vim::OtherEnd no longer appears in the command palette and can't be called.

So it's not supported for those that don't have vim enabled, but the good news is the code to perform that action should already exist. It just needs to be exposed in a way that it can be called with vim mode disabled.

quarkw avatar Apr 12 '24 14:04 quarkw