mapping.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. :orphan:
  2. Making your keyboard dance
  3. ==============================
  4. .. highlight:: conf
  5. kitty has extremely powerful facilities for mapping keyboard actions.
  6. Things like combining actions, multi-key mappings, modal mappings,
  7. mappings that send arbitrary text, and mappings dependent on the program
  8. currently running in kitty.
  9. Let's start with the basics. You can map a key press to an action in kitty using
  10. the following syntax::
  11. map ctrl+a new_window_with_cwd
  12. This will map the key press :kbd:`Ctrl+a` to open a new :term:`window`
  13. with the working directory set to the working directory of the current window.
  14. This is the basic operation of the map directive, the tip of the iceberg, for
  15. more read the sections below.
  16. Combining multiple actions on a single keypress
  17. -----------------------------------------------------
  18. Multiple actions can be combined on a single keypress, like a macro. To do this
  19. map the key press to the :ac:`combine` action::
  20. map key combine <separator> action1 <separator> action2 <separator> action3 ...
  21. For example::
  22. map kitty_mod+e combine : new_window : next_layout
  23. This will create a new window and switch to the next available layout. You can
  24. also run arbitrarily powerful scripts on a key press. There are two major
  25. techniques for doing this, using remote control scripts or using kittens.
  26. Remote control scripts
  27. ^^^^^^^^^^^^^^^^^^^^^^^^^
  28. These can be written in any language and use the "kitten" binary to control
  29. kitty via its extensive :doc:`Remote control <remote-control>` API. First,
  30. if you just want to run a single remote control command on a key press,
  31. you can just do::
  32. map f1 remote_control set-spacing margin=30
  33. This will run the ``set-spacing`` command, changing window margins to 30 pixels. For
  34. more complex scripts, write a script file in any language you like and save it
  35. somewhere, preferably in the kitty configuration directory. Do not forget to make it
  36. executable. In the script file you run remote control commands by running the
  37. "kitten" binary, for example:
  38. .. code-block:: sh
  39. #!/bin/sh
  40. kitten @ set-spacing margin=30
  41. kitten @ new_window
  42. ...
  43. The script can perform arbitrarily complex logic and actions, limited only by
  44. the remote control API, that you can browse by running ``kitten @ --help``.
  45. To run the script you created on a key press, use::
  46. map f1 remote_control_script /path/to/myscript
  47. Kittens
  48. ^^^^^^^^^^^^^
  49. Here, kittens refer to Python scripts. The scripts have two parts, one that
  50. runs as a regular command line program inside a kitty window to, for example,
  51. ask the user for some input and a second part that runs inside the kitty
  52. process itself and can perform any operation on the kitty UI, which is itself
  53. implemented in Python. However, the kitty internal API is not documented and
  54. can (very rarely) change, so kittens are harder to get started with than remote
  55. control scripts. To run a kitten on a key press::
  56. map f1 kitten mykitten.py
  57. Many of kitty's features are themselves implemented as kittens, for example,
  58. :doc:`/kittens/unicode_input`, :doc:`/kittens/hints` and
  59. :doc:`/kittens/themes`. To learn about writing your own kittens, see
  60. :doc:`/kittens/custom`.
  61. Syntax for specifying keys
  62. -----------------------------
  63. A mapping maps a key press to some action. In their most basic form, keypresses
  64. are :code:`modifier+key`. Keys are identified simply by their lowercase Unicode
  65. characters. For example: :code:`a` for the :kbd:`A` key, :code:`[` for the left
  66. square bracket key, etc. For functional keys, such as :kbd:`Enter` or
  67. :kbd:`Escape`, the names are present at :ref:`Functional key definitions
  68. <functional>`. For modifier keys, the names are :kbd:`ctrl` (:kbd:`control`,
  69. :kbd:`⌃`), :kbd:`shift` (:kbd:`⇧`), :kbd:`alt` (:kbd:`opt`, :kbd:`option`,
  70. :kbd:`⌥`), :kbd:`super` (:kbd:`cmd`, :kbd:`command`, :kbd:`⌘`).
  71. Additionally, you can use the name :opt:`kitty_mod` as a modifier, the default
  72. value of which is :kbd:`ctrl+shift`. The default kitty shortcuts are defined
  73. using this value, so by changing it in :file:`kitty.conf` you can change
  74. all the modifiers used by all the default shortcuts.
  75. On Linux, you can also use XKB names for functional keys that don't have kitty
  76. names. See :link:`XKB keys
  77. <https://github.com/xkbcommon/libxkbcommon/blob/master/include/xkbcommon/xkbcommon-keysyms.h>`
  78. for a list of key names. The name to use is the part after the :code:`XKB_KEY_`
  79. prefix. Note that you can only use an XKB key name for keys that are not known
  80. as kitty keys.
  81. Finally, you can use raw system key codes to map keys, again only for keys that
  82. are not known as kitty keys. To see the system key code for a key, start kitty
  83. with the :option:`kitty --debug-input` option, kitty will output some debug text
  84. for every key event. In that text look for :code:`native_code`, the value
  85. of that becomes the key name in the shortcut. For example:
  86. .. code-block:: none
  87. on_key_input: glfw key: 0x61 native_code: 0x61 action: PRESS mods: none text: 'a'
  88. Here, the key name for the :kbd:`A` key is :code:`0x61` and you can use it with::
  89. map ctrl+0x61 something
  90. This maps :kbd:`Ctrl+A` to something.
  91. Multi-key mappings
  92. --------------------
  93. A mapping in kitty can involve pressing multiple keys in sequence, with the
  94. syntax shown below::
  95. map key1>key2>key3 action
  96. For example::
  97. map ctrl+f>2 set_font_size 20
  98. The default mappings to run the :doc:`hints kitten </kittens/hints>` to select text on the screen are
  99. examples of multi-key mappings.
  100. Unmapping default shortcuts
  101. -----------------------------
  102. kitty comes with dozens of default keyboard mappings for common operations. See
  103. :doc:`actions` for the full list of actions and the default shortcuts that map
  104. to them. You can unmap an individual shortcut, so that it is passed on to the
  105. program running inside kitty, by mapping it to nothing, for example::
  106. map kitty_mod+enter
  107. This unmaps the default shortcut :sc:`new_window` to open a new window. Almost
  108. all default shortcuts are of the form ``modifier + key`` where the
  109. modifier defaults to :kbd:`Ctrl+Shift` and can be changed using the :opt:`kitty_mod` setting
  110. in :file:`kitty.conf`.
  111. If you want to clear all default shortcuts, you can use
  112. :opt:`clear_all_shortcuts` in :file:`kitty.conf`.
  113. If you would like kitty to completely ignore a key event, not even sending it to
  114. the program running in the terminal, map it to :ac:`discard_event`::
  115. map kitty_mod+f1 discard_event
  116. .. _conditional_mappings:
  117. Conditional mappings depending on the state of the focused window
  118. ----------------------------------------------------------------------
  119. Sometimes, you may want different mappings to be active when running a
  120. particular program in kitty, perhaps because it has some native functionality
  121. that duplicates kitty functions or there is a conflict, etc. kitty has the
  122. ability to create mappings that work only when the currently focused window
  123. matches some criteria, such as when it has a particular title or user variable.
  124. Let's see some examples::
  125. map --when-focus-on title:keyboard.protocol kitty_mod+t
  126. This will cause :kbd:`kitty_mod+t` (the default shortcut for opening a new tab)
  127. to be unmapped only when the focused window
  128. has :code:`keyboard protocol` in its title. Run the show-key kitten as::
  129. kitten show-key -m kitty
  130. Press :kbd:`ctrl+shift+t` and instead of a new tab opening, you will
  131. see the key press being reported by the kitten. :code:`--when-focus-on` can test
  132. the focused window using very powerful criteria, see :ref:`search_syntax` for
  133. details. A more practical example unmaps the key when the focused window is
  134. running an editor::
  135. map --when-focus-on var:in_editor kitty_mod+c
  136. In order to make this work, you need to configure your editor as show below:
  137. .. tab:: vim
  138. In :file:`~/.vimrc` add:
  139. .. code-block:: vim
  140. let &t_ti = &t_ti . "\\033]1337;SetUserVar=in_editor=MQo\\007"
  141. let &t_te = &t_te . "\\033]1337;SetUserVar=in_editor\\007"
  142. .. tab:: neovim
  143. In :file:`~/.config/nvim/init.lua` add:
  144. .. code-block:: lua
  145. vim.api.nvim_create_autocmd({ "VimEnter", "VimResume" }, {
  146. group = vim.api.nvim_create_augroup("KittySetVarVimEnter", { clear = true }),
  147. callback = function()
  148. io.stdout:write("\x1b]1337;SetUserVar=in_editor=MQo\007")
  149. end,
  150. })
  151. vim.api.nvim_create_autocmd({ "VimLeave", "VimSuspend" }, {
  152. group = vim.api.nvim_create_augroup("KittyUnsetVarVimLeave", { clear = true }),
  153. callback = function()
  154. io.stdout:write("\x1b]1337;SetUserVar=in_editor\007")
  155. end,
  156. })
  157. These cause the editor to set the :code:`in_editor` variable in kitty and unset it when exiting.
  158. As a result, the :kbd:`ctrl+shift+c` key will be passed to the editor instead of
  159. copying to clipboard. In the editor, you can map it to copy to the clipboard,
  160. thereby allowing use of a common shortcut both inside and outside the editor
  161. for copying to clipboard.
  162. Sending arbitrary text or keys to the program running in kitty
  163. --------------------------------------------------------------------------------
  164. This is accomplished by using ``map`` with :sc:`send_text <send_text>` in :file:`kitty.conf`.
  165. For example::
  166. map f1 send_text normal,application Hello, world!
  167. Now, pressing :kbd:`f1` will cause ``Hello, world!`` to show up at your shell
  168. prompt. To have the shell execute a command sent via ``send_text`` you need to
  169. also simulate pressing the enter key which is ``\r``. For example::
  170. map f1 send_text normal,application echo Hello, world!\r
  171. Now, if you press :kbd:`f1` when at shell prompt it will run the ``echo Hello,
  172. world!`` command.
  173. To have one key press send another key press, use :ac:`send_key`::
  174. map alt+s send_key ctrl+s
  175. This causes the program running in kitty to receive the :kbd:`ctrl+s` key when
  176. you press the :kbd:`alt+s` key. To see this in action, run::
  177. kitten show-key -m kitty
  178. Which will print out what key events it receives.
  179. .. _modal_mappings:
  180. Modal mappings
  181. --------------------------
  182. kitty has the ability, like vim, to use *modal* key maps. Except that unlike
  183. vim it allows you to define your own arbitrary number of modes. To create a new
  184. mode, use ``map --new-mode <my mode name> <shortcut to enter mode>``. For
  185. example, lets create a mode to manage windows: switching focus, moving the window, etc.::
  186. # Create a new "manage windows" mode (mw)
  187. map --new-mode mw kitty_mod+f7
  188. # Switch focus to the neighboring window in the indicated direction using arrow keys
  189. map --mode mw left neighboring_window left
  190. map --mode mw right neighboring_window right
  191. map --mode mw up neighboring_window up
  192. map --mode mw down neighboring_window down
  193. # Move the active window in the indicated direction
  194. map --mode mw shift+up move_window up
  195. map --mode mw shift+left move_window left
  196. map --mode mw shift+right move_window right
  197. map --mode mw shift+down move_window down
  198. # Resize the active window
  199. map --mode mw n resize_window narrower
  200. map --mode mw w resize_window wider
  201. map --mode mw t resize_window taller
  202. map --mode mw s resize_window shorter
  203. # Exit the manage window mode
  204. map --mode mw esc pop_keyboard_mode
  205. Now, if you run kitty as:
  206. .. code-block:: sh
  207. kitty -o enabled_layouts=vertical --session <(echo "launch\nlaunch\nlaunch")
  208. Press :kbd:`Ctrl+Shift+F7` to enter the mode and then press the up and
  209. down arrow keys to focus the next/previous window. Press :kbd:`Shift+Up` or
  210. :kbd:`Shift+Down` to move the active window up and down. Press :kbd:`t` to make
  211. the active window taller and :kbd:`s` to make it shorter. To exit the mode
  212. press :kbd:`Esc`.
  213. Pressing an unknown key while in a custom keyboard mode by default
  214. beeps. This can be controlled by the ``map --on-unknown`` option as shown
  215. below::
  216. # Beep on unknown keys
  217. map --new-mode XXX --on-unknown beep ...
  218. # Ingore unknown keys silently
  219. map --new-mode XXX --on-unknown ignore ...
  220. # Beep and exit the keyboard mode on unknown key
  221. map --new-mode XXX --on-unknown end ...
  222. # Pass unknown keys to the program running in the active window
  223. map --new-mode XXX --on-unknown passthrough ...
  224. When a key matches an action in a custom keyboard mode, the action is performed
  225. and the custom keyboard mode remains in effect. If you would rather have the
  226. keyboard mode end after the action you can use ``map --on-action`` as shown
  227. below::
  228. # Have this keyboard mode automatically exit after performing any action
  229. map --new-mode XXX --on-action end ...
  230. All mappable actions
  231. ------------------------
  232. There is a list of :doc:`all mappable actions <actions>`.
  233. Debugging mapping issues
  234. ------------------------------
  235. To debug mapping issues, kitty has several facilities. First, when you run
  236. kitty with the ``--debug-input`` command line flag it outputs details
  237. about all key events it receives form the system and how they are handled.
  238. To see what key events are sent to applications, run kitty like this::
  239. kitty kitten show-key
  240. Press the keys you want to debug and the kitten will print out the bytes it
  241. receives. Note that this uses the legacy terminal keyboard protocol that does
  242. not support all keys and key events. To debug the :doc:`full kitty keyboard
  243. protocol that <keyboard-protocol>` that is nowadays being adopted by more and
  244. more programs, use::
  245. kitty kitten show-key -m kitty