launch.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. The :command:`launch` command
  2. --------------------------------
  3. .. program:: launch
  4. |kitty| has a :code:`launch` action that can be used to run arbitrary programs
  5. in new windows/tabs. It can be mapped to user defined shortcuts in
  6. :file:`kitty.conf`. It is very powerful and allows sending the contents of the
  7. current window to the launched program, as well as many other options.
  8. In the simplest form, you can use it to open a new kitty window running the
  9. shell, as shown below::
  10. map f1 launch
  11. To run a different program simply pass the command line as arguments to launch::
  12. map f1 launch vim path/to/some/file
  13. To open a new window with the same working directory as the currently active
  14. window::
  15. map f1 launch --cwd=current
  16. To open the new window in a new tab::
  17. map f1 launch --type=tab
  18. To run multiple commands in a shell, use::
  19. map f1 launch sh -c "ls && exec zsh"
  20. To pass the contents of the current screen and scrollback to the started
  21. process::
  22. map f1 launch --stdin-source=@screen_scrollback less
  23. There are many more powerful options, refer to the complete list below.
  24. .. note::
  25. To avoid duplicating launch actions with frequently used parameters, you can
  26. use :opt:`action_alias` to define launch action aliases. For example::
  27. action_alias launch_tab launch --cwd=current --type=tab
  28. map f1 launch_tab vim
  29. map f2 launch_tab emacs
  30. The :kbd:`F1` key will now open :program:`vim` in a new tab with the current
  31. windows working directory.
  32. The piping environment
  33. --------------------------
  34. When using :option:`launch --stdin-source`, the program to which the data is
  35. piped has a special environment variable declared, :envvar:`KITTY_PIPE_DATA`
  36. whose contents are::
  37. KITTY_PIPE_DATA={scrolled_by}:{cursor_x},{cursor_y}:{lines},{columns}
  38. where ``scrolled_by`` is the number of lines kitty is currently scrolled by,
  39. ``cursor_(x|y)`` is the position of the cursor on the screen with ``(1,1)``
  40. being the top left corner and ``{lines},{columns}`` being the number of rows
  41. and columns of the screen.
  42. Special arguments
  43. -------------------
  44. There are a few special placeholder arguments that can be specified as part of
  45. the command line:
  46. ``@selection``
  47. Replaced by the currently selected text.
  48. ``@active-kitty-window-id``
  49. Replaced by the id of the currently active kitty window.
  50. ``@line-count``
  51. Replaced by the number of lines in STDIN. Only present when passing some
  52. data to STDIN.
  53. ``@input-line-number``
  54. Replaced by the number of lines a pager should scroll to match the current
  55. scroll position in kitty. See :opt:`scrollback_pager` for details.
  56. ``@scrolled-by``
  57. Replaced by the number of lines kitty is currently scrolled by.
  58. ``@cursor-x``
  59. Replaced by the current cursor x position with 1 being the leftmost cell.
  60. ``@cursor-y``
  61. Replaced by the current cursor y position with 1 being the topmost cell.
  62. ``@first-line-on-screen``
  63. Replaced by the first line on screen. Can be used for pager positioning.
  64. ``@last-line-on-screen``
  65. Replaced by the last line on screen. Can be used for pager positioning.
  66. For example::
  67. map f1 launch my-program @active-kitty-window-id
  68. .. _watchers:
  69. Watching launched windows
  70. ---------------------------
  71. The :option:`launch --watcher` option allows you to specify Python functions
  72. that will be called at specific events, such as when the window is resized or
  73. closed. Note that you can also specify watchers that are loaded for all windows,
  74. via :opt:`watcher`. To create a watcher, specify the path to a Python module
  75. that specifies callback functions for the events you are interested in, for
  76. create :file:`~/.config/kitty/mywatcher.py` and use :option:`launch --watcher` = :file:`mywatcher.py`:
  77. .. code-block:: python
  78. # ~/.config/kitty/mywatcher.py
  79. from typing import Any
  80. from kitty.boss import Boss
  81. from kitty.window import Window
  82. def on_load(boss: Boss, data: dict[str, Any]) -> None:
  83. # This is a special function that is called just once when this watcher
  84. # module is first loaded, can be used to perform any initializztion/one
  85. # time setup. Any exceptions in this function are printed to kitty's
  86. # STDERR but otherwise ignored.
  87. ...
  88. def on_resize(boss: Boss, window: Window, data: dict[str, Any]) -> None:
  89. # Here data will contain old_geometry and new_geometry
  90. # Note that resize is also called the first time a window is created
  91. # which can be detected as old_geometry will have all zero values, in
  92. # particular, old_geometry.xnum and old_geometry.ynum will be zero.
  93. ...
  94. def on_focus_change(boss: Boss, window: Window, data: dict[str, Any])-> None:
  95. # Here data will contain focused
  96. ...
  97. def on_close(boss: Boss, window: Window, data: dict[str, Any])-> None:
  98. # called when window is closed, typically when the program running in
  99. # it exits
  100. ...
  101. def on_set_user_var(boss: Boss, window: Window, data: dict[str, Any]) -> None:
  102. # called when a "user variable" is set or deleted on a window. Here
  103. # data will contain key and value
  104. ...
  105. def on_title_change(boss: Boss, window: Window, data: dict[str, Any]) -> None:
  106. # called when the window title is changed on a window. Here
  107. # data will contain title and from_child. from_child will be True
  108. # when a title change was requested via escape code from the program
  109. # running in the terminal
  110. ...
  111. def on_cmd_startstop(boss: Boss, window: Window, data: dict[str, Any]) -> None:
  112. # called when the shell starts/stops executing a command. Here
  113. # data will contain is_start, cmdline and time.
  114. ...
  115. Every callback is passed a reference to the global ``Boss`` object as well as
  116. the ``Window`` object the action is occurring on. The ``data`` object is a dict
  117. that contains event dependent data. You have full access to kitty internals in
  118. the watcher scripts, however kitty internals are not documented/stable so for
  119. most things you are better off using the kitty :doc:`Remote control API </remote-control>`.
  120. Simply call :code:`boss.call_remote_control()`, with the same arguments you
  121. would pass to ``kitten @``. For example:
  122. .. code-block:: python
  123. def on_resize(boss: Boss, window: Window, data: dict[str, Any]) -> None:
  124. # send some text to the resized window
  125. boss.call_remote_control(window, ('send-text', f'--match=id:{window.id}', 'hello world'))
  126. Run, ``kitten @ --help`` in a kitty terminal, to see all the remote control
  127. commands available to you.
  128. Finding executables
  129. -----------------------
  130. When you specify a command to run as just a name rather than an absolute path,
  131. it is searched for in the system-wide :envvar:`PATH` environment variable. Note
  132. that this **may not** be the value of :envvar:`PATH` inside a shell, as shell
  133. startup scripts often change the value of this variable. If it is not found
  134. there, then a system specific list of default paths is searched. If it is still
  135. not found, then your shell is run and the value of :envvar:`PATH` inside the
  136. shell is used.
  137. See :opt:`exe_search_path` for details and how to control this.
  138. Syntax reference
  139. ------------------
  140. .. include:: /generated/launch.rst