[LINUX] I want to use Mac / emacs key bindings (keyboard shortcuts) on Ubuntu xkeysnail recommendations

Xkeysnail that can convert keyboard shortcuts is wonderful, so let's use it.

On Mac, you can use Ctrl + a to move to the beginning of a line and Ctrl + k to cut to the end of a line, not just in the emacs terminal, but everywhere. I want to achieve this on Ubuntu as well. I wasn't quite happy with gnome-tweak-tool key-theme = Emacs, which implements emacs key bindings in Gnome; it was overwritten by Windows-like shortcuts in the browser, especially in Jupyter Notebook with Ctrl-a. It's hard to say "select the whole". xkeysnail makes Ctrl + a look like the Home key to move to the beginning of a line, so it can be used in a browser (as long as the web app supports normal windows styles). There is no Ubuntu element in particular and it can be used in Linux in general.

1. Installation

$ sudo apt install python3-pip
$ sudo pip3 install xkeysnail

pip is Python's library package management software.

2. Create config.py

For the time being, it can be anywhere, so create config.py. Click here for official config.py.

3. Run

In the directory where that config.py is:

$ xhost +SI:localuser:root
$ sudo xkeysnail config.py

Troubleshooting

  1. command not found: xkeysnail

If you do pip3 install ... instead of sudo pip3 install ..., it will be installed in ~ / .local / bin / xkeysnail and your PATH is probably not in your path.

$ pip3 show xkeysnail
(Omitted)
Location: /usr/local/lib/python3.6/dist-packages

You can find the installation destination by looking at. If you are using many environments such as conda and pyenv, be careful which environment you installed. The environment may differ depending on whether you use sudo or not. To redo the forgotten sudo pip

$ pip3 uninstall xkeysnail
$ sudo pip3 install xkeysnail

Alternatively, pass it through the PATH if you like, or execute it with the absolute PATH.

  1. ImportError: sys.meta_path is None, Python is likely shutting down

If you look higher, you will find more specific errors. For example

FileNotFoundError: [Errno 2] No such file or directory: 'config.pu'

Wrong the file name of config.py. You may have the wrong key name in config.py.

  1. Xlib.error.DisplayConnectionError: Can't connect to display ":0":

It appears if you have not done xhost +.

My config.py


# -*- coding: utf-8 -*-
import string
from xkeysnail.transform import *

# define timeout for multipurpose_modmap
define_timeout(1)

# [Global modemap] Change modifier keys as in xmodmap
define_modmap({
    Key.CAPSLOCK: Key.ESC,  #Use caps lock as Esc. There may be many Ctrl groups
    Key.LEFT_META: Key.RIGHT_META, #overview Disable:Left ⌘ → Right ⌘
})

# Always paste with ⌘-v including terminals
#None is unconditional=Means all applications
define_keymap(None, {
    K("Super-v"): [K("C-v"), set_mark(False)],
}, "Paste")


# Mac-like keybindings outside emacs and terminals
#Minimal emacs keybinding.
#Official config.py is more emacs. C-x C-s → You can also save.
mapping = {
    # Beginning/End of line
    K("C-a"): with_mark(K("home")),
    K("C-e"): with_mark(K("end")),
    
    # Delete
    K("C-d"): [K("delete"), set_mark(False)],
    K("C-h"): with_mark(K("backspace")),
    
    # Kill line
    K("C-k"): [K("Shift-end"), K("C-x"), set_mark(False)],
}

#Ctrl ⌘α for all alphabets α+Suppose you are pressing α
for c in string.ascii_lowercase:
    mapping[K('Super-' + c)] = K('C-' + c)

#Change the shortcut outside of Emacs and Terminal. Hyper is the name of the terminal application I'm using.
define_keymap(lambda wm_class: wm_class not in ("Emacs", "Hyper", "Gnome-terminal"),
              mapping, "Mac-like")

You can see it by looking at the standard output of the terminal where ** wm_class ** is running xkeysnail with the application name. The display of this WM_CLASS and keys is suppressed by adding the xkeysnail -q option.

Minimal python

For those unfamiliar with Python.

comment

Comment from # to the end of the line.

String
Dictionary dict

A data structure that stores Key-Value pairs; shortcuts you want to define: used to give shortcuts to be executed.

d = {key1: value1.
     key2: value2,
     key3: value3}

