shell-integration.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. Note that for the fish shell this does not take effect, since fish always
  65. marks prompts.
  66. no-complete
  67. Turn off completion for the kitty command.
  68. Note that for the fish shell this does not take effect, since fish already
  69. comes with a kitty completion script.
  70. no-sudo
  71. Do not alias :program:`sudo` to ensure the kitty terminfo files are
  72. available in the sudo environment. This is needed if you have sudo
  73. configured to disable setting of environment variables on the command line.
  74. By default, if sudo is configured to allow all commands for the current
  75. user, setting of environment variables at the command line is also allowed.
  76. Only if commands are restricted is this needed.
  77. More ways to browse command output
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. You can add further key and mouse bindings to browse the output of commands
  80. easily. For example to select the output of a command by right clicking the
  81. mouse on the output, define the following in :file:`kitty.conf`:
  82. .. code:: conf
  83. mouse_map right press ungrabbed mouse_select_command_output
  84. Now, when you right click on the output, the entire output is selected, ready
  85. to be copied.
  86. The feature to jump to previous prompts (
  87. :sc:`scroll_to_previous_prompt` and :sc:`scroll_to_next_prompt`) and mouse
  88. actions (:ac:`mouse_select_command_output` and :ac:`mouse_show_command_output`)
  89. can be integrated with browsing command output as well. For example, define the
  90. following mapping in :file:`kitty.conf`:
  91. .. code:: conf
  92. map f1 show_last_visited_command_output
  93. Now, pressing :kbd:`F1` will cause the output of the last jumped to command or
  94. the last mouse clicked command output to be opened in a pager for easy browsing.
  95. In addition, You can define shortcut to get the first command output on screen.
  96. For example, define the following in :file:`kitty.conf`:
  97. .. code:: conf
  98. map f1 show_first_command_output_on_screen
  99. Now, pressing :kbd:`F1` will cause the output of the first command output on
  100. screen to be opened in a pager.
  101. You can also add shortcut to scroll to the last jumped position. For example,
  102. define the following in :file:`kitty.conf`:
  103. .. code:: conf
  104. map f1 scroll_to_prompt 0
  105. How it works
  106. -----------------
  107. At startup, kitty detects if the shell you have configured (either system wide
  108. or the :opt:`shell` option in :file:`kitty.conf`) is a supported shell. If so,
  109. kitty injects some shell specific code into the shell, to enable shell
  110. integration. How it does so varies for different shells.
  111. .. tab:: zsh
  112. For zsh, kitty sets the :envvar:`ZDOTDIR` environment variable to make zsh
  113. load kitty's :file:`.zshenv` which restores the original value of
  114. :envvar:`ZDOTDIR` and sources the original :file:`.zshenv`. It then loads
  115. the shell integration code. The remainder of zsh's startup process proceeds
  116. as normal.
  117. .. tab:: fish
  118. For fish, to make it automatically load the integration code provided by
  119. kitty, the integration script directory path is prepended to the
  120. :envvar:`XDG_DATA_DIRS` environment variable. This is only applied to the
  121. fish process and will be cleaned up by the integration script after startup.
  122. No files are added or modified.
  123. .. tab:: bash
  124. For bash, kitty starts bash in POSIX mode, using the environment variable
  125. :envvar:`ENV` to load the shell integration script. This prevents bash from
  126. loading any startup files itself. The loading of the startup files is done
  127. by the integration script, after disabling POSIX mode. From the perspective
  128. of those scripts there should be no difference to running vanilla bash.
  129. Then, when launching the shell, kitty sets the environment variable
  130. :envvar:`KITTY_SHELL_INTEGRATION` to the value of the :opt:`shell_integration`
  131. option. The shell integration code reads the environment variable, turns on the
  132. specified integration functionality and then unsets the variable so as to not
  133. pollute the system.
  134. The actual shell integration code uses hooks provided by each shell to send
  135. special escape codes to kitty, to perform the various tasks. You can see the
  136. code used for each shell below:
  137. .. raw:: html
  138. <details>
  139. <summary>Click to toggle shell integration code</summary>
  140. .. tab:: zsh
  141. .. literalinclude:: ../shell-integration/zsh/kitty-integration
  142. :language: zsh
  143. .. tab:: fish
  144. .. literalinclude:: ../shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish
  145. :language: fish
  146. :force:
  147. .. tab:: bash
  148. .. literalinclude:: ../shell-integration/bash/kitty.bash
  149. :language: bash
  150. .. raw:: html
  151. </details>
  152. Shell integration over SSH
  153. ----------------------------
  154. The easiest way to have shell integration work when SSHing into remote systems
  155. is to use the :doc:`ssh kitten <kittens/ssh>`. Simply run::
  156. kitten ssh hostname
  157. And, by magic, you will be logged into the remote system with fully functional
  158. shell integration. Alternately, you can :ref:`setup shell integration manually
  159. <manual_shell_integration>`, by copying the kitty shell integration scripts to
  160. the remote server and editing the shell rc files there, as described below.
  161. Shell integration in a container
  162. ----------------------------------
  163. Install the kitten `standalone binary
  164. <https://github.com/kovidgoyal/kitty/releases/latest/download/kitten-linux-amd64>`__ in the container
  165. somewhere in the PATH, then you can log into the container with:
  166. .. code-block:: sh
  167. docker exec -ti container-id kitten run-shell --shell=/path/to/your/shell/in/the/container
  168. The kitten will even take care of making the kitty terminfo database available
  169. in the container automatically.
  170. .. _clone_shell:
  171. Clone the current shell into a new window
  172. -----------------------------------------------
  173. You can clone the current shell into a new kitty window by simply running the
  174. :command:`clone-in-kitty` command, for example:
  175. .. code-block:: sh
  176. clone-in-kitty
  177. clone-in-kitty --type=tab
  178. clone-in-kitty --title "I am a clone"
  179. This will open a new window running a new shell instance but with all
  180. environment variables and the current working directory copied. This even works
  181. over SSH when using :doc:`kittens/ssh`.
  182. The :command:`clone-in-kitty` command takes almost all the same arguments as the
  183. :doc:`launch <launch>` command, so you can open a new tab instead or a new OS
  184. window, etc. Arguments of launch that can cause code execution or that don't
  185. make sense when cloning are ignored. Most prominently, the following options are
  186. ignored: :option:`--allow-remote-control <launch --allow-remote-control>`,
  187. :option:`--copy-cmdline <launch --copy-cmdline>`, :option:`--copy-env <launch
  188. --copy-env>`, :option:`--stdin-source <launch --stdin-source>`,
  189. :option:`--marker <launch --marker>` and :option:`--watcher <launch --watcher>`.
  190. :command:`clone-in-kitty` can be configured to source arbitrary code in the
  191. cloned window using environment variables. It will automatically clone virtual
  192. environments created by the :link:`Python venv module
  193. <https://docs.python.org/3/library/venv.html>` or :link:`Conda
  194. <https://conda.io/>`. In addition, setting the
  195. env var :envvar:`KITTY_CLONE_SOURCE_CODE` to some shell code will cause that
  196. code to be run in the cloned window with :code:`eval`. Similarly, setting
  197. :envvar:`KITTY_CLONE_SOURCE_PATH` to the path of a file will cause that file to
  198. be sourced in the cloned window. This can be controlled by
  199. :opt:`clone_source_strategies`.
  200. :command:`clone-in-kitty` works by asking the shell to serialize its internal
  201. state (mainly CWD and env vars) and this state is transmitted to kitty and
  202. restored by the shell integration scripts in the cloned window.
  203. .. _edit_file:
  204. Edit files in new kitty windows even over SSH
  205. ------------------------------------------------
  206. .. code-block:: sh
  207. edit-in-kitty myfile.txt
  208. edit-in-kitty --type tab --title "Editing My File" myfile.txt
  209. # open myfile.txt at line 75 (works with vim, neovim, emacs, nano, micro)
  210. edit-in-kitty +75 myfile.txt
  211. The :command:`edit-in-kitty` command allows you to seamlessly edit files
  212. in your default :opt:`editor` in new kitty windows. This works even over
  213. SSH (if you use the :doc:`ssh kitten <kittens/ssh>`), allowing you
  214. to easily edit remote files in your local editor with all its bells and
  215. whistles.
  216. The :command:`edit-in-kitty` command takes almost all the same arguments as the
  217. :doc:`launch <launch>` command, so you can open a new tab instead or a new OS
  218. window, etc. Not all arguments are supported, see the discussion in the
  219. :ref:`clone_shell` section above.
  220. In order to avoid remote code execution, kitty will only execute the configured
  221. editor and pass the file path to edit to it.
  222. .. note:: To edit files using sudo the best method is to set the
  223. :code:`SUDO_EDITOR` environment variable to ``kitten edit-in-kitty`` and
  224. then edit the file using the ``sudoedit`` or ``sudo -e`` commands.
  225. .. _run_shell:
  226. Using shell integration in sub-shells, containers, etc.
  227. -----------------------------------------------------------
  228. .. versionadded:: 0.29.0
  229. To start a sub-shell with shell integration automatically setup, simply run::
  230. kitten run-shell
  231. This will start a sub-shell using the same binary as the currently running
  232. shell, with shell-integration enabled. To start a particular shell use::
  233. kitten run-shell --shell=/bin/bash
  234. To run a command before starting the shell use::
  235. kitten run-shell ls .
  236. This will run ``ls .`` before starting the shell.
  237. This will even work on remote systems where kitty itself is not installed,
  238. provided you use the :doc:`SSH kitten <kittens/ssh>` to connect to the system.
  239. Use ``kitten run-shell --help`` to learn more.
  240. .. _manual_shell_integration:
  241. Manual shell integration
  242. ----------------------------
  243. The automatic shell integration is designed to be minimally intrusive, as such
  244. it won't work for sub-shells, terminal multiplexers, containers, etc.
  245. For such systems, you should either use the :ref:`run-shell <run_shell>` command described above or
  246. setup manual shell integration by adding some code to your shells startup files to load the shell integration script.
  247. First, in :file:`kitty.conf` set:
  248. .. code-block:: conf
  249. shell_integration disabled
  250. Then in your shell's rc file, add the lines:
  251. .. tab:: zsh
  252. .. code-block:: sh
  253. if test -n "$KITTY_INSTALLATION_DIR"; then
  254. export KITTY_SHELL_INTEGRATION="enabled"
  255. autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
  256. kitty-integration
  257. unfunction kitty-integration
  258. fi
  259. .. tab:: fish
  260. .. code-block:: fish
  261. if set -q KITTY_INSTALLATION_DIR
  262. set --global KITTY_SHELL_INTEGRATION enabled
  263. source "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish"
  264. set --prepend fish_complete_path "$KITTY_INSTALLATION_DIR/shell-integration/fish/vendor_completions.d"
  265. end
  266. .. tab:: bash
  267. .. code-block:: sh
  268. if test -n "$KITTY_INSTALLATION_DIR"; then
  269. export KITTY_SHELL_INTEGRATION="enabled"
  270. source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
  271. fi
  272. The value of :envvar:`KITTY_SHELL_INTEGRATION` is the same as that for
  273. :opt:`shell_integration`, except if you want to disable shell integration
  274. completely, in which case simply do not set the
  275. :envvar:`KITTY_SHELL_INTEGRATION` variable at all.
  276. In a container, you will need to install the kitty shell integration scripts
  277. and make sure the :envvar:`KITTY_INSTALLATION_DIR` environment variable is set
  278. to point to the location of the scripts.
  279. Integration with other shells
  280. -------------------------------
  281. There exist third-party integrations to use these features for various other
  282. shells:
  283. * Jupyter console and IPython via a patch (:iss:`4475`)
  284. * `xonsh <https://github.com/xonsh/xonsh/issues/4623>`__
  285. * `Nushell <https://github.com/nushell/nushell/discussions/12065>`__: Set ``$env.config.shell_integration = true`` in your ``config.nu`` to enable it.
  286. Notes for shell developers
  287. -----------------------------
  288. The protocol used for marking the prompt is very simple. You should consider
  289. adding it to your shell as a builtin. Many modern terminals make use of it, for
  290. example: kitty, iTerm2, WezTerm, DomTerm
  291. Just before starting to draw the PS1 prompt send the escape code::
  292. <OSC>133;A<ST>
  293. Just before starting to draw the PS2 prompt send the escape code::
  294. <OSC>133;A;k=s<ST>
  295. Just before running a command/program, send the escape code::
  296. <OSC>133;C<ST>
  297. Optionally, when a command is finished its "exit status" can be reported as::
  298. <OSC>133;D;exit status as base 10 integer<ST>
  299. Here ``<OSC>`` is the bytes ``0x1b 0x5d`` and ``<ST>`` is the bytes ``0x1b
  300. 0x5c``. This is exactly what is needed for shell integration in kitty. For the
  301. full protocol, that also marks the command region, see `the iTerm2 docs
  302. <https://iterm2.com/documentation-escape-codes.html>`_.
  303. kitty additionally supports several extra fields for the ``<OSC>133;A`` command
  304. to control its behavior, separated by semi-colons. They are::
  305. redraw=0 - this tells kitty that the shell will not redraw the prompt on
  306. resize so it should not erase it
  307. special_key=1 - this tells kitty to use a special key instead of arrow keys
  308. to move the cursor on mouse click. Useful if arrow keys have side-effects
  309. like triggering auto complete. The shell integration script then binds the
  310. special key, as needed.
  311. k=s - this tells kitty that the secondary (PS2) prompt is starting at the
  312. current line.
  313. click_events=1 - this tells kitty that the shell is capable of handling
  314. mouse click events. kitty will thus send a click event to the shell when
  315. the user clicks somewhere in the prompt. The shell can then move the cursor
  316. to that position or perform some other appropriate action. Without this,
  317. kitty will instead generate a number of fake key events to move the cursor
  318. to the clicked location, which is not fully robust.
  319. kitty also optionally supports sending the cmdline going to be executed with ``<OSC>133;C`` as::
  320. <OSC>133;C;cmdline=cmdline encoded by %q<ST>
  321. or
  322. <OSC>133;C;cmdline_url=cmdline as UTF-8 URL %-escaped text<ST>
  323. Here, *encoded by %q* means the encoding produced by the %q format to printf in
  324. bash and similar shells. Which is basically shell escaping with the addition of
  325. using `ANSI C quoting
  326. <https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html#ANSI_002dC-Quoting>`__
  327. for control characters (``$''`` quoting).