1
votes

In Sublime Text 3 on OSX, I am able to use the keyboard shortcut "command+left/right arrow" to jump to the beginning/end of a line, as well as "command+shift+left/right arrow" to select from the point I am at to the beginning/end of the line.

Is there a way to get the same shortcut on linux or windows in the form of "ctrl+left/right arrow" and "ctrl+shift+left/right arrow" respectively?

2

2 Answers

5
votes

and are built-in OS X shortcuts, and are not part of Sublime. However, you can mimic this behavior on Windows or Linux by overriding the default behaviors of Ctrl/ and CtrlShift/ using a custom keymap.

By default, using Ctrl/ moves the cursor backwards and forwards, respectively, by words, while holding down Shift allows for selection. If you want to change this, open Preferences -> Key Bindings-User and add the following content:

{
    "keys": ["ctrl+right"],
    "command": "move_to", "args": {"to": "eol", "extend": false}
},
{
    "keys": ["ctrl+left"],
    "command": "move_to", "args": {"to": "bol", "extend": false}
},
{
    "keys": ["ctrl+shift+right"],
    "command": "move_to", "args": {"to": "eol", "extend": true}
},
{
    "keys": ["ctrl+shift+left"],
    "command": "move_to", "args": {"to": "bol", "extend": true}
}

If the file is empty when you open it, make sure you surround everything with opening and closing square brackets [ ]. Save the file (it will automatically save in the correct place, which is the User directory under Packages, the directory opened when selecting Preferences -> Browse Packages...), and your new shortcuts should work as expected. Please not that when you are working with indented text, moving to the beginning of the line with Ctrl will move you to the beginning of the text on that line, not to the very first position on the line. If you want to do that, either hit Ctrl again, or change bol to hardbol in the key definitions above.

1
votes

Kinto is something I created recently that not only remaps the keyboard to be more mac like it will change the keymap, as needed for terminal usage and provides a custom keymap to handle Cmd + arrow keys system-wide. You no longer will need to specify a custom key binding in sublime text.

Initially Kinto did not handle Cmd + arrow keys, but that was literally one of the first issues someone brought up to me so I added it in by creating a native xkb keymap.

https://github.com/rbreaves/kinto

Update

Kinto now uses xkeysnail to simplify the remapping process.

Here's an example of the what the new config file looks like, but the full remap is part of Kinto and needs the latest xkeysnail from github, not the repos.

# -*- coding: utf-8 -*-

import re
from xkeysnail.transform import *

terminals = ["gnome-terminal","konsole","io.elementary.terminal","terminator","sakura","guake","tilda","xterm","eterm","kitty"]
terminals = [term.casefold() for term in terminals]
termStr = "|".join(str(x) for x in terminals)

# [Conditional modmap] Change modifier keys in certain applications
define_conditional_modmap(lambda wm_class: wm_class.casefold() not in terminals,{
    # Default Mac/Win
    Key.LEFT_ALT: Key.RIGHT_CTRL,   # WinMac
    Key.LEFT_META: Key.LEFT_ALT,    # WinMac
    Key.LEFT_CTRL: Key.LEFT_META,   # WinMac
    Key.RIGHT_ALT: Key.RIGHT_CTRL,  # WinMac
    Key.RIGHT_META: Key.RIGHT_ALT,  # WinMac
    Key.RIGHT_CTRL: Key.RIGHT_META, # WinMac
})

# [Conditional modmap] Change modifier keys in certain applications
define_conditional_modmap(re.compile(termStr, re.IGNORECASE), {
    # Default Mac/Win
    Key.LEFT_ALT: Key.RIGHT_CTRL,   # WinMac
    Key.LEFT_META: Key.LEFT_ALT,    # WinMac
    Key.LEFT_CTRL: Key.LEFT_CTRL,   # WinMac
    Key.RIGHT_ALT: Key.RIGHT_CTRL,  # WinMac
    Key.RIGHT_META: Key.RIGHT_ALT,  # WinMac
    Key.RIGHT_CTRL: Key.LEFT_CTRL,  # WinMac
})

define_keymap(None,{
    # Cmd Tab - App Switching Default
    K("RC-Tab"): K("RC-F13"),                     # Default
    K("RC-Shift-Tab"): K("RC-Shift-F13"),         # Default
    K("RC-Grave"): K("RC-Shift-F13"),             # Default
    # K("RC-Tab"): K("RC-backslash"),               # Chromebook
    # K("RC-Shift-Tab"): K("RC-Shift-backslash"),   # Chromebook
    # K("RC-Grave"): K("RC-Shift-backslash"),       # Chromebook
    # In-App Tab switching
    # K("M-Tab"): K("C-Tab"),                       # Chromebook - In-App Tab switching
    # K("M-Shift-Tab"): K("C-Shift-Tab"),           # Chromebook - In-App Tab switching
    # K("M-Grave") : K("C-Shift-Tab"),              # Chromebook - In-App Tab switching
    K("Super-Tab"): K("LC-Tab"),                  # Default
    K("Super-Shift-Tab"): K("LC-Shift-Tab"),      # Default
    K("LC-Grave") : K("LC-Shift-Tab"),            # Default

    # Wordwise
    K("RC-Left"): K("Home"),                      # Beginning of Line
    K("RC-Shift-Left"): K("Shift-Home"),          # Select all to Beginning of Line
    K("RC-Right"): K("End"),                      # End of Line
    K("RC-Shift-Right"): K("Shift-End"),          # Select all to End of Line
    # K("RC-Left"): K("C-LEFT_BRACE"),              # Firefox-nw - Back
    # K("RC-Right"): K("C-RIGHT_BRACE"),            # Firefox-nw - Forward
    # K("RC-Left"): K("M-LEFT"),                    # Chrome-nw - Back
    # K("RC-Right"): K("M-RIGHT"),                  # Chrome-nw - Forward
    K("RC-Up"): K("C-Home"),                      # Beginning of File
    K("RC-Shift-Up"): K("C-Shift-Home"),          # Select all to Beginning of File
    K("RC-Down"): K("C-End"),                     # End of File
    K("RC-Shift-Down"): K("C-Shift-End"),         # Select all to End of File
    K("M-Backspace"): K("Delete"),                # Delete
    # K(""): pass_through_key,                      # cancel
    # K(""): K(""),                                 #
})