It's okay to add a comma (trailing comma) at the end, or not {key3: value3,}.


Can be added after creation:

d[key4] = value4



#### Tuple, list, set

```python
wm_class not in ("Hyper", "Gnome-terminal", "Emacs")

String wm_True if class is none of the three. By the negation operator not

not (wm_class in ("Hyper", "Gnome-terminal", "Emacs"))

Same as. If you want to make it True

wm_class in ("Hyper", "Gnome-terminal", "Emacs")

("a", "b", "c")Is an array that cannot be modifiedtuple.An array that you can change if you don't care about the detailslist ["a", "b", "c"]Also setset {"a", "b", "c"}Is the same.

If one

 wm_class == "Hyper" # True if equal
 wm_class! = "Hyper" # True if not equal

But it is good. Don't forget the last comma when using a single element tuple:

wm_class in ("Hyper",)

Without commas

wm_class in "Hyper"

The meaning changes as it becomes the same as "wm_class is"Hyper"Is it a substring? Will mean.

"ype" in "Hyper"  # => True
"ype" in ("Hyper")  # => True, ("Hyper") = "Hyper"
 "type" in ("Hyper",) # => False, "ype" is "Hyper"
 It is not an element of a set consisting of one element #.

#####Anonymous function lambda

f = lambda x: x**2

Is

def f(x):
    return x**2

Same as. Application name wm_Used to define a function that returns whether to change a keyboard shortcut for a class.

define_keymap function

Application namewm_classYou can change whether to convert the shortcut by.

def define_keymap(condition, mappings: dict, name: str):

conditionsconditionDictionary ifmappingsPerforms the shortcut conversion defined in.nameIs the name of any setting.

There are three ways to specify condition:

condition:
 None: Apply unconditionally
 Function: A function that takes the application name wm_class and returns True / False
 Regular expression: Regular expression that wm_class should match (details omitted)

Key

Key.CAPSLOCKAndKey.ESCThe list of thingskey.pyIt is written in.

f Key
ESC
TAB
CAPSLOCK
LEFT_CTRL, RIGHT_CTRL
LEFT_SHIFT, RIGHT_SHIFT
Key Apple Magic keyboard Windows keyboard
LEFT_META, RIGHT_META command ⌘ Win key
LEFT_ALT, RIGHT_ALT option ⌥ Alt

##K function

K("C-b")With Ctrl+Represents b. C can be Ctrl or can be limited to left or right by adding LR.You can see how to write it by pressing a key and looking at the standard output of xkeysnail..The Modifier key also has a generic term or alias (Win can be Super, etc.) when it can be left or right..

Modifier key str for K
Ctrl ^ C, Ctrl, LC, RC, LCtrl, RCtrl
Shift Shift, LShift, RShift
Win/command ⌘ Win, Super, LWin, RWin, LSuper, RSuper
Alt/option ⌥ Alt, M, LAlt, RAlt, RM, LM

reference: create_modifiers_from_strings(modifier_strs) in transform.py.

##Automatic start

If the settings are complete and it works, I want to run it automatically when the computer starts. For the time being:

https://hidakatsuya.hateblo.jp/entry/2019/11/20/215608

Reference

Author's article: https://qiita.com/mooz@github/items/c5f25f27847333dd0b37 GitHub: https://github.com/mooz/xkeysnail

Recommended Posts

I want to use Mac / emacs key bindings (keyboard shortcuts) on Ubuntu xkeysnail recommendations
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to AWS Lambda with Python on Mac!
I want to know if you install Python on Mac ・ Iroha
I want to use both key and value of Python iterator
I want to use jar from python
I want to use IPython Qt Console
I want to develop Android apps on Android
I tried to use Resultoon on Mac + AVT-C875, but I was frustrated on the way.
When I try to use Jupiter notebook on Mac, I can only select python2
I want to realize something like AutoHotkey with AutoKey on Ubuntu (Kali Linux)
I want to use MATLAB feval with python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
I want to do pyenv + pipenv on Windows
I want to use the activation function Mish
Preparing to use aws cli on Mac OS X
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to find a popular package on PyPi
I want to restart CentOS 8 on time every day.
I want to use the R dataset in python
I want to set up a GUI development environment with Python or Golang on Mac
I don't tweet, but I want to use tweepy: just display the search results on the console
I want to use Ubuntu's desktop environment on Android for the time being (UserLAnd version)