matze / vim-move

Plugin to move lines and selections up and down
http://www.vim.org/scripts/script.php?script_id=4687
MIT License
1.21k stars 56 forks source link

Fixed macOS alt-keys not working (see issue #49) #71

Closed dzfrias closed 2 years ago

dzfrias commented 2 years ago

Issue

vim-move would not work on macOS due to the operating system's incompatibility with the A key mapping modifier.

Fix

The plugin detects if the user's system is macOS and maps the keys accordingly.

matze commented 2 years ago

I do not have a Mac to test, so maybe @alex-popov-tech could have a look and close #49 if fixed?

alex-popov-tech commented 2 years ago

@matze done, thank you

BStephenBB commented 2 years ago

I'm on a Mac (Monterey, version 12.4) and and this plugin has been working perfectly for years, but it seems to have stopped working after this PR merged. When I comment out all the additions from this PR it goes back to working again.

dzfrias commented 2 years ago

Hmm that's weird. Are you using iTerm? Maybe it's a terminal emulator problem. If so we can use the $TERM_PROGRAM environment variable to check if the user is on iTerm. I wish there was a better way to check for this.

BStephenBB commented 2 years ago

Hmm that's weird. Are you using iTerm? Maybe it's a terminal emulator problem. If so we can use the $TERM_PROGRAM environment variable to check if the user is on iTerm. I wish there was a better way to check for this.

I am and it does seem to be an iTerm specific problem. Just tested it with the default terminal app, as well as kitty and alacrity and the plugin works as is (without me commenting out the diff from this PR).

dzfrias commented 2 years ago

I'm using iTerm as well, so this is definitely strange. I'm on the same macOS version as you as well. I'll do some more research, but I'm really not sure what we can do to fix this because I don't know what the problem is.

iguntur commented 2 years ago

same thing here, I'm using Big Sur and iTerm and suddenly it doesn't work properly after these PRs are merged too.

I also tried it with kitty and alacritty and it worked fine.

BStephenBB commented 2 years ago

Here is my iTerm profile exported as JSON w/ all the color related stuff removed (made this way too long):

Notably there seems to be a "Option Key Sends" : 2, setting which looks like it might be related. I don't recall ever setting this, so I think it's likely the default? @dzfrias and @iguntur what do your profiles have "Option Key Sends" set to?

{
  "Use Non-ASCII Font" : false,
  "Tags" : [

  ],
  "Rows" : 25,
  "Default Bookmark" : "No",
  "Blend" : 0.050000000000000003,
  "Non-ASCII Anti Aliased" : true,
  "Use Bright Bold" : true,
  "Ambiguous Double Width" : false,
  "Jobs to Ignore" : [
    "rlogin",
    "ssh",
    "slogin",
    "telnet"
  ],
  "Working Directory" : "\/Users\/redacted",
  "Blinking Cursor" : false,
  "Disable Window Resizing" : true,
  "Sync Title" : false,
  "Prompt Before Closing 2" : false,
  "BM Growl" : true,
  "Command" : "",
  "Description" : "Default",
  "Mouse Reporting" : true,
  "Screen" : -1,
  "Columns" : 80,
  "Idle Code" : 0,
  "Custom Command" : "No",
  "ASCII Anti Aliased" : true,
  "Application Keypad Allowed" : false,
  "Non Ascii Font" : "Monaco 12",
  "Vertical Spacing" : 1,
  "Use Bold Font" : true,
  "Option Key Sends" : 2,
  "Character Encoding" : 4,
  "Use Italic Font" : true,
  "Unlimited Scrollback" : false,
  "Keyboard Map" : {
    "0xf700-0x260000" : {
      "Action" : 10,
      "Text" : "[1;6A"
    },
    "0x37-0x40000" : {
      "Action" : 11,
      "Text" : "0x1f"
    },
    "0x32-0x40000" : {
      "Action" : 11,
      "Text" : "0x00"
    },
    "0xf709-0x20000" : {
      "Action" : 10,
      "Text" : "[17;2~"
    },
    "0xf70c-0x20000" : {
      "Action" : 10,
      "Text" : "[20;2~"
    },
    "0xf729-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2H"
    },
    "0xf72b-0x40000" : {
      "Action" : 10,
      "Text" : "[1;5F"
    },
    "0xf705-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2Q"
    },
    "0xf703-0x260000" : {
      "Action" : 10,
      "Text" : "[1;6C"
    },
    "0xf700-0x220000" : {
      "Action" : 10,
      "Text" : "[1;2A"
    },
    "0xf701-0x280000" : {
      "Action" : 11,
      "Text" : "0x1b 0x1b 0x5b 0x42"
    },
    "0x38-0x40000" : {
      "Action" : 11,
      "Text" : "0x7f"
    },
    "0x33-0x40000" : {
      "Action" : 11,
      "Text" : "0x1b"
    },
    "0xf703-0x220000" : {
      "Action" : 10,
      "Text" : "[1;2C"
    },
    "0xf701-0x240000" : {
      "Action" : 10,
      "Text" : "[1;5B"
    },
    "0xf70d-0x20000" : {
      "Action" : 10,
      "Text" : "[21;2~"
    },
    "0xf702-0x260000" : {
      "Action" : 10,
      "Text" : "[1;6D"
    },
    "0xf729-0x40000" : {
      "Action" : 10,
      "Text" : "[1;5H"
    },
    "0xf706-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2R"
    },
    "0x34-0x40000" : {
      "Action" : 11,
      "Text" : "0x1c"
    },
    "0xf700-0x280000" : {
      "Action" : 11,
      "Text" : "0x1b 0x1b 0x5b 0x41"
    },
    "0x2d-0x40000" : {
      "Action" : 11,
      "Text" : "0x1f"
    },
    "0xf70e-0x20000" : {
      "Action" : 10,
      "Text" : "[23;2~"
    },
    "0xf702-0x220000" : {
      "Action" : 10,
      "Text" : "[1;2D"
    },
    "0xf703-0x280000" : {
      "Action" : 11,
      "Text" : "0x1b 0x1b 0x5b 0x43"
    },
    "0xf700-0x240000" : {
      "Action" : 10,
      "Text" : "[1;5A"
    },
    "0xf707-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2S"
    },
    "0xf70a-0x20000" : {
      "Action" : 10,
      "Text" : "[18;2~"
    },
    "0x35-0x40000" : {
      "Action" : 11,
      "Text" : "0x1d"
    },
    "0xf70f-0x20000" : {
      "Action" : 10,
      "Text" : "[24;2~"
    },
    "0xf703-0x240000" : {
      "Action" : 10,
      "Text" : "[1;5C"
    },
    "0xf701-0x260000" : {
      "Action" : 10,
      "Text" : "[1;6B"
    },
    "0xf702-0x280000" : {
      "Action" : 11,
      "Text" : "0x1b 0x1b 0x5b 0x44"
    },
    "0xf72b-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2F"
    },
    "0x36-0x40000" : {
      "Action" : 11,
      "Text" : "0x1e"
    },
    "0xf708-0x20000" : {
      "Action" : 10,
      "Text" : "[15;2~"
    },
    "0xf701-0x220000" : {
      "Action" : 10,
      "Text" : "[1;2B"
    },
    "0xf70b-0x20000" : {
      "Action" : 10,
      "Text" : "[19;2~"
    },
    "0xf702-0x240000" : {
      "Action" : 10,
      "Text" : "[1;5D"
    },
    "0xf704-0x20000" : {
      "Action" : 10,
      "Text" : "[1;2P"
    }
  },
  "Window Type" : 0,
  "Background Image Location" : "",
  "Blur" : false,
  "Scrollback Lines" : 8000,
  "Send Code When Idle" : false,
  "Close Sessions On End" : true,
  "Terminal Type" : "xterm-256color",
  "Visual Bell" : true,
  "Flashing Bell" : false,
  "Scrollback in Alternate Screen" : false,
  "Silence Bell" : true,
  "Mouse Reporting allow mouse wheel" : false,
  "ASCII Ligatures" : false,
  "Name" : "Default",
  "Shortcut" : "",
  "Transparency" : 0,
  "Custom Directory" : "Recycle",
  "Guid" : "3A7967B4-6A1E-4D7C-846D-3C1FE12ACF7E",
  "Normal Font" : "DankMonoNerdFontCompleteM-Regular 23",
  "Horizontal Spacing" : 1,
  "Right Option Key Sends" : 2
}

Also, found this setting in experimental features (see picture), but I don't have it turned on. Seems like another option that could potentially affect this. image

iguntur commented 2 years ago

Hi @BStephenBB, yes, I have the { "Option Key Sends": 2 } in the JSON "Default" profile.

Here's the chunks of related to the key for "Option Key ..."

{
  // ...
  "Option Key Sends" : 2,
  "Right Option Key Sends" : 2,
  "Right Option Key Changeable" : true,
  // ...
}

I'm not sure what the { "Option Key Sends": 2 } meaning

dzfrias commented 2 years ago

Here are my settings related to the option key:

{
  "Option Key Sends" : 0,
  "Right Option Key Sends" : 0,
  "Right Option Key Changeable" : true,
}

After poking around these settings I think they are the problem. These settings are located under Profile->Keys. If we can find a way to temporarily change this setting for the user in the plugin, we could fix this issue. I have the Apps can change this setting on for the alt keys. And I can try turning on DECSET 1036 to see if there's an iTerm escape sequence for this.

Update: I found an iTerm escape sequence to change the option key to ESC+.

echo "\e[?1036h"

This worked for me, even though my option key sends 0. I entered Vim and and behaved the same way your terminals do. If we can trigger this within Vim we could set the proper behavior for the user. Unfortunately I had to turn on the DECSET 1036 setting to get this to work, but since that setting is in experimental mode, it might be automatically set in a future release of iTerm2.

BStephenBB commented 2 years ago

My 2¢:

I think we should figure out what will work for default "stock" iTerm and make vim-move work with that out of the box. We could also add a config option to make vim-move work with the alternative iTerm options.

If it's too hard to make vim-move work with default iTerm settings and other MacOS terminals, then we should consider having an iTerm specific config option and note that in the docs/README. We could also note how to adjust iTerm so that it behaves with vim-move.

dzfrias commented 2 years ago

I agree. I'm not sure what to keep as the default though. I never messed with the option key for my iTerm settings, so I think { "Option Key Sends" : 0 } is the default. I don't know if its the default for kitty and alacritty. We can always get their terminal name. However, I think we should assume { "Option Key Sends" : 2 } is the user's configuration, as it encourages users to set the option key to ESC+ permanently, which will most likely help them with other plugins with this problem. It would be much easier for every vim plugin if everybody who used iTerm had the { "Option Key Sends" : 2 } option on. What are your thoughts?

We should definitely include a note about this in the README and include an option for this.

michaeltintiuc commented 2 years ago

I've noticed vim-move broke today on Monterey 12.4, I didn't set any custom mappings but I had macos_option_as_alt yes in kitty, after setting it to the default no it started working

dzfrias commented 2 years ago

I made another pr (#72) about this issue. We might need more discussion about this, but I thought I should at least get a working version up in the air for now.

huyz commented 2 years ago

FYI, after this PR, this is what works for me: https://github.com/matze/vim-move/issues/69#issuecomment-1199891566