hints.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Hints
  2. ==========
  3. .. only:: man
  4. Overview
  5. --------------
  6. |kitty| has a *hints mode* to select and act on arbitrary text snippets
  7. currently visible on the screen. For example, you can press :sc:`open_url`
  8. to choose any URL visible on the screen and then open it using your default web
  9. browser.
  10. .. figure:: ../screenshots/hints_mode.png
  11. :alt: URL hints mode
  12. :align: center
  13. :width: 100%
  14. URL hints mode
  15. Similarly, you can press :sc:`insert_selected_path` to select anything that
  16. looks like a path or filename and then insert it into the terminal, very useful
  17. for picking files from the output of a :program:`git` or :program:`ls` command
  18. and adding them to the command line for the next command.
  19. You can also press :sc:`goto_file_line` to select anything that looks like a
  20. path or filename followed by a colon and a line number and open the file in
  21. your default editor at the specified line number (opening at line number will
  22. work only if your editor supports the +linenum command line syntax or is a
  23. "known" editor). The patterns and editor to be used can be modified using
  24. options passed to the kitten. For example::
  25. map ctrl+g kitten hints --type=linenum --linenum-action=tab nvim +{line} {path}
  26. will open the selected file in a new tab inside `Neovim <https://neovim.io/>`__
  27. when you press :kbd:`Ctrl+G`.
  28. Pressing :sc:`open_selected_hyperlink` will open :term:`hyperlinks`, i.e. a URL
  29. that has been marked as such by the program running in the terminal,
  30. for example, by ``ls --hyperlink=auto``. If :program:`ls` comes with your OS
  31. does not support hyperlink, you may need to install `GNU Coreutils
  32. <https://www.gnu.org/software/coreutils/>`__.
  33. You can also :doc:`customize what actions are taken for different types of URLs
  34. <../open_actions>`.
  35. .. note:: If there are more hints than letters, hints will use multiple
  36. letters. In this case, when you press the first letter, only hints
  37. starting with that letter are displayed. Pressing the second letter will
  38. select that hint or press :kbd:`Enter` or :kbd:`Space` to select the empty
  39. hint.
  40. For mouse lovers, the hints kitten also allows you to click on any matched text to
  41. select it instead of typing the hint character.
  42. The hints kitten is very powerful to see more detailed help on its various
  43. options and modes of operation, see below. You can use these options to
  44. create mappings in :file:`kitty.conf` to select various different text
  45. snippets. See :sc:`insert_selected_path <insert_selected_path>` for examples.
  46. Completely customizing the matching and actions of the kitten
  47. ---------------------------------------------------------------
  48. The hints kitten supports writing simple Python scripts that can be used to
  49. completely customize how it finds matches and what happens when a match is
  50. selected. This allows the hints kitten to provide the user interface, while you
  51. can provide the logic for finding matches and performing actions on them. This
  52. is best illustrated with an example. Create the file :file:`custom-hints.py` in
  53. the :ref:`kitty config directory <confloc>` with the following contents:
  54. .. code-block:: python
  55. import re
  56. def mark(text, args, Mark, extra_cli_args, *a):
  57. # This function is responsible for finding all
  58. # matching text. extra_cli_args are any extra arguments
  59. # passed on the command line when invoking the kitten.
  60. # We mark all individual word for potential selection
  61. for idx, m in enumerate(re.finditer(r'\w+', text)):
  62. start, end = m.span()
  63. mark_text = text[start:end].replace('\n', '').replace('\0', '')
  64. # The empty dictionary below will be available as groupdicts
  65. # in handle_result() and can contain string keys and arbitrary JSON
  66. # serializable values.
  67. yield Mark(idx, start, end, mark_text, {})
  68. def handle_result(args, data, target_window_id, boss, extra_cli_args, *a):
  69. # This function is responsible for performing some
  70. # action on the selected text.
  71. # matches is a list of the selected entries and groupdicts contains
  72. # the arbitrary data associated with each entry in mark() above
  73. matches, groupdicts = [], []
  74. for m, g in zip(data['match'], data['groupdicts']):
  75. if m:
  76. matches.append(m), groupdicts.append(g)
  77. for word, match_data in zip(matches, groupdicts):
  78. # Lookup the word in a dictionary, the open_url function
  79. # will open the provided url in the system browser
  80. boss.open_url(f'https://www.google.com/search?q=define:{word}')
  81. Now run kitty with::
  82. kitty -o 'map f1 kitten hints --customize-processing custom-hints.py'
  83. When you press the :kbd:`F1` key you will be able to select a word to
  84. look it up in the Google dictionary.
  85. .. include:: ../generated/cli-kitten-hints.rst
  86. .. note::
  87. To avoid having to specify the same command line options on every
  88. invocation, you can use the :opt:`action_alias` option in
  89. :file:`kitty.conf`, creating aliases that have common sets of options.
  90. For example::
  91. action_alias myhints kitten hints --alphabet qfjdkslaureitywovmcxzpq1234567890
  92. map f1 myhints --customize-processing custom-hints.py