Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to exchange beginning/end of region #4697

Closed
1 task done
failable opened this issue Jun 15, 2023 · 5 comments · Fixed by #23428
Closed
1 task done

Add command to exchange beginning/end of region #4697

failable opened this issue Jun 15, 2023 · 5 comments · Fixed by #23428
Labels
editor Feedback for code editing, formatting, editor iterations, etc enhancement [core label] keymap / key binding Feedback for keyboard shortcuts, key mapping, etc

Comments

@failable
Copy link

failable commented Jun 15, 2023

Check for existing issues

  • 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-.

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')
Screen.Recording.2023-07-31.at.14.04.57.mov
@JosephTLyons JosephTLyons added editor Feedback for code editing, formatting, editor iterations, etc keymap / key binding Feedback for keyboard shortcuts, key mapping, etc and removed triage labels Jun 18, 2023
@JosephTLyons
Copy link
Collaborator

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

@JosephTLyons JosephTLyons transferred this issue from zed-industries/community Jan 24, 2024
@quarkw
Copy link

quarkw commented Apr 12, 2024

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

@failable
Copy link
Author

@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"
    }
}

@quarkw
Copy link

quarkw commented Apr 12, 2024

@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.

@notpeter
Copy link
Member

We've recently implemented support for an emacs-style mark-mode (editor::SetMark) distinct of vim mode:

This included support for exchanging the mark and the cursor location (editor::ExchangeMark). Emacs keybind maps these by default to ctrl-space and ctrl-x ctrl-x respectively.

I was unaware of this enhancement request when that was implemented, but it sounds like you would like to have something similar but for normal selections not just mark/selection mode selections so I've implemented that here:

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor Feedback for code editing, formatting, editor iterations, etc enhancement [core label] keymap / key binding Feedback for keyboard shortcuts, key mapping, etc
Projects
None yet
4 participants