define_keymap(lambda wm_class: wm_class.casefold() not in ("code"),{
    # Wordwise remaining - for Everything but VS Code
    K("M-Left"): K("C-Left"),               # Left of Word
    K("M-Shift-Left"): K("C-Shift-Left"),   # Select Left of Word
    K("M-Right"): K("C-Right"),             # Right of Word
    K("M-Shift-Right"): K("C-Shift-Right"), # Select Right of Word
    # ** VS Code fix **
    #   Electron issue precludes normal keybinding fix.
    #   Alt menu auto-focus/toggle gets in the way.
    #
    #   refer to ./xkeysnail-config/vscode_keybindings.json
    # **
    #
    # ** Firefox fix **
    #   User will need to set "ui.key.menuAccessKeyFocuses"
    #   under about:config to false.
    #
    #   https://superuser.com/questions/770301/pentadactyl-how-to-disable-menu-bar-toggle-by-alt
    # **
    #
})

define_keymap(re.compile(termStr, re.IGNORECASE),{
    # Ctrl Tab - In App Tab Switching
    K("LC-Tab") : K("LC-PAGE_DOWN"),
    K("LC-Shift-Tab") : K("LC-PAGE_UP"),
    K("LC-Grave") : K("LC-PAGE_UP"),
    # Converts Cmd to use Ctrl-Shift
    K("RC-Tab"): K("RC-F13"),
    K("RC-Shift-Tab"): K("RC-Shift-F13"),
    K("RC-V"): K("C-Shift-V"),
    K("RC-MINUS"): K("C-Shift-MINUS"),
    K("RC-EQUAL"): K("C-Shift-EQUAL"),
    K("RC-BACKSPACE"): K("C-Shift-BACKSPACE"),
    K("RC-Q"): K("C-Shift-Q"),
    K("RC-W"): K("C-Shift-W"),
    K("RC-E"): K("C-Shift-E"),
    K("RC-R"): K("C-Shift-R"),
    K("RC-T"): K("C-Shift-t"),
    K("RC-Y"): K("C-Shift-Y"),
    K("RC-U"): K("C-Shift-U"),
    K("RC-I"): K("C-Shift-I"),
    K("RC-O"): K("C-Shift-O"),
    K("RC-P"): K("C-Shift-P"),
    K("RC-LEFT_BRACE"): K("C-Shift-LEFT_BRACE"),
    K("RC-RIGHT_BRACE"): K("C-Shift-RIGHT_BRACE"),
    K("RC-A"): K("C-Shift-A"),
    K("RC-S"): K("C-Shift-S"),
    K("RC-D"): K("C-Shift-D"),
    K("RC-F"): K("C-Shift-F"),
    K("RC-G"): K("C-Shift-G"),
    K("RC-H"): K("C-Shift-H"),
    K("RC-J"): K("C-Shift-J"),
    K("RC-K"): K("C-Shift-K"),
    K("RC-L"): K("C-Shift-L"),
    K("RC-SEMICOLON"): K("C-Shift-SEMICOLON"),
    K("RC-APOSTROPHE"): K("C-Shift-APOSTROPHE"),
    K("RC-GRAVE"): K("C-Shift-GRAVE"),
    K("RC-BACKSLASH"): K("C-Shift-BACKSLASH"),
    K("RC-Z"): K("C-Shift-Z"),
    K("RC-X"): K("C-Shift-X"),
    K("RC-C"): K("C-Shift-C"),
    K("RC-V"): K("C-Shift-V"),
    K("RC-B"): K("C-Shift-B"),
    K("RC-N"): K("C-Shift-N"),
    K("RC-M"): K("C-Shift-M"),
    K("RC-COMMA"): K("C-Shift-COMMA"),
    K("RC-DOT"): K("C-Shift-DOT"),
    K("RC-SLASH"): K("C-Shift-SLASH"),
    K("RC-KPASTERISK"): K("C-Shift-KPASTERISK"),
}, "terminals")

https://medium.com/@benreaves/kinto-a-mac-inspired-keyboard-mapping-for-linux-58f731817c0

Here's a Gist as well, if you just want to see what is at the heart of it all, it will not alternate your keymap when needed though. The Gist also does not include custom xkb keymap files that setup macOS style cursors/word-wise manipulations that use Cmd and the arrow keys.

https://gist.github.com/rbreaves/f4cf8a991eaeea893999964f5e83eebb