shell-integration.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. .. _shell_integration:
  2. Shell integration
  3. -------------------
  4. kitty has the ability to integrate closely within common shells, such as `zsh
  5. <https://www.zsh.org/>`__, `fish <https://fishshell.com>`__ and `bash
  6. <https://www.gnu.org/software/bash/>`__ to enable features such as jumping to
  7. previous prompts in the scrollback, viewing the output of the last command in
  8. :program:`less`, using the mouse to move the cursor while editing prompts, etc.
  9. .. versionadded:: 0.24.0
  10. Features
  11. -------------
  12. * Open the output of the last command in a pager such as :program:`less`
  13. (:sc:`show_last_command_output`)
  14. * Jump to the previous/next prompt in the scrollback
  15. (:sc:`scroll_to_previous_prompt` / :sc:`scroll_to_next_prompt`)
  16. * Click with the mouse anywhere in the current command to move the cursor there
  17. * Hold :kbd:`Ctrl+Shift` and right-click on any command output in the scrollback
  18. to view it in a pager
  19. * The current working directory or the command being executed are automatically
  20. displayed in the kitty window titlebar/tab title
  21. * The text cursor is changed to a bar when editing commands at the shell prompt
  22. * :ref:`clone_shell` with all environment variables and the working directory
  23. copied
  24. * :ref:`Edit files in new kitty windows <edit_file>` even over SSH
  25. * Glitch free window resizing even with complex prompts. Achieved by erasing
  26. the prompt on resize and allowing the shell to redraw it cleanly.
  27. * Sophisticated completion for the :program:`kitty` command in the shell.
  28. * When confirming a quit command if a window is sitting at a shell prompt,
  29. it is not counted (for details, see :opt:`confirm_os_window_close`)
  30. Configuration
  31. ---------------
  32. Shell integration is controlled by the :opt:`shell_integration` option. By
  33. default, all integration features are enabled. Individual features can be turned
  34. off or it can be disabled entirely as well. The :opt:`shell_integration` option
  35. takes a space separated list of keywords:
  36. disabled
  37. Turn off all shell integration. The shell's launch environment is not
  38. modified and :envvar:`KITTY_SHELL_INTEGRATION` is not set. Useful for
  39. :ref:`manual integration <manual_shell_integration>`.
  40. no-rc
  41. Do not modify the shell's launch environment to enable integration. Useful
  42. if you prefer to load the kitty shell integration code yourself, either as
  43. part of :ref:`manual integration <manual_shell_integration>` or because
  44. you have some other software that sets up shell integration.
  45. This will still set the :envvar:`KITTY_SHELL_INTEGRATION` environment
  46. variable when kitty runs the shell.
  47. no-cursor
  48. Turn off changing of the text cursor to a bar when editing shell command
  49. line.
  50. no-title
  51. Turn off setting the kitty window/tab title based on shell state.
  52. Note that for the fish shell kitty relies on fish's native title setting
  53. functionality instead.
  54. no-cwd
  55. Turn off reporting the current working directory. This is used to allow
  56. :ac:`new_window_with_cwd` and similar to open windows logged into remote
  57. machines using the :doc:`ssh kitten <kittens/ssh>` automatically with the
  58. same working directory as the current window.
  59. Note that for the fish shell this will not disable its built-in current
  60. working directory reporting.
  61. no-prompt-mark
  62. Turn off marking of prompts. This disables jumping to prompt, browsing
  63. output of last command and click to move cursor functionality.
  64. no-complete
  65. Turn off completion for the kitty command.
  66. Note that for the fish shell this does not take effect, since fish already
  67. comes with a kitty completion script.
  68. no-sudo
  69. Do not alias :program:`sudo` to ensure the kitty terminfo files are
  70. available in the sudo environment. This is needed if you have sudo
  71. configured to disable setting of environment variables on the command line.
  72. By default, if sudo is configured to allow all commands for the current
  73. user, setting of environment variables at the command line is also allowed.
  74. Only if commands are restricted is this needed.
  75. More ways to browse command output
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. You can add further key and mouse bindings to browse the output of commands
  78. easily. For example to select the output of a command by right clicking the
  79. mouse on the output, define the following in :file:`kitty.conf`:
  80. .. code:: conf
  81. mouse_map right press ungrabbed mouse_select_command_output
  82. Now, when you right click on the output, the entire output is selected, ready
  83. to be copied.
  84. The feature to jump to previous prompts (
  85. :sc:`scroll_to_previous_prompt` and :sc:`scroll_to_next_prompt`) and mouse
  86. actions (:ac:`mouse_select_command_output` and :ac:`mouse_show_command_output`)
  87. can be integrated with browsing command output as well. For example, define the
  88. following mapping in :file:`kitty.conf`:
  89. .. code:: conf
  90. map f1 show_last_visited_command_output
  91. Now, pressing :kbd:`F1` will cause the output of the last jumped to command or
  92. the last mouse clicked command output to be opened in a pager for easy browsing.
  93. In addition, You can define shortcut to get the first command output on screen.
  94. For example, define the following in :file:`kitty.conf`:
  95. .. code:: conf
  96. map f1 show_first_command_output_on_screen
  97. Now, pressing :kbd:`F1` will cause the output of the first command output on
  98. screen to be opened in a pager.
  99. You can also add shortcut to scroll to the last jumped position. For example,
  100. define the following in :file:`kitty.conf`:
  101. .. code:: conf
  102. map f1 scroll_to_prompt 0
  103. How it works
  104. -----------------
  105. At startup, kitty detects if the shell you have configured (either system wide
  106. or the :opt:`shell` option in :file:`kitty.conf`) is a supported shell. If so,
  107. kitty injects some shell specific code into the shell, to enable shell
  108. integration. How it does so varies for different shells.
  109. .. tab:: zsh
  110. For zsh, kitty sets the :envvar:`ZDOTDIR` environment variable to make zsh
  111. load kitty's :file:`.zshenv` which restores the original value of
  112. :envvar:`ZDOTDIR` and sources the original :file:`.zshenv`. It then loads
  113. the shell integration code. The remainder of zsh's startup process proceeds
  114. as normal.
  115. .. tab:: fish
  116. For fish, to make it automatically load the integration code provided by
  117. kitty, the integration script directory path is prepended to the
  118. :envvar:`XDG_DATA_DIRS` environment variable. This is only applied to the
  119. fish process and will be cleaned up by the integration script after startup.
  120. No files are added or modified.
  121. .. tab:: bash
  122. For bash, kitty starts bash in POSIX mode, using the environment variable
  123. :envvar:`ENV` to load the shell integration script. This prevents bash from
  124. loading any startup files itself. The loading of the startup files is done
  125. by the integration script, after disabling POSIX mode. From the perspective
  126. of those scripts there should be no difference to running vanilla bash.
  127. Then, when launching the shell, kitty sets the environment variable
  128. :envvar:`KITTY_SHELL_INTEGRATION` to the value of the :opt:`shell_integration`
  129. option. The shell integration code reads the environment variable, turns on the
  130. specified integration functionality and then unsets the variable so as to not
  131. pollute the system.
  132. The actual shell integration code uses hooks provided by each shell to send
  133. special escape codes to kitty, to perform the various tasks. You can see the
  134. code used for each shell below:
  135. .. raw:: html
  136. <details>
  137. <summary>Click to toggle shell integration code</summary>
  138. .. tab:: zsh
  139. .. literalinclude:: ../shell-integration/zsh/kitty-integration
  140. :language: zsh
  141. .. tab:: fish
  142. .. literalinclude:: ../shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish
  143. :language: fish
  144. :force:
  145. .. tab:: bash
  146. .. literalinclude:: ../shell-integration/bash/kitty.bash
  147. :language: bash
  148. .. raw:: html
  149. </details>
  150. Shell integration over SSH
  151. ----------------------------
  152. The easiest way to have shell integration work when SSHing into remote systems
  153. is to use the :doc:`ssh kitten <kittens/ssh>`. Simply run::
  154. kitten ssh hostname
  155. And, by magic, you will be logged into the remote system with fully functional
  156. shell integration. Alternately, you can :ref:`setup shell integration manually
  157. <manual_shell_integration>`, by copying the kitty shell integration scripts to
  158. the remote server and editing the shell rc files there, as described below.
  159. Shell integration in a container
  160. ----------------------------------
  161. Install the kitten `standalone binary
  162. <https://github.com/kovidgoyal/kitty/releases/latest/download/kitten-linux-amd64>`__ in the container
  163. somewhere in the PATH, then you can log into the container with:
  164. .. code-block:: sh
  165. docker exec -ti container-id kitten run-shell --shell=/path/to/your/shell/in/the/container
  166. The kitten will even take care of making the kitty terminfo database available
  167. in the container automatically.
  168. .. _clone_shell:
  169. Clone the current shell into a new window
  170. -----------------------------------------------
  171. You can clone the current shell into a new kitty window by simply running the
  172. :command:`clone-in-kitty` command, for example:
  173. .. code-block:: sh
  174. clone-in-kitty
  175. clone-in-kitty --type=tab
  176. clone-in-kitty --title "I am a clone"
  177. This will open a new window running a new shell instance but with all
  178. environment variables and the current working directory copied. This even works
  179. over SSH when using :doc:`kittens/ssh`.
  180. The :command:`clone-in-kitty` command takes almost all the same arguments as the
  181. :doc:`launch <launch>` command, so you can open a new tab instead or a new OS
  182. window, etc. Arguments of launch that can cause code execution or that don't
  183. make sense when cloning are ignored. Most prominently, the following options are
  184. ignored: :option:`--allow-remote-control <launch --allow-remote-control>`,
  185. :option:`--copy-cmdline <launch --copy-cmdline>`, :option:`--copy-env <launch
  186. --copy-env>`, :option:`--stdin-source <launch --stdin-source>`,
  187. :option:`--marker <launch --marker>` and :option:`--watcher <launch --watcher>`.
  188. :command:`clone-in-kitty` can be configured to source arbitrary code in the
  189. cloned window using environment variables. It will automatically clone virtual
  190. environments created by the :link:`Python venv module
  191. <https://docs.python.org/3/library/venv.html>` or :link:`Conda
  192. <https://conda.io/>`. In addition, setting the
  193. env var :envvar:`KITTY_CLONE_SOURCE_CODE` to some shell code will cause that
  194. code to be run in the cloned window with :code:`eval`. Similarly, setting
  195. :envvar:`KITTY_CLONE_SOURCE_PATH` to the path of a file will cause that file to
  196. be sourced in the cloned window. This can be controlled by
  197. :opt:`clone_source_strategies`.
  198. :command:`clone-in-kitty` works by asking the shell to serialize its internal
  199. state (mainly CWD and env vars) and this state is transmitted to kitty and
  200. restored by the shell integration scripts in the cloned window.
  201. .. _edit_file:
  202. Edit files in new kitty windows even over SSH
  203. ------------------------------------------------
  204. .. code-block:: sh
  205. edit-in-kitty myfile.txt
  206. edit-in-kitty --type tab --title "Editing My File" myfile.txt
  207. # open myfile.txt at line 75 (works with vim, neovim, emacs, nano, micro)
  208. edit-in-kitty +75 myfile.txt
  209. The :command:`edit-in-kitty` command allows you to seamlessly edit files
  210. in your default :opt:`editor` in new kitty windows. This works even over
  211. SSH (if you use the :doc:`ssh kitten <kittens/ssh>`), allowing you
  212. to easily edit remote files in your local editor with all its bells and
  213. whistles.
  214. The :command:`edit-in-kitty` command takes almost all the same arguments as the
  215. :doc:`launch <launch>` command, so you can open a new tab instead or a new OS
  216. window, etc. Not all arguments are supported, see the discussion in the
  217. :ref:`clone_shell` section above.
  218. In order to avoid remote code execution, kitty will only execute the configured
  219. editor and pass the file path to edit to it.
  220. .. note:: To edit files using sudo the best method is to set the
  221. :code:`SUDO_EDITOR` environment variable to ``kitten edit-in-kitty`` and
  222. then edit the file using the ``sudoedit`` or ``sudo -e`` commands.
  223. .. _run_shell:
  224. Using shell integration in sub-shells, containers, etc.
  225. -----------------------------------------------------------
  226. .. versionadded:: 0.29.0
  227. To start a sub-shell with shell integration automatically setup, simply run::
  228. kitten run-shell
  229. This will start a sub-shell using the same binary as the currently running
  230. shell, with shell-integration enabled. To start a particular shell use::
  231. kitten run-shell --shell=/bin/bash
  232. To run a command before starting the shell use::
  233. kitten run-shell ls .
  234. This will run ``ls .`` before starting the shell.
  235. This will even work on remote systems where kitty itself is not installed,
  236. provided you use the :doc:`SSH kitten <kittens/ssh>` to connect to the system.
  237. Use ``kitten run-shell --help`` to learn more.
  238. .. _manual_shell_integration:
  239. Manual shell integration
  240. ----------------------------
  241. The automatic shell integration is designed to be minimally intrusive, as such
  242. it won't work for sub-shells, terminal multiplexers, containers, etc.
  243. For such systems, you should either use the :ref:`run-shell <run_shell>` command described above or
  244. setup manual shell integration by adding some code to your shells startup files to load the shell integration script.
  245. First, in :file:`kitty.conf` set:
  246. .. code-block:: conf
  247. shell_integration disabled
  248. Then in your shell's rc file, add the lines:
  249. .. tab:: zsh
  250. .. code-block:: sh
  251. if test -n "$KITTY_INSTALLATION_DIR"; then
  252. export KITTY_SHELL_INTEGRATION="enabled"
  253. autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
  254. kitty-integration
  255. unfunction kitty-integration
  256. fi
  257. .. tab:: fish
  258. .. code-block:: fish
  259. if set -q KITTY_INSTALLATION_DIR
  260. set --global KITTY_SHELL_INTEGRATION enabled
  261. source "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish"
  262. set --prepend fish_complete_path "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_completions.d"
  263. end
  264. .. tab:: bash
  265. .. code-block:: sh
  266. if test -n "$KITTY_INSTALLATION_DIR"; then
  267. export KITTY_SHELL_INTEGRATION="enabled"
  268. source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
  269. fi
  270. The value of :envvar:`KITTY_SHELL_INTEGRATION` is the same as that for
  271. :opt:`shell_integration`, except if you want to disable shell integration
  272. completely, in which case simply do not set the
  273. :envvar:`KITTY_SHELL_INTEGRATION` variable at all.
  274. In a container, you will need to install the kitty shell integration scripts
  275. and make sure the :envvar:`KITTY_INSTALLATION_DIR` environment variable is set
  276. to point to the location of the scripts.
  277. Integration with other shells
  278. -------------------------------
  279. There exist third-party integrations to use these features for various other
  280. shells:
  281. * Jupyter console and IPython via a patch (:iss:`4475`)
  282. * `xonsh <https://github.com/xonsh/xonsh/issues/4623>`__
  283. * `Nushell <https://github.com/nushell/nushell/discussions/12065>`__: Set ``$env.config.shell_integration = true`` in your ``config.nu`` to enable it.
  284. Notes for shell developers
  285. -----------------------------
  286. The protocol used for marking the prompt is very simple. You should consider
  287. adding it to your shell as a builtin. Many modern terminals make use of it, for
  288. example: kitty, iTerm2, WezTerm, DomTerm
  289. Just before starting to draw the PS1 prompt send the escape code::
  290. <OSC>133;A<ST>
  291. Just before starting to draw the PS2 prompt send the escape code::
  292. <OSC>133;A;k=s<ST>
  293. Just before running a command/program, send the escape code::
  294. <OSC>133;C<ST>
  295. Here ``<OSC>`` is the bytes ``0x1b 0x5d`` and ``<ST>`` is the bytes ``0x1b
  296. 0x5c``. This is exactly what is needed for shell integration in kitty. For the
  297. full protocol, that also marks the command region, see `the iTerm2 docs
  298. <https://iterm2.com/documentation-escape-codes.html>`_.