launch.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. Simply specify the path to a Python module that specifies callback
  74. functions for the events you are interested in, for example:
  75. .. code-block:: python
  76. from typing import Any, Dict
  77. from kitty.boss import Boss
  78. from kitty.window import Window
  79. def on_resize(boss: Boss, window: Window, data: Dict[str, Any]) -> None:
  80. # Here data will contain old_geometry and new_geometry
  81. def on_focus_change(boss: Boss, window: Window, data: Dict[str, Any])-> None:
  82. # Here data will contain focused
  83. def on_close(boss: Boss, window: Window, data: Dict[str, Any])-> None:
  84. # called when window is closed, typically when the program running in
  85. # it exits
  86. def on_set_user_var(boss: Boss, window: Window, data: Dict[str, Any]) -> None:
  87. # called when a "user variable" is set or deleted on a window. Here
  88. # data will contain key and value
  89. def on_title_change(boss: Boss, window: Window, data: Dict[str, Any]) -> None:
  90. # called when the window title is changed on a window. Here
  91. # data will contain title and from_child. from_child will be True
  92. # when a title change was requested via escape code from the program
  93. # running in the terminal
  94. def on_cmd_startstop(boss: Boss, window: Window, data: Dict[str, Any]) -> None:
  95. # called when the shell starts/stops executing a command. Here
  96. # data will contain is_start, cmdline and time.
  97. Every callback is passed a reference to the global ``Boss`` object as well as
  98. the ``Window`` object the action is occurring on. The ``data`` object is a dict
  99. that contains event dependent data. Some useful methods and attributes for the
  100. ``Window`` object are: ``as_text(as_ans=False, add_history=False,
  101. add_wrap_markers=False, alternate_screen=False)`` with which you can get the
  102. contents of the window and its scrollback buffer. Similarly,
  103. ``window.child.pid`` is the PID of the processes that was launched
  104. in the window and ``window.id`` is the internal kitty ``id`` of the window.
  105. Finding executables
  106. -----------------------
  107. When you specify a command to run as just a name rather than an absolute path,
  108. it is searched for in the system-wide :envvar:`PATH` environment variable. Note
  109. that this **may not** be the value of :envvar:`PATH` inside a shell, as shell
  110. startup scripts often change the value of this variable. If it is not found
  111. there, then a system specific list of default paths is searched. If it is still
  112. not found, then your shell is run and the value of :envvar:`PATH` inside the
  113. shell is used.
  114. See :opt:`exe_search_path` for details and how to control this.
  115. Syntax reference
  116. ------------------
  117. .. include:: /generated/launch.rst