My configuration files for Debian/Ubuntu applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

how_to_create_plugin.md 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # How to create Tmux plugins
  2. Creating a new plugin is easy.
  3. For demonstration purposes we'll create a simple plugin that lists all
  4. installed TPM plugins. Yes, a plugin that lists plugins :) We'll bind that to
  5. `prefix + T`.
  6. The source code for this example plugin can be found
  7. [here](https://github.com/tmux-plugins/tmux-example-plugin).
  8. ### 1. create a new git project
  9. TPM depends on git for downloading and updating plugins.
  10. To create a new git project:
  11. $ mkdir tmux_my_plugin
  12. $ cd tmux_my_plugin
  13. $ git init
  14. ### 2. create a `*.tmux` plugin run file
  15. When it sources a plugin, TPM executes all `*.tmux` files in your plugins'
  16. directory. That's how plugins are run.
  17. Create a plugin run file in plugin directory:
  18. $ touch my_plugin.tmux
  19. $ chmod u+x my_plugin.tmux
  20. You can have more than one `*.tmux` file, and all will get executed. However, usually
  21. you'll need just one.
  22. ### 3. create a plugin key binding
  23. We want the behavior of the plugin to trigger when a user hits `prefix + T`.
  24. Key `T` is chosen because:
  25. - it's "kind of" a mnemonic for `TPM`
  26. - the key is not used by Tmux natively. Tmux man page, KEY BINDINGS section
  27. contains a list of all the bindings Tmux uses. There's plenty of unused keys
  28. and we don't want to override any of Tmux default key bindings.
  29. Open the plugin run file in your favorite text editor:
  30. $ vim my_plugin.tmux
  31. # or
  32. $ subl my_plugin.tmux
  33. Put the following content in the file:
  34. #!/usr/bin/env bash
  35. CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  36. tmux bind-key T run-shell "$CURRENT_DIR/scripts/tmux_list_plugins.sh"
  37. As you can see, plugin run file is a simple bash script that sets up the binding.
  38. When pressed, `prefix + T` will execute another shell script:
  39. `tmux_list_plugins.sh`. That script should be in `scripts/` directory -
  40. relative to the plugin run file.
  41. ### 4. listing plugins
  42. Now that we have the binding, let's create a script that's invoked with
  43. `prefix + T`.
  44. $ mkdir scripts
  45. $ touch scripts/tmux_list_plugins.sh
  46. $ chmod u+x scripts/tmux_list_plugins.sh
  47. And here's the script content:
  48. #!/usr/bin/env bash
  49. # fetching the directory where plugins are installed
  50. plugin_path="$(tmux show-env -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)"
  51. # listing installed plugins
  52. ls -1 "$plugin_path"
  53. ### 5. try it out
  54. To see if this works, execute the plugin run file:
  55. $ ./my_plugin.tmux
  56. That should set up the key binding. Now hit `prefix + T` and see if it works.
  57. ### 6. publish the plugin
  58. When everything is ready, push the plugin to an online git repository,
  59. preferably Github.
  60. Other users can install your plugin by just adding plugin git URL to the
  61. `@plugin` list in their `.tmux.conf`.
  62. If the plugin is on Github, your users will be able to use the shorthand of
  63. `github_username/repository`.
  64. ### Conclusion
  65. Hopefully, that was easy. As you can see, it's mostly shell scripting.
  66. You can use other scripting languages (ruby, python etc) but plain old shell
  67. is preferred because of portability.