open_actions.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. Scripting the mouse click
  2. ======================================================
  3. |kitty| has support for :term:`terminal hyperlinks <hyperlinks>`. These are
  4. generated by many terminal programs, such as ``ls``, ``gcc``, ``systemd``,
  5. :ref:`tool_mdcat`, etc. You can customize exactly what happens when clicking on
  6. these hyperlinks in |kitty|.
  7. You can tell kitty to take arbitrarily many, complex actions when a link is
  8. clicked. Let us illustrate with some examples, first. Create the file
  9. :file:`~/.config/kitty/open-actions.conf` with the following:
  10. .. code:: conf
  11. # Open any image in the full kitty window by clicking on it
  12. protocol file
  13. mime image/*
  14. action launch --type=overlay kitten icat --hold -- ${FILE_PATH}
  15. Now, run ``ls --hyperlink=auto`` in kitty and click on the filename of an
  16. image, holding down :kbd:`ctrl+shift`. It will be opened over the current
  17. window. Press any key to close it.
  18. .. note::
  19. The :program:`ls` comes with macOS does not support hyperlink, you need to
  20. install `GNU Coreutils <https://www.gnu.org/software/coreutils/>`__. If you
  21. install it via `Homebrew <https://formulae.brew.sh/formula/coreutils>`__, it
  22. will be :program:`gls`.
  23. Each entry in :file:`open-actions.conf` consists of one or more
  24. :ref:`matching_criteria`, such as ``protocol`` and ``mime`` and one or more
  25. ``action`` entries. In the example above kitty uses the :doc:`launch <launch>`
  26. action which can be used to run external programs. Entries are separated by
  27. blank lines.
  28. Actions are very powerful, anything that you can map to a key combination in
  29. :file:`kitty.conf` can be used as an action. You can specify more than one
  30. action per entry if you like, for example:
  31. .. code:: conf
  32. # Tail a log file (*.log) in a new OS Window and reduce its font size
  33. protocol file
  34. ext log
  35. action launch --title ${FILE} --type=os-window tail -f -- ${FILE_PATH}
  36. action change_font_size current -2
  37. In the action specification you can expand environment variables, as shown in
  38. the examples above. In addition to regular environment variables, there are
  39. some special variables, documented below:
  40. ``URL``
  41. The full URL being opened
  42. ``FILE_PATH``
  43. The path portion of the URL (unquoted)
  44. ``FILE``
  45. The file portion of the path of the URL (unquoted)
  46. ``FRAGMENT``
  47. The fragment (unquoted), if any of the URL or the empty string.
  48. ``NETLOC``
  49. The net location aka hostname (unquoted), if any of the URL or the empty string.
  50. ``URL_PATH``
  51. The path, query and fragment portions of the URL, without any
  52. unquoting.
  53. ``EDITOR``
  54. The terminal based text editor. The configured :opt:`editor` in
  55. :file:`kitty.conf` is preferred.
  56. ``SHELL``
  57. The path to the shell. The configured :opt:`shell` in :file:`kitty.conf` is
  58. preferred, without arguments.
  59. .. note::
  60. You can use the :opt:`action_alias` option just as in :file:`kitty.conf` to
  61. define aliases for frequently used actions.
  62. .. _matching_criteria:
  63. Matching criteria
  64. ------------------
  65. An entry in :file:`open-actions.conf` must have one or more matching criteria.
  66. URLs that match all criteria for an entry will trigger that entry's actions.
  67. Processing stops at the first matching entry, so put more specific matching
  68. criteria at the start of the list. Entries in the file are separated by blank
  69. lines. The various available criteria are:
  70. ``protocol``
  71. A comma separated list of protocols, for example: ``http, https``. If
  72. absent, there is no constraint on protocol.
  73. ``url``
  74. A regular expression that must match against the entire (unquoted) URL
  75. ``fragment_matches``
  76. A regular expression that must match against the fragment (part after #) in
  77. the URL
  78. ``mime``
  79. A comma separated list of MIME types, for example: ``text/*, image/*,
  80. application/pdf``. You can add MIME types to kitty by creating a file named
  81. :file:`mime.types` in the :ref:`kitty configuration directory <confloc>`.
  82. Useful if your system MIME database does not have definitions you need. This
  83. file is in the standard format of one definition per line, like:
  84. ``text/plain rst md``. Note that the MIME type for directories is
  85. ``inode/directory``. MIME types are detected based on file extension, not
  86. file contents.
  87. ``ext``
  88. A comma separated list of file extensions, for example: ``jpeg, tar.gz``
  89. ``file``
  90. A shell glob pattern that must match the filename, for example:
  91. ``image-??.png``
  92. .. _launch_actions:
  93. Scripting the opening of files with kitty on macOS
  94. -------------------------------------------------------
  95. On macOS you can use :guilabel:`Open With` in Finder or drag and drop files and
  96. URLs onto the kitty dock icon to open them with kitty. The default actions are:
  97. * Open text files in your editor and images using the icat kitten.
  98. * Run shell scripts in a shell
  99. * Open SSH urls using the ssh command
  100. These actions can also be executed from the command line by running::
  101. kitty +open file_or_url another_url ...
  102. # macOS only
  103. open -a kitty.app file_or_url another_url ...
  104. Since macOS lacks an official interface to set default URL scheme handlers,
  105. kitty has a command you can use for it. The first argument for is the URL
  106. scheme, and the second optional argument is the bundle id of the app, which
  107. defaults to kitty, if not specified. For example:
  108. .. code-block:: sh
  109. # Set kitty as the handler for ssh:// URLs
  110. kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; import sys; cocoa_set_url_handler(*sys.argv[1:]); print("OK")' ssh
  111. # Set someapp as the handler for xyz:// URLs
  112. kitty +runpy 'from kitty.fast_data_types import cocoa_set_url_handler; import sys; cocoa_set_url_handler(*sys.argv[1:]); print("OK")' xyz someapp.bundle.identifier
  113. You can customize these actions by creating a :file:`launch-actions.conf` file
  114. in the :ref:`kitty config directory <confloc>`, just like the
  115. :file:`open-actions.conf` file above. For example:
  116. .. literalinclude:: ../kitty/open_actions.py
  117. :language: conf
  118. :start-at: # Open script files
  119. :end-before: '''.splitlines()))