feat: tmux stuff

This commit is contained in:
2025-04-07 14:42:22 +01:00
parent 58c03a3a42
commit 229e3a4e11
68 changed files with 3513 additions and 3 deletions

View File

@ -0,0 +1,9 @@
# Force text files to have unix eols, so Windows/Cygwin does not break them
*.* eol=lf
# These files are unfortunately not recognized as text files so
# explicitly listing them here
tpm eol=lf
bin/* eol=lf
bindings/* eol=lf
tests/* eol=lf

4
config/tmux/plugins/tpm/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
**/.vagrant/
run_tests
tests/run_tests_in_isolation
tests/helpers/helpers.sh

3
config/tmux/plugins/tpm/.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "lib/tmux-test"]
path = lib/tmux-test
url = https://github.com/tmux-plugins/tmux-test.git

View File

@ -0,0 +1,19 @@
# generic packages and tmux
before_install:
- sudo apt-get update
- sudo apt-get install -y git-core expect
- sudo apt-get install -y python-software-properties software-properties-common
- sudo apt-get install -y libevent-dev libncurses-dev
- git clone https://github.com/tmux/tmux.git
- cd tmux
- git checkout 2.0
- sh autogen.sh
- ./configure && make && sudo make install
install:
- git fetch --unshallow --recurse-submodules || git fetch --recurse-submodules
# manual `git clone` required for testing `tmux-test` plugin itself
- git clone https://github.com/tmux-plugins/tmux-test lib/tmux-test; true
- lib/tmux-test/setup
script: ./tests/run_tests_in_isolation

View File

@ -0,0 +1,86 @@
# Changelog
### master
### v3.1.0, 2023-01-03
- upgrade to new version of `tmux-test`
- bug: when using `emacs` copy mode, Enter does not quit screen after tpm
installation/update. Fix by making `Escape` the key for emacs mode.
- add a doc with troubleshooting instructions
- add `.gitattributes` file that forces linefeed characters (classic `\n`) as
line endings - helps with misconfigured git on windows/cygwin
- readme update: announce Cygwin support
- un-deprecate old plugin definition syntax: `set -g @tpm_plugins`
- More stuff, check `git log`.
### v3.0.0, 2015-08-03
- refactor `shared_set_tpm_path_constant` function
- move all instructions to `docs/` dir
- add `bin/install_plugins` cli executable script
- improved test runner function
- switch to using [tmux-test](https://github.com/tmux-plugins/tmux-test)
framework
- add `bin/update_plugins` cli executable script
- refactor test `expect` scripts, make them simpler and ensure they properly
assert expectations
- refactor code that sets 'TMUX_PLUGIN_MANAGER_PATH' global env var
- stop using global variable for 'tpm path'
- support defining plugins via `set -g @plugin` in sourced files as well
### v2.0.0, 2015-07-07
- enable overriding default key bindings
- start using `C-c` to clear screen
- add uninstall/clean procedure and keybinding (prefix+alt+u) (@chilicuil)
- add new `set @plugin 'repo'` plugin definition syntax (@chilicuil)
- revert back to using `-g` flag in new plugin definition syntax
- permit leading whitespace with new plugin definition syntax (thanks @chilicuil)
- make sure `TMUX_PLUGIN_MANAGER_PATH` always has trailng slash
- ensure old/deprecated plugin syntax `set -g @tpm_plugins` works alongside new
`set -g @plugin` syntax
### v1.2.2, 2015-02-08
- set GIT_TERMINAL_PROMPT=0 when doing `git clone`, `pull` or `submodule update`
to ensure git does not prompt for username/password in any case
### v1.2.1, 2014-11-21
- change the way plugin name is expanded. It now uses the http username
and password by default, like this: `https://git::@github.com/`. This prevents
username and password prompt (and subsequently tmux install hanging) with old
git versions. Fixes #7.
### v1.2.0, 2014-11-20
- refactor tests so they can be used on travis
- add travis.yml, add travis badge to the readme
### v1.1.0, 2014-11-19
- if the plugin is not downloaded do not source it
- remove `PLUGINS.md`, an obsolete list of plugins
- update readme with instructions about uninstalling plugins
- tilde char and `$HOME` in `TMUX_SHARED_MANAGER_PATH` couldn't be used because
they are just plain strings. Fixing the problem by manually expanding them.
- bugfix: fragile `*.tmux` file globbing (@majutsushi)
### v1.0.0, 2014-08-05
- update readme because of github organization change to
[tmux-plugins](https://github.com/tmux-plugins)
- update tests to pass
- update README to suggest different first plugin
- update list of plugins in the README
- remove README 'about' section
- move key binding to the main file. Delete `key_binding.sh`.
- rename `display_message` -> `echo_message`
- installing plugins installs just new plugins. Already installed plugins aren't
updated.
- add 'update plugin' binding and functionality
- add test for updating a plugin
### v0.0.2, 2014-07-17
- run all *.tmux plugin files as executables
- fix all redirects to /dev/null
- fix bug: TPM shared path is created before sync (cloning plugins from github
is done)
- add test suite running in Vagrant
- add Tmux version check. `TPM` won't run if Tmux version is less than 1.9.
### v0.0.1, 2014-05-21
- get TPM up and running

View File

@ -0,0 +1,2 @@
Instructions moved to
[docs/how_to_create_plugin.md](docs/how_to_create_plugin.md).

View File

@ -0,0 +1,20 @@
MIT license
Copyright (C) 2014 Bruno Sutic
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,101 @@
# Tmux Plugin Manager
[![Build Status](https://travis-ci.org/tmux-plugins/tpm.svg?branch=master)](https://travis-ci.org/tmux-plugins/tpm)
Installs and loads `tmux` plugins.
Tested and working on Linux, OSX, and Cygwin.
See list of plugins [here](https://github.com/tmux-plugins/list).
### Installation
Requirements: `tmux` version 1.9 (or higher), `git`, `bash`.
Clone TPM:
```bash
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
```
Put this at the bottom of `~/.tmux.conf` (`$XDG_CONFIG_HOME/tmux/tmux.conf`
works too):
```bash
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'github_username/plugin_name#branch'
# set -g @plugin 'git@github.com:user/plugin'
# set -g @plugin 'git@bitbucket.com:user/plugin'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
```
Reload TMUX environment so TPM is sourced:
```bash
# type this in terminal if tmux is already running
tmux source ~/.tmux.conf
```
That's it!
### Installing plugins
1. Add new plugin to `~/.tmux.conf` with `set -g @plugin '...'`
2. Press `prefix` + <kbd>I</kbd> (capital i, as in **I**nstall) to fetch the plugin.
You're good to go! The plugin was cloned to `~/.tmux/plugins/` dir and sourced.
### Uninstalling plugins
1. Remove (or comment out) plugin from the list.
2. Press `prefix` + <kbd>alt</kbd> + <kbd>u</kbd> (lowercase u as in **u**ninstall) to remove the plugin.
All the plugins are installed to `~/.tmux/plugins/` so alternatively you can
find plugin directory there and remove it.
### Key bindings
`prefix` + <kbd>I</kbd>
- Installs new plugins from GitHub or any other git repository
- Refreshes TMUX environment
`prefix` + <kbd>U</kbd>
- updates plugin(s)
`prefix` + <kbd>alt</kbd> + <kbd>u</kbd>
- remove/uninstall plugins not on the plugin list
### Docs
- [Help, tpm not working](docs/tpm_not_working.md) - problem solutions
More advanced features and instructions, regular users probably do not need
this:
- [How to create a plugin](docs/how_to_create_plugin.md). It's easy.
- [Managing plugins via the command line](docs/managing_plugins_via_cmd_line.md)
- [Changing plugins install dir](docs/changing_plugins_install_dir.md)
- [Automatic TPM installation on a new machine](docs/automatic_tpm_installation.md)
### Tests
Tests for this project run on [Travis CI](https://travis-ci.org/tmux-plugins/tpm).
When run locally, [vagrant](https://www.vagrantup.com/) is required.
Run tests with:
```bash
# within project directory
./run_tests
```
### License
[MIT](LICENSE.md)

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Script intended for use via the command line.
#
# `.tmux.conf` needs to be set for TPM. Tmux has to be installed on the system,
# but does not need to be started in order to run this script.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
main() {
"$SCRIPTS_DIR/clean_plugins.sh" # has correct exit code
}
main

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Script intended for use via the command line.
#
# `.tmux.conf` needs to be set for TPM. Tmux has to be installed on the system,
# but does not need to be started in order to run this script.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
main() {
"$SCRIPTS_DIR/install_plugins.sh" # has correct exit code
}
main

View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Script intended for use via the command line.
#
# `.tmux.conf` needs to be set for TPM. Tmux has to be installed on the system,
# but does not need to be started in order to run this script.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
PROGRAM_NAME="$0"
if [ $# -eq 0 ]; then
echo "usage:"
echo " $PROGRAM_NAME all update all plugins"
echo " $PROGRAM_NAME tmux-foo update plugin 'tmux-foo'"
echo " $PROGRAM_NAME tmux-bar tmux-baz update multiple plugins"
exit 1
fi
main() {
"$SCRIPTS_DIR/update_plugin.sh" --shell-echo "$*" # has correct exit code
}
main "$*"

View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Tmux key-binding script.
# Scripts intended to be used via the command line are in `bin/` directory.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
HELPERS_DIR="$SCRIPTS_DIR/helpers"
source "$HELPERS_DIR/tmux_echo_functions.sh"
source "$HELPERS_DIR/tmux_utils.sh"
main() {
reload_tmux_environment
"$SCRIPTS_DIR/clean_plugins.sh" --tmux-echo >/dev/null 2>&1
reload_tmux_environment
end_message
}
main

View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Tmux key-binding script.
# Scripts intended to be used via the command line are in `bin/` directory.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
HELPERS_DIR="$SCRIPTS_DIR/helpers"
source "$HELPERS_DIR/tmux_echo_functions.sh"
source "$HELPERS_DIR/tmux_utils.sh"
main() {
reload_tmux_environment
"$SCRIPTS_DIR/install_plugins.sh" --tmux-echo >/dev/null 2>&1
reload_tmux_environment
end_message
}
main

View File

@ -0,0 +1,49 @@
#!/usr/bin/env bash
# Tmux key-binding script.
# Scripts intended to be used via the command line are in `bin/` directory.
# This script:
# - shows a list of installed plugins
# - starts a prompt to enter the name of the plugin that will be updated
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/../scripts"
HELPERS_DIR="$SCRIPTS_DIR/helpers"
source "$HELPERS_DIR/plugin_functions.sh"
source "$HELPERS_DIR/tmux_echo_functions.sh"
source "$HELPERS_DIR/tmux_utils.sh"
display_plugin_update_list() {
local plugins="$(tpm_plugins_list_helper)"
tmux_echo "Installed plugins:"
tmux_echo ""
for plugin in $plugins; do
# displaying only installed plugins
if plugin_already_installed "$plugin"; then
local plugin_name="$(plugin_name_helper "$plugin")"
tmux_echo " $plugin_name"
fi
done
tmux_echo ""
tmux_echo "Type plugin name to update it."
tmux_echo ""
tmux_echo "- \"all\" - updates all plugins"
tmux_echo "- ENTER - cancels"
}
update_plugin_prompt() {
tmux command-prompt -p 'plugin update:' " \
send-keys C-c; \
run-shell '$SCRIPTS_DIR/update_plugin_prompt_handler.sh %1'"
}
main() {
reload_tmux_environment
display_plugin_update_list
update_plugin_prompt
}
main

View File

@ -0,0 +1,12 @@
# Automatic tpm installation
One of the first things we do on a new machine is cloning our dotfiles. Not everything comes with them though, so for example `tpm` most likely won't be installed.
If you want to install `tpm` and plugins automatically when tmux is started, put the following snippet in `.tmux.conf` before the final `run '~/.tmux/plugins/tpm/tpm'`:
```
if "test ! -d ~/.tmux/plugins/tpm" \
"run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm && ~/.tmux/plugins/tpm/bin/install_plugins'"
```
This useful tip was submitted by @acr4 and narfman0.

View File

@ -0,0 +1,16 @@
# Changing plugins install dir
By default, TPM installs plugins in a subfolder named `plugins/` inside
`$XDG_CONFIG_HOME/tmux/` if a `tmux.conf` file was found at that location, or
inside `~/.tmux/` otherwise.
You can change the install path by putting this in `.tmux.conf`:
set-environment -g TMUX_PLUGIN_MANAGER_PATH '/some/other/path/'
Tmux plugin manager initialization in `.tmux.conf` should also be updated:
# initializes TMUX plugin manager in a new path
run /some/other/path/tpm/tpm
Please make sure that the `run` line is at the very bottom of `.tmux.conf`.

View File

@ -0,0 +1,108 @@
# How to create Tmux plugins
Creating a new plugin is easy.
For demonstration purposes we'll create a simple plugin that lists all
installed TPM plugins. Yes, a plugin that lists plugins :) We'll bind that to
`prefix + T`.
The source code for this example plugin can be found
[here](https://github.com/tmux-plugins/tmux-example-plugin).
### 1. create a new git project
TPM depends on git for downloading and updating plugins.
To create a new git project:
$ mkdir tmux_my_plugin
$ cd tmux_my_plugin
$ git init
### 2. create a `*.tmux` plugin run file
When it sources a plugin, TPM executes all `*.tmux` files in your plugins'
directory. That's how plugins are run.
Create a plugin run file in plugin directory:
$ touch my_plugin.tmux
$ chmod u+x my_plugin.tmux
You can have more than one `*.tmux` file, and all will get executed. However, usually
you'll need just one.
### 3. create a plugin key binding
We want the behavior of the plugin to trigger when a user hits `prefix + T`.
Key `T` is chosen because:
- it's "kind of" a mnemonic for `TPM`
- the key is not used by Tmux natively. Tmux man page, KEY BINDINGS section
contains a list of all the bindings Tmux uses. There's plenty of unused keys
and we don't want to override any of Tmux default key bindings.
Open the plugin run file in your favorite text editor:
$ vim my_plugin.tmux
# or
$ subl my_plugin.tmux
Put the following content in the file:
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
tmux bind-key T run-shell "$CURRENT_DIR/scripts/tmux_list_plugins.sh"
As you can see, plugin run file is a simple bash script that sets up the binding.
When pressed, `prefix + T` will execute another shell script:
`tmux_list_plugins.sh`. That script should be in `scripts/` directory -
relative to the plugin run file.
### 4. listing plugins
Now that we have the binding, let's create a script that's invoked with
`prefix + T`.
$ mkdir scripts
$ touch scripts/tmux_list_plugins.sh
$ chmod u+x scripts/tmux_list_plugins.sh
And here's the script content:
#!/usr/bin/env bash
# fetching the directory where plugins are installed
plugin_path="$(tmux show-env -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)"
# listing installed plugins
ls -1 "$plugin_path"
### 5. try it out
To see if this works, execute the plugin run file:
$ ./my_plugin.tmux
That should set up the key binding. Now hit `prefix + T` and see if it works.
### 6. publish the plugin
When everything is ready, push the plugin to an online git repository,
preferably GitHub.
Other users can install your plugin by just adding plugin git URL to the
`@plugin` list in their `.tmux.conf`.
If the plugin is on GitHub, your users will be able to use the shorthand of
`github_username/repository`.
### Conclusion
Hopefully, that was easy. As you can see, it's mostly shell scripting.
You can use other scripting languages (ruby, python etc) but plain old shell
is preferred because of portability.

View File

@ -0,0 +1,36 @@
# Managing plugins via the command line
Aside from tmux key bindings, TPM provides shell interface for managing plugins
via scripts located in [bin/](../bin/) directory.
Tmux does not need to be started in order to run scripts (but it's okay if it
is). If you [changed tpm install dir](../docs/changing_plugins_install_dir.md)
in `.tmux.conf` that should work fine too.
Prerequisites:
- tmux installed on the system (doh)
- `.tmux.conf` set up for TPM
### Installing plugins
As usual, plugins need to be specified in `.tmux.conf`. Run the following
command to install plugins:
~/.tmux/plugins/tpm/bin/install_plugins
### Updating plugins
To update all installed plugins:
~/.tmux/plugins/tpm/bin/update_plugins all
or update a single plugin:
~/.tmux/plugins/tpm/bin/update_plugins tmux-sensible
### Removing plugins
To remove plugins not on the plugin list:
~/.tmux/plugins/tpm/bin/clean_plugins

View File

@ -0,0 +1,102 @@
# Help, tpm not working!
Here's the list of issues users had with `tpm`:
<hr />
> Nothing works. `tpm` key bindings `prefix + I`, `prefix + U` not even
defined.
Related [issue #22](https://github.com/tmux-plugins/tpm/issues/22)
- Do you have required `tmux` version to run `tpm`?<br/>
Check `tmux` version with `$ tmux -V` command and make sure it's higher or
equal to the required version for `tpm` as stated in the readme.
- ZSH tmux plugin might be causing issues.<br/>
If you have it installed, try disabling it and see if `tpm` works then.
<hr />
> Help, I'm using custom config file with `tmux -f /path/to/my_tmux.conf`
to start Tmux and for some reason plugins aren't loaded!?
Related [issue #57](https://github.com/tmux-plugins/tpm/issues/57)
`tpm` has a known issue when using custom config file with `-f` option.
The solution is to use alternative plugin definition syntax. Here are the steps
to make it work:
1. remove all `set -g @plugin` lines from tmux config file
2. in the config file define the plugins in the following way:
# List of plugins
set -g @tpm_plugins ' \
tmux-plugins/tpm \
tmux-plugins/tmux-sensible \
tmux-plugins/tmux-resurrect \
'
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
3. Reload TMUX environment so TPM is sourced: `$ tmux source /path/to/my_tmux.conf`
The plugins should now be working.
<hr />
> Weird sequence of characters show up when installing or updating plugins
Related: [issue #25](https://github.com/tmux-plugins/tpm/issues/25)
- This could be caused by [tmuxline.vim](https://github.com/edkolev/tmuxline.vim)
plugin. Uninstall it and see if things work.
<hr />
> "failed to connect to server" error when sourcing .tmux.conf
Related: [issue #48](https://github.com/tmux-plugins/tpm/issues/48)
- Make sure `tmux source ~/.tmux.conf` command is ran from inside `tmux`.
<hr />
> tpm not working: '~/.tmux/plugins/tpm/tpm' returned 2 (Windows / Cygwin)
Related: [issue #81](https://github.com/tmux-plugins/tpm/issues/81)
This issue is most likely caused by Windows line endings. For example, if you
have git's `core.autocrlf` option set to `true`, git will automatically convert
all the files to Windows line endings which might cause a problem.
The solution is to convert all line ending to Unix newline characters. This
command handles that for all files under `.tmux/` dir (skips `.git`
subdirectories):
```bash
find ~/.tmux -type d -name '.git*' -prune -o -type f -print0 | xargs -0 dos2unix
```
<hr />
> '~/.tmux/plugins/tpm/tpm' returned 127 (on macOS, w/ tmux installed using brew)
Related: [issue #67](https://github.com/tmux-plugins/tpm/issues/67)
This problem is because tmux's `run-shell` command runs a shell which doesn't read from user configs, thus tmux installed in a brew prefix (e.g. `/usr/local/bin`) will not be found.
The solution is to find your brew prefix
```sh
> echo "$(brew --prefix)/bin"
/opt/homebrew/bin
```
And prepend it to the `PATH` environment variable
```
set-environment -g PATH "/opt/homebrew/bin:/bin:/usr/bin"
```
before any `run-shell`/`run` commands in `~/.tmux.conf`.

View File

@ -0,0 +1,2 @@
.vagrant/
lib/

View File

@ -0,0 +1,19 @@
# generic packages and tmux
before_install:
- sudo apt-get update
- sudo apt-get install -y git-core expect
- sudo apt-get install -y python-software-properties software-properties-common
- sudo apt-get install -y libevent-dev libncurses-dev
- git clone https://github.com/tmux/tmux.git
- cd tmux
- git checkout 2.0
- sh autogen.sh
- ./configure && make && sudo make install
install:
- git fetch --unshallow --recurse-submodules || git fetch --recurse-submodules
# manual `git clone` required for testing `tmux-test` plugin itself
- git clone https://github.com/tmux-plugins/tmux-test lib/tmux-test; true
- lib/tmux-test/setup
script: ./tests/run_tests_in_isolation

View File

@ -0,0 +1,45 @@
# Changelog
### master
- move `setup` task to `.travis.yml` for travis tests
- "merge" travis.yml and travis_for_plugins.yml files (no need to keep em
separate)
- add more useful helper functions
- remove tmux-test repo as a submodule from self, this causes issues with
`$ git submodule update --recursive --init` command that some users use for
managing other plugins
- add new helper `teardown_helper`
- add `run_tests` helper
- change CLI syntax for choosing vagrant machine to run the tests on
- enable running just a single test via `run_tests` cli interface
- add `--keep-running` cli option to continue running vagrant after the tests
are done executing
- start using tmux 2.0 for tests
### v0.2.0, 2015-02-22
- `setup` script gitignores `tests/helpers.sh`
- move `tests/helpers.sh` to `tests/helpers/helpers.sh`
- `setup` undo removes added lines from gitignore file
### v0.1.0, 2015-02-22
- changes so that 'tmux-test' can be included with tmux plugins
- do not gitignore submodules directory
- add installation and usage instructions
- copy `.travis.yml` to the project root when running `setup` script
- add a brief mention of travis CI to the readme
- add test helpers
- `setup` script symlinks helpers file to `tests/` directory
- `setup` script can undo most of its actions
- add a tmux scripting test
- `tmux-test` uses `tmux-test` to test itself
- update `tmux-test` submodule
- a different `travis.yml` for `tmux-test` and for plugins
### v0.0.1, 2015-02-21
- git init
- add vagrant provisioning scripts for ubuntu and debian
- add a ".travis.yml" file
- generic "run_tests" script
- "run_tests_in_isolation" script
- add "Vagrantfile"
- enable passing VM names as arguments to "run_tests" script

View File

@ -0,0 +1,19 @@
Copyright (C) Bruno Sutic
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,134 @@
# tmux-test
[![Build Status](https://travis-ci.org/tmux-plugins/tmux-test.png?branch=master)](https://travis-ci.org/tmux-plugins/tmux-test)
A small framework for isolated testing of tmux plugins. Isolation is achieved by
running the tests in `Vagrant`. Works on [travis](travis-ci.org) too.
Extracted from [tmux plugin manager](https://github.com/tmux-plugins/tpm) and
[tmux-copycat](https://github.com/tmux-plugins/tmux-copycat).
Dependencies: `Vagrant` (no required when running on travis).
### Setup
Let's say you made tmux plugin with the following file hierarchy:
```text
/tmux-plugin
|-- plugin.tmux
`-- scripts
`-- plugin_script.sh
```
From your project root directory (tmux-plugin/) execute the following shell
command to fetch `tmux-test` and add it as a submodule:
$ git submodule add https://github.com/tmux-plugins/tmux-test.git lib/tmux-test
Run the `setup` script:
$ lib/tmux-test/setup
The project directory will now look like this (additions have comments):
```text
/tmux-plugin
|-- plugin.tmux
|-- run_tests # symlink, gitignored
|-- .gitignore # 2 lines appended to gitignore
|-- .travis.yml # added
|-- lib/tmux-test/ # git submodule
|-- scripts
| `-- plugin_script.sh
`-- tests # dir to put the tests in
`-- run_tests_in_isolation.sh # symlink, gitignored
`-- helpers
`-- helpers.sh # symlinked bash helpers, gitignored
```
`tmux-test` is now set up. You are ok to commit the additions to the repo.
### Writing and running tests
A test is any executable with a name starting with `test_` in `tests/`
directory.
Now that you installed `tmux-test` let's create an example test.
- create a `tests/test_example.sh` file with the following content (it's a
`bash` script but it can be any executable):
#/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# bash helpers provided by 'tmux-test'
source $CURRENT_DIR/helpers/helpers.sh
# installs plugin from current repo in Vagrant (or on Travis)
install_tmux_plugin_under_test_helper
# start tmux in background (plugin under test is sourced)
tmux new -d
# get first session name
session_name="$(tmux list-sessions -F "#{session_name}")"
# fail the test if first session name is not "0"
if [ "$session_name" == "0" ]; then
# fail_helper is also provided by 'tmux-test'
fail_helper "First session name is not '0' by default"
fi
# sets the right script exit code ('tmux-test' helper)
exit_helper
- make the test file executable with `$ chmod +x tests/test_example.sh`
- run the test by executing `./run_tests` from the project root directory
- the first invocation might take some time because Vagrant's ubuntu virtual
machine is downloading. You should see `Success, tests pass!` message when it's
done.
Check out more example test scripts in this project's [tests/ directory](tests/).
### Continuous integration
The setup script (`lib/tmux-test/setup`) added a `.travis.yml` file to the
project root. To setup continuous integration, just add/enable the project on
[travis](travis-ci.org).
### Notes
- The `tests/` directory for tests and `lib/tmux-test/` for cloning `tmux-test`
into cannot be changed currently
- Don't run `tests/run_tests_in_isolation` script on your local development
environment. That's an internal test runner meant to be executed in an
isolated environment like `vagrant` or `travis`.<br/>
Use `./run_tests` script.
- You can use `KEEP_RUNNING=true ./run_tests` for faster test running cycle.
If this case `Vagrant` will keep running even after the tests are done.
- You can use `VAGRANT_CWD=lib/tmux-text/ vagrant ssh ubuntu` for ssh login to
`Vagrant`.
### Running `tmux-test` framework tests
`tmux-test` uses itself to test itself. To run framework tests:
- clone this project `$ git clone git@github.com:tmux-plugins/tmux-test.git`
- `$ cd tmux-test`
- run `$ ./run_framework_tests`
### Other goodies
- [tmux-copycat](https://github.com/tmux-plugins/tmux-copycat) - a plugin for
regex searches in tmux and fast match selection
- [tmux-continuum](https://github.com/tmux-plugins/tmux-continuum) - automatic
restoring and continuous saving of tmux env
You might want to follow [@brunosutic](https://twitter.com/brunosutic) on
twitter if you want to hear about new tmux plugins or feature updates.
### License
[MIT](LICENSE.md)

View File

@ -0,0 +1,17 @@
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder "../../", "/vagrant"
config.vm.define :ubuntu do |ubuntu|
ubuntu.vm.box = "hashicorp/precise64"
ubuntu.vm.provision "shell", path: "vagrant_ubuntu_provisioning.sh"
end
config.vm.define :centos do |centos|
centos.vm.box = "chef/centos-6.5"
centos.vm.provision "shell", path: "vagrant_centos_provisioning.sh"
end
end

View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# This file is used to run "tmux-test" framework tests.
# "setup" script is needed to run the tests, but it overrides some working dir
# files. To address that, "setup" is run before the tests and it's actions are
# undone after.
main() {
git clone https://github.com/tmux-plugins/tmux-test lib/tmux-test
lib/tmux-test/setup
./run_tests
local exit_value=$?
lib/tmux-test/setup "undo"
exit "$exit_value"
}
main

View File

@ -0,0 +1,93 @@
#!/usr/bin/env bash
# invoke this script from your projects root directory
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# pass "undo" as a script arg to undo most of the setup actions
UNDO_SETUP="$1"
undo() {
[ "$UNDO_SETUP" == "undo" ]
}
restore() {
local file="$1"
rm -f "$file"
git checkout -- "$file" 2>/dev/null
}
gitignore() {
local file="$1"
grep -q "^${file}$" .gitignore 2>/dev/null || echo "$file" >> .gitignore
}
remove_from_gitignore() {
local file="$1"
local escaped_filename="$(echo "$file" | sed "s,/,\\\/,g")"
sed -i"" "/^${escaped_filename}$/d" .gitignore
}
add_files_to_gitignore() {
if ! undo; then
gitignore "run_tests"
gitignore "tests/run_tests_in_isolation"
gitignore "tests/helpers/helpers.sh"
else
remove_from_gitignore "run_tests"
remove_from_gitignore "tests/run_tests_in_isolation"
remove_from_gitignore "tests/helpers/helpers.sh"
fi
}
symlink_user_test_runner() {
local file="run_tests"
if ! undo; then
ln -sf "lib/tmux-test/${file}" "$file"
else
restore "$file"
fi
}
create_directory_for_tests() {
if ! undo; then
mkdir -p tests/helpers/
fi
}
symlink_internal_test_runner() {
local file="tests/run_tests_in_isolation"
if ! undo; then
ln -sf "../lib/tmux-test/${file}" "$file"
else
restore "$file"
fi
}
symlink_test_helpers() {
local file="tests/helpers/helpers.sh"
if ! undo; then
ln -sf "../../lib/tmux-test/${file}" "$file"
else
restore "$file"
fi
}
copy_travis_yml() {
local file=".travis.yml"
if ! undo; then
cp "lib/tmux-test/${file}" "$file"
else
restore "$file"
fi
}
main() {
add_files_to_gitignore
symlink_user_test_runner
create_directory_for_tests
symlink_internal_test_runner
symlink_test_helpers
copy_travis_yml
}
main

View File

@ -0,0 +1,68 @@
# This file is a symlink from 'tmux-test' plugin.
# You probably don't want to edit it.
# Global variable that keeps the value of test status (success/fail).
# Suggested usage is via `fail_helper` and `exit_helper` functions.
TEST_STATUS="success"
# PRIVATE FUNCTIONS
_clone_the_plugin() {
local plugin_path="${HOME}/.tmux/plugins/tmux-plugin-under-test/"
rm -rf "$plugin_path"
git clone --recursive "${CURRENT_DIR}/../" "$plugin_path" >/dev/null 2>&1
}
_add_plugin_to_tmux_conf() {
set_tmux_conf_helper<<-HERE
run-shell '~/.tmux/plugins/tmux-plugin-under-test/*.tmux'
HERE
}
# PUBLIC HELPER FUNCTIONS
teardown_helper() {
rm -f ~/.tmux.conf
rm -rf ~/.tmux/
tmux kill-server >/dev/null 2>&1
}
set_tmux_conf_helper() {
> ~/.tmux.conf # empty tmux.conf file
while read line; do
echo "$line" >> ~/.tmux.conf
done
}
fail_helper() {
local message="$1"
echo "$message" >&2
TEST_STATUS="fail"
}
exit_helper() {
teardown_helper
if [ "$TEST_STATUS" == "fail" ]; then
echo "FAIL!"
echo
exit 1
else
echo "SUCCESS"
echo
exit 0
fi
}
install_tmux_plugin_under_test_helper() {
_clone_the_plugin
_add_plugin_to_tmux_conf
}
run_tests() {
# get all the functions starting with 'test_' and invoke them
for test in $(compgen -A function | grep "^test_"); do
"$test"
done
exit_helper
}

View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
# This file is a symlink from 'tmux-test' plugin.
# You probably don't want to edit it.
# This script should be run within an isolated enviroment (Vagrant, travis).
# Depending on what the tests do, it might NOT be safe to run this script
# directly on the development machine.
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
EXIT_VALUE=0 # running a test suite is successful by default
all_test_files() {
ls -1 "$CURRENT_DIR" | # test files are in the current dir
\grep -i "^test" | # test file names start with "test"
xargs # file names in a single line
}
set_exit_val_to_false() {
EXIT_VALUE=1
}
run_tests() {
local test_file tests_files
if [ "$#" -gt 0 ]; then
test_files="${@//tests\//}" # remove 'tests/' directory prefix
else
test_files="$(all_test_files)"
fi
for test_file in $test_files; do
echo "Running test: $test_file"
"${CURRENT_DIR}/${test_file}"
# handling exit value
local test_exit_value="$?"
if [ "$test_exit_value" -ne 0 ]; then
set_exit_val_to_false
fi
done
}
main() {
run_tests "$@"
exit "$EXIT_VALUE"
}
main "$@"

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
exit 0

View File

@ -0,0 +1,24 @@
#/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# bash helpers provided by 'tmux-test'
source $CURRENT_DIR/helpers/helpers.sh
# installs plugin from current repo in Vagrant (or on Travis)
install_tmux_plugin_under_test_helper
# start tmux in background (plugin under test is sourced)
tmux new -d
# get first session name
session_name="$(tmux list-sessions -F "#{session_name}")"
# fail the test if first session name is not "0"
if ! [ "$session_name" == "0" ]; then
# fail_helper is also provided by 'tmux-test'
fail_helper "First session name is not '0' by default"
fi
# sets the right script exit code ('tmux-test' helper)
exit_helper

View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $CURRENT_DIR/helpers/helpers.sh
number_of_windows() {
tmux list-windows |
wc -l |
sed "s/ //g"
}
main() {
# start tmux in the background
tmux new -d
tmux new-window
local number_of_windows="$(number_of_windows)"
if ! [ "$number_of_windows" -eq 2 ]; then
fail_helper "Incorrect number of windows. Expected 2, got $number_of_windows"
fi
exit_helper
}
main

View File

@ -0,0 +1,26 @@
#!/usr/bin/env bash
# libevent2 installation instructions from here
# https://gist.github.com/rschuman/6168833
sudo su -
yum -y install gcc kernel-devel make automake autoconf ncurses-devel
yum -y install git-core expect vim ruby ruby-devel ruby-irb
# install libevent2 from source
curl http://sourceforge.net/projects/levent/files/latest/download?source=files -L -o libevent2.tar.gz -w 'Last URL was: %{url_effective}'
cd ~/downloads
tar zxvf libevent2.tar.gz
cd ./libevent-*
./configure --prefix=/usr/local
make
make install
# compile tmux
git clone https://github.com/tmux/tmux.git ~/tmux_source
cd ~/tmux_source
git checkout 2.0
sh autogen.sh
LDFLAGS="-L/usr/local/lib -Wl,-rpath=/usr/local/lib" ./configure --prefix=/usr/local
make && sudo make install

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
sudo apt-get update
sudo apt-get install -y git-core expect vim
sudo apt-get install -y python-software-properties software-properties-common
sudo apt-get install -y build-essential libtool autotools-dev autoconf
sudo apt-get install -y pkg-config libevent-dev libncurses-dev
# install tmux 2.0
git clone https://github.com/tmux/tmux.git ~/tmux_source
cd ~/tmux_source
git checkout 2.0
sh autogen.sh
./configure && make && sudo make install

View File

@ -0,0 +1,78 @@
#!/usr/bin/env bash
VERSION="$1"
UNSUPPORTED_MSG="$2"
get_tmux_option() {
local option=$1
local default_value=$2
local option_value=$(tmux show-option -gqv "$option")
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
# Ensures a message is displayed for 5 seconds in tmux prompt.
# Does not override the 'display-time' tmux option.
display_message() {
local message="$1"
# display_duration defaults to 5 seconds, if not passed as an argument
if [ "$#" -eq 2 ]; then
local display_duration="$2"
else
local display_duration="5000"
fi
# saves user-set 'display-time' option
local saved_display_time=$(get_tmux_option "display-time" "750")
# sets message display time to 5 seconds
tmux set-option -gq display-time "$display_duration"
# displays message
tmux display-message "$message"
# restores original 'display-time' value
tmux set-option -gq display-time "$saved_display_time"
}
# this is used to get "clean" integer version number. Examples:
# `tmux 1.9` => `19`
# `1.9a` => `19`
get_digits_from_string() {
local string="$1"
local only_digits="$(echo "$string" | tr -dC '[:digit:]')"
echo "$only_digits"
}
tmux_version_int() {
local tmux_version_string=$(tmux -V)
echo "$(get_digits_from_string "$tmux_version_string")"
}
unsupported_version_message() {
if [ -n "$UNSUPPORTED_MSG" ]; then
echo "$UNSUPPORTED_MSG"
else
echo "Error, Tmux version unsupported! Please install Tmux version $VERSION or greater!"
fi
}
exit_if_unsupported_version() {
local current_version="$1"
local supported_version="$2"
if [ "$current_version" -lt "$supported_version" ]; then
display_message "$(unsupported_version_message)"
exit 1
fi
}
main() {
local supported_version_int="$(get_digits_from_string "$VERSION")"
local current_version_int="$(tmux_version_int)"
exit_if_unsupported_version "$current_version_int" "$supported_version_int"
}
main

View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HELPERS_DIR="$CURRENT_DIR/helpers"
source "$HELPERS_DIR/plugin_functions.sh"
source "$HELPERS_DIR/utility.sh"
if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
source "$HELPERS_DIR/tmux_echo_functions.sh"
else # shell output functions
source "$HELPERS_DIR/shell_echo_functions.sh"
fi
clean_plugins() {
local plugins plugin plugin_directory
plugins="$(tpm_plugins_list_helper)"
for plugin_directory in "$(tpm_path)"/*; do
[ -d "${plugin_directory}" ] || continue
plugin="$(plugin_name_helper "${plugin_directory}")"
case "${plugins}" in
*"${plugin}"*) : ;;
*)
[ "${plugin}" = "tpm" ] && continue
echo_ok "Removing \"$plugin\""
rm -rf "${plugin_directory}" >/dev/null 2>&1
[ -d "${plugin_directory}" ] &&
echo_err " \"$plugin\" clean fail" ||
echo_ok " \"$plugin\" clean success"
;;
esac
done
}
main() {
ensure_tpm_path_exists
clean_plugins
exit_value_helper
}
main

View File

@ -0,0 +1,104 @@
# using @tpm_plugins is now deprecated in favor of using @plugin syntax
tpm_plugins_variable_name="@tpm_plugins"
# manually expanding tilde char or `$HOME` variable.
_manual_expansion() {
local path="$1"
local expanded_tilde="${path/#\~/$HOME}"
echo "${expanded_tilde/#\$HOME/$HOME}"
}
_tpm_path() {
local string_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)/"
_manual_expansion "$string_path"
}
_CACHED_TPM_PATH="$(_tpm_path)"
# Get the absolute path to the users configuration file of TMux.
# This includes a prioritized search on different locations.
#
_get_user_tmux_conf() {
# Define the different possible locations.
xdg_location="${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf"
default_location="$HOME/.tmux.conf"
# Search for the correct configuration file by priority.
if [ -f "$xdg_location" ]; then
echo "$xdg_location"
else
echo "$default_location"
fi
}
_tmux_conf_contents() {
user_config=$(_get_user_tmux_conf)
cat /etc/tmux.conf "$user_config" 2>/dev/null
if [ "$1" == "full" ]; then # also output content from sourced files
local file
for file in $(_sourced_files); do
cat $(_manual_expansion "$file") 2>/dev/null
done
fi
}
# return files sourced from tmux config files
_sourced_files() {
_tmux_conf_contents |
sed -E -n -e "s/^[[:space:]]*source(-file)?[[:space:]]+(-q+[[:space:]]+)?['\"]?([^'\"]+)['\"]?/\3/p"
}
# Want to be able to abort in certain cases
trap "exit 1" TERM
export TOP_PID=$$
_fatal_error_abort() {
echo >&2 "Aborting."
kill -s TERM $TOP_PID
}
# PUBLIC FUNCTIONS BELOW
tpm_path() {
if [ "$_CACHED_TPM_PATH" == "/" ]; then
echo >&2 "FATAL: Tmux Plugin Manager not configured in tmux.conf"
_fatal_error_abort
fi
echo "$_CACHED_TPM_PATH"
}
tpm_plugins_list_helper() {
# lists plugins from @tpm_plugins option
echo "$(tmux start-server\; show-option -gqv "$tpm_plugins_variable_name")"
# read set -g @plugin "tmux-plugins/tmux-example-plugin" entries
_tmux_conf_contents "full" |
awk '/^[ \t]*set(-option)? +-g +@plugin/ { gsub(/'\''/,""); gsub(/'\"'/,""); print $4 }'
}
# Allowed plugin name formats:
# 1. "git://github.com/user/plugin_name.git"
# 2. "user/plugin_name"
plugin_name_helper() {
local plugin="$1"
# get only the part after the last slash, e.g. "plugin_name.git"
local plugin_basename="$(basename "$plugin")"
# remove ".git" extension (if it exists) to get only "plugin_name"
local plugin_name="${plugin_basename%.git}"
echo "$plugin_name"
}
plugin_path_helper() {
local plugin="$1"
local plugin_name="$(plugin_name_helper "$plugin")"
echo "$(tpm_path)${plugin_name}/"
}
plugin_already_installed() {
local plugin="$1"
local plugin_path="$(plugin_path_helper "$plugin")"
[ -d "$plugin_path" ] &&
cd "$plugin_path" &&
git remote >/dev/null 2>&1
}

View File

@ -0,0 +1,7 @@
echo_ok() {
echo "$*"
}
echo_err() {
fail_helper "$*"
}

View File

@ -0,0 +1,28 @@
_has_emacs_mode_keys() {
$(tmux show -gw mode-keys | grep -q emacs)
}
tmux_echo() {
local message="$1"
tmux run-shell "echo '$message'"
}
echo_ok() {
tmux_echo "$*"
}
echo_err() {
tmux_echo "$*"
}
end_message() {
if _has_emacs_mode_keys; then
local continue_key="ESCAPE"
else
local continue_key="ENTER"
fi
tmux_echo ""
tmux_echo "TMUX environment reloaded."
tmux_echo ""
tmux_echo "Done, press $continue_key to continue."
}

View File

@ -0,0 +1,6 @@
HELPERS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$HELPERS_DIR/plugin_functions.sh"
reload_tmux_environment() {
tmux source-file $(_get_user_tmux_conf) >/dev/null 2>&1
}

View File

@ -0,0 +1,17 @@
ensure_tpm_path_exists() {
mkdir -p "$(tpm_path)"
}
fail_helper() {
local message="$1"
echo "$message" >&2
FAIL="true"
}
exit_value_helper() {
if [ "$FAIL" == "true" ]; then
exit 1
else
exit 0
fi
}

View File

@ -0,0 +1,75 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HELPERS_DIR="$CURRENT_DIR/helpers"
source "$HELPERS_DIR/plugin_functions.sh"
source "$HELPERS_DIR/utility.sh"
if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
source "$HELPERS_DIR/tmux_echo_functions.sh"
else # shell output functions
source "$HELPERS_DIR/shell_echo_functions.sh"
fi
clone() {
local plugin="$1"
local branch="$2"
if [ -n "$branch" ]; then
cd "$(tpm_path)" &&
GIT_TERMINAL_PROMPT=0 git clone -b "$branch" --single-branch --recursive "$plugin" >/dev/null 2>&1
else
cd "$(tpm_path)" &&
GIT_TERMINAL_PROMPT=0 git clone --single-branch --recursive "$plugin" >/dev/null 2>&1
fi
}
# tries cloning:
# 1. plugin name directly - works if it's a valid git url
# 2. expands the plugin name to point to a GitHub repo and tries cloning again
clone_plugin() {
local plugin="$1"
local branch="$2"
clone "$plugin" "$branch" ||
clone "https://git::@github.com/$plugin" "$branch"
}
# clone plugin and produce output
install_plugin() {
local plugin="$1"
local branch="$2"
local plugin_name="$(plugin_name_helper "$plugin")"
if plugin_already_installed "$plugin"; then
echo_ok "Already installed \"$plugin_name\""
else
echo_ok "Installing \"$plugin_name\""
clone_plugin "$plugin" "$branch" &&
echo_ok " \"$plugin_name\" download success" ||
echo_err " \"$plugin_name\" download fail"
fi
}
install_plugins() {
local plugins="$(tpm_plugins_list_helper)"
for plugin in $plugins; do
IFS='#' read -ra plugin <<< "$plugin"
install_plugin "${plugin[0]}" "${plugin[1]}"
done
}
verify_tpm_path_permissions() {
local path="$(tpm_path)"
# check the write permission flag for all users to ensure
# that we have proper access
[ -w "$path" ] ||
echo_err "$path is not writable!"
}
main() {
ensure_tpm_path_exists
verify_tpm_path_permissions
install_plugins
exit_value_helper
}
main

View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HELPERS_DIR="$CURRENT_DIR/helpers"
source "$HELPERS_DIR/plugin_functions.sh"
plugin_dir_exists() {
[ -d "$1" ]
}
# Runs all *.tmux files from the plugin directory.
# Files are ran as executables.
# No errors if the plugin dir does not exist.
silently_source_all_tmux_files() {
local plugin_path="$1"
local plugin_tmux_files="$plugin_path*.tmux"
if plugin_dir_exists "$plugin_path"; then
for tmux_file in $plugin_tmux_files; do
# if the glob didn't find any files this will be the
# unexpanded glob which obviously doesn't exist
[ -f "$tmux_file" ] || continue
# runs *.tmux file as an executable
$tmux_file >/dev/null 2>&1
done
fi
}
source_plugins() {
local plugin plugin_path
local plugins="$(tpm_plugins_list_helper)"
for plugin in $plugins; do
IFS='#' read -ra plugin <<< "$plugin"
plugin_path="$(plugin_path_helper "${plugin[0]}")"
silently_source_all_tmux_files "$plugin_path"
done
}
main() {
source_plugins
}
main

View File

@ -0,0 +1,78 @@
#!/usr/bin/env bash
# this script handles core logic of updating plugins
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HELPERS_DIR="$CURRENT_DIR/helpers"
source "$HELPERS_DIR/plugin_functions.sh"
source "$HELPERS_DIR/utility.sh"
if [ "$1" == "--tmux-echo" ]; then # tmux-specific echo functions
source "$HELPERS_DIR/tmux_echo_functions.sh"
else # shell output functions
source "$HELPERS_DIR/shell_echo_functions.sh"
fi
# from now on ignore first script argument
shift
pull_changes() {
local plugin="$1"
local plugin_path="$(plugin_path_helper "$plugin")"
cd "$plugin_path" &&
GIT_TERMINAL_PROMPT=0 git pull &&
GIT_TERMINAL_PROMPT=0 git submodule update --init --recursive
}
update() {
local plugin="$1" output
output=$(pull_changes "$plugin" 2>&1)
if (( $? == 0 )); then
echo_ok " \"$plugin\" update success"
echo_ok "$(echo "$output" | sed -e 's/^/ | /')"
else
echo_err " \"$plugin\" update fail"
echo_err "$(echo "$output" | sed -e 's/^/ | /')"
fi
}
update_all() {
echo_ok "Updating all plugins!"
echo_ok ""
local plugins="$(tpm_plugins_list_helper)"
for plugin in $plugins; do
IFS='#' read -ra plugin <<< "$plugin"
local plugin_name="$(plugin_name_helper "${plugin[0]}")"
# updating only installed plugins
if plugin_already_installed "$plugin_name"; then
update "$plugin_name" &
fi
done
wait
}
update_plugins() {
local plugins="$*"
for plugin in $plugins; do
IFS='#' read -ra plugin <<< "$plugin"
local plugin_name="$(plugin_name_helper "${plugin[0]}")"
if plugin_already_installed "$plugin_name"; then
update "$plugin_name" &
else
echo_err "$plugin_name not installed!" &
fi
done
wait
}
main() {
ensure_tpm_path_exists
if [ "$1" == "all" ]; then
update_all
else
update_plugins "$*"
fi
exit_value_helper
}
main "$*"

View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HELPERS_DIR="$CURRENT_DIR/helpers"
if [ $# -eq 0 ]; then
exit 0
fi
source "$HELPERS_DIR/tmux_echo_functions.sh"
source "$HELPERS_DIR/tmux_utils.sh"
main() {
"$CURRENT_DIR/update_plugin.sh" --tmux-echo "$*"
reload_tmux_environment
end_message
}
main "$*"

View File

@ -0,0 +1,13 @@
install_key_option="@tpm-install"
default_install_key="I"
update_key_option="@tpm-update"
default_update_key="U"
clean_key_option="@tpm-clean"
default_clean_key="M-u"
SUPPORTED_TMUX_VERSION="1.9"
DEFAULT_TPM_ENV_VAR_NAME="TMUX_PLUGIN_MANAGER_PATH"
DEFAULT_TPM_PATH="$HOME/.tmux/plugins/"

View File

@ -0,0 +1,36 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + I
send "I"
# cloning might take a while
set timeout 20
expect_after {
timeout { exit 1 }
}
expect {
"Installing \"non-existing-plugin\""
}
expect {
"\"non-existing-plugin\" download fail"
}
expect {
"Done, press ENTER to continue" {
exit 0
}
}
exit 1

View File

@ -0,0 +1,35 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + alt + u
send "u"
set timeout 5
expect_after {
timeout { exit 1 }
}
expect {
"Removing \"tmux-example-plugin\""
}
expect {
"\"tmux-example-plugin\" clean success"
}
expect {
"Done, press ENTER to continue." {
exit 0
}
}
exit 1

View File

@ -0,0 +1,44 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + I
send "I"
# cloning might take a while
set timeout 15
expect_after {
timeout { exit 1 }
}
expect {
"Installing \"tmux-example-plugin\""
}
expect {
"\"tmux-example-plugin\" download success"
}
expect {
"Installing \"tmux-copycat\""
}
expect {
"\"tmux-copycat\" download success"
}
expect {
"Done, press ENTER to continue." {
exit 0
}
}
exit 1

View File

@ -0,0 +1,50 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + I
send "I"
# cloning might take a while
set timeout 15
expect_after {
timeout { exit 1 }
}
expect {
"Installing \"tmux-example-plugin\""
}
expect {
"\"tmux-example-plugin\" download success"
}
expect {
"Done, press ENTER to continue" {
send "
"
}
}
sleep 1
# this is tmux prefix + I
send "I"
expect {
"Already installed \"tmux-example-plugin\""
}
expect {
"Done, press ENTER to continue" {
exit 0
}
}

View File

@ -0,0 +1,55 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + U
send "U"
set timeout 15
expect_after {
timeout { exit 1 }
}
expect {
"Installed plugins"
}
expect {
"tmux-example-plugin"
}
expect {
"\"all\" - updates all plugins"
}
expect {
"ENTER - cancels"
}
# wait for tmux to display prompt before sending characters
sleep 1
send "tmux-example-plugin\r"
expect {
"Updating \"tmux-example-plugin\""
}
expect {
"\"tmux-example-plugin\" update success"
}
expect {
"Done, press ENTER to continue." {
exit 0
}
}
exit 1

View File

@ -0,0 +1,59 @@
#!/usr/bin/env expect
# disables script output
log_user 0
spawn tmux
# Waiting for tmux to attach. If this is not done, next command, `send` will
# not work properly.
sleep 1
# this is tmux prefix + U
send "U"
set timeout 5
expect_after {
timeout { exit 1 }
}
expect {
"Installed plugins"
}
expect {
"tmux-example-plugin"
}
expect {
"\"all\" - updates all plugins"
}
expect {
"ENTER - cancels"
}
# wait for tmux to display prompt before sending characters
sleep 1
send "all\r"
expect {
"Updating all plugins!"
}
expect {
"Updating \"tmux-example-plugin\""
}
expect {
"\"tmux-example-plugin\" update success"
}
expect {
"Done, press ENTER to continue." {
exit 0
}
}
exit 1

View File

@ -0,0 +1,13 @@
check_dir_exists_helper() {
[ -d "$1" ]
}
# runs the scripts and asserts it has the correct output and exit code
script_run_helper() {
local script="$1"
local expected_output="$2"
local expected_exit_code="${3:-0}"
$script 2>&1 |
grep "$expected_output" >/dev/null 2>&1 && # grep -q flag quits the script early
[ "${PIPESTATUS[0]}" -eq "$expected_exit_code" ]
}

View File

@ -0,0 +1,67 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TPM_DIR="$PWD"
PLUGINS_DIR="$HOME/.tmux/plugins"
source "$CURRENT_DIR/helpers/helpers.sh"
source "$CURRENT_DIR/helpers/tpm.sh"
manually_install_the_plugin() {
rm -rf "$PLUGINS_DIR"
mkdir -p "$PLUGINS_DIR"
cd "$PLUGINS_DIR"
git clone --quiet https://github.com/tmux-plugins/tmux-example-plugin
}
# TMUX KEY-BINDING TESTS
test_plugin_uninstallation_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
run-shell "$TPM_DIR/tpm"
HERE
manually_install_the_plugin
"$CURRENT_DIR/expect_successful_clean_plugins" ||
fail_helper "[key-binding] clean fails"
teardown_helper
}
# SCRIPT TESTS
test_plugin_uninstallation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
run-shell "$TPM_DIR/tpm"
HERE
manually_install_the_plugin
script_run_helper "$TPM_DIR/bin/clean_plugins" '"tmux-example-plugin" clean success' ||
fail_helper "[script] plugin cleaning fails"
teardown_helper
}
test_unsuccessful_plugin_uninstallation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
run-shell "$TPM_DIR/tpm"
HERE
manually_install_the_plugin
chmod 000 "$PLUGINS_DIR/tmux-example-plugin" # disable directory deletion
local expected_exit_code=1
script_run_helper "$TPM_DIR/bin/clean_plugins" '"tmux-example-plugin" clean fail' "$expected_exit_code" ||
fail_helper "[script] unsuccessful plugin cleaning doesn't fail"
chmod 755 "$PLUGINS_DIR/tmux-example-plugin" # enable directory deletion
teardown_helper
}
run_tests

View File

@ -0,0 +1,284 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PLUGINS_DIR="$HOME/.tmux/plugins"
TPM_DIR="$PWD"
CUSTOM_PLUGINS_DIR="$HOME/foo/plugins"
ADDITIONAL_CONFIG_FILE_1="$HOME/.tmux/additional_config_file_1"
ADDITIONAL_CONFIG_FILE_2="$HOME/.tmux/additional_config_file_2"
source "$CURRENT_DIR/helpers/helpers.sh"
source "$CURRENT_DIR/helpers/tpm.sh"
# TMUX KEY-BINDING TESTS
test_plugin_installation_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
"$CURRENT_DIR/expect_successful_plugin_download" ||
fail_helper "[key-binding] plugin installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding] plugin download fails"
teardown_helper
}
test_plugin_installation_via_tmux_key_binding_set_option() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set-option -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
"$CURRENT_DIR/expect_successful_plugin_download" ||
fail_helper "[key-binding][set-option] plugin installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding][set-option] plugin download fails"
teardown_helper
}
test_plugin_installation_custom_dir_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set-environment -g TMUX_PLUGIN_MANAGER_PATH '$CUSTOM_PLUGINS_DIR'
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
"$CURRENT_DIR/expect_successful_plugin_download" ||
fail_helper "[key-binding][custom dir] plugin installation fails"
check_dir_exists_helper "$CUSTOM_PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding][custom dir] plugin download fails"
teardown_helper
rm -rf "$CUSTOM_PLUGINS_DIR"
}
test_non_existing_plugin_installation_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/non-existing-plugin"
run-shell "$TPM_DIR/tpm"
HERE
"$CURRENT_DIR/expect_failed_plugin_download" ||
fail_helper "[key-binding] non existing plugin installation doesn't fail"
teardown_helper
}
test_multiple_plugins_installation_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
\ \ set -g @plugin 'tmux-plugins/tmux-copycat'
run-shell "$TPM_DIR/tpm"
HERE
"$CURRENT_DIR/expect_successful_multiple_plugins_download" ||
fail_helper "[key-binding] multiple plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[key-binding] plugin download fails (tmux-copycat)"
teardown_helper
}
test_plugins_installation_from_sourced_file_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
source '$ADDITIONAL_CONFIG_FILE_1'
set -g @plugin 'tmux-plugins/tmux-example-plugin'
run-shell "$TPM_DIR/tpm"
HERE
mkdir ~/.tmux
echo "set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1"
"$CURRENT_DIR/expect_successful_multiple_plugins_download" ||
fail_helper "[key-binding][sourced file] plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding][sourced file] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[key-binding][sourced file] plugin download fails (tmux-copycat)"
teardown_helper
}
test_plugins_installation_from_multiple_sourced_files_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
\ \ source '$ADDITIONAL_CONFIG_FILE_1'
source-file '$ADDITIONAL_CONFIG_FILE_2'
run-shell "$TPM_DIR/tpm"
HERE
mkdir ~/.tmux
echo "set -g @plugin 'tmux-plugins/tmux-example-plugin'" > "$ADDITIONAL_CONFIG_FILE_1"
echo " set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_2"
"$CURRENT_DIR/expect_successful_multiple_plugins_download" ||
fail_helper "[key-binding][multiple sourced files] plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding][multiple sourced files] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[key-binding][multiple sourced files] plugin download fails (tmux-copycat)"
teardown_helper
}
# SCRIPT TESTS
test_plugin_installation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-example-plugin" download success' ||
fail_helper "[script] plugin installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script] plugin download fails"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-example-plugin"' ||
fail_helper "[script] plugin already installed message fail"
teardown_helper
}
test_plugin_installation_custom_dir_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set-environment -g TMUX_PLUGIN_MANAGER_PATH '$CUSTOM_PLUGINS_DIR'
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-example-plugin" download success' ||
fail_helper "[script][custom dir] plugin installation fails"
check_dir_exists_helper "$CUSTOM_PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script][custom dir] plugin download fails"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-example-plugin"' ||
fail_helper "[script][custom dir] plugin already installed message fail"
teardown_helper
rm -rf "$CUSTOM_PLUGINS_DIR"
}
test_non_existing_plugin_installation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/non-existing-plugin"
run-shell "$TPM_DIR/tpm"
HERE
local expected_exit_code=1
script_run_helper "$TPM_DIR/bin/install_plugins" '"non-existing-plugin" download fail' "$expected_exit_code" ||
fail_helper "[script] non existing plugin installation doesn't fail"
teardown_helper
}
test_multiple_plugins_installation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
\ \ set -g @plugin 'tmux-plugins/tmux-copycat'
run-shell "$TPM_DIR/tpm"
HERE
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-example-plugin" download success' ||
fail_helper "[script] multiple plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[script] plugin download fails (tmux-copycat)"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-copycat"' ||
fail_helper "[script] multiple plugins already installed message fail"
teardown_helper
}
test_plugins_installation_from_sourced_file_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
source '$ADDITIONAL_CONFIG_FILE_1'
set -g @plugin 'tmux-plugins/tmux-example-plugin'
run-shell "$TPM_DIR/tpm"
HERE
mkdir ~/.tmux
echo "set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1"
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-copycat" download success' ||
fail_helper "[script][sourced file] plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script][sourced file] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[script][sourced file] plugin download fails (tmux-copycat)"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-copycat"' ||
fail_helper "[script][sourced file] plugins already installed message fail"
teardown_helper
}
test_plugins_installation_from_multiple_sourced_files_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
\ \ source '$ADDITIONAL_CONFIG_FILE_1'
source-file '$ADDITIONAL_CONFIG_FILE_2'
set -g @plugin 'tmux-plugins/tmux-example-plugin'
run-shell "$TPM_DIR/tpm"
HERE
mkdir ~/.tmux
echo " set -g @plugin 'tmux-plugins/tmux-copycat'" > "$ADDITIONAL_CONFIG_FILE_1"
echo "set -g @plugin 'tmux-plugins/tmux-sensible'" > "$ADDITIONAL_CONFIG_FILE_2"
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-sensible" download success' ||
fail_helper "[script][multiple sourced files] plugins installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script][multiple sourced files] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[script][multiple sourced files] plugin download fails (tmux-copycat)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-sensible/" ||
fail_helper "[script][multiple sourced files] plugin download fails (tmux-sensible)"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-sensible"' ||
fail_helper "[script][multiple sourced files] plugins already installed message fail"
teardown_helper
}
run_tests

View File

@ -0,0 +1,100 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PLUGINS_DIR="$HOME/.tmux/plugins"
TPM_DIR="$PWD"
source "$CURRENT_DIR/helpers/helpers.sh"
source "$CURRENT_DIR/helpers/tpm.sh"
# TMUX KEY-BINDING TESTS
test_plugin_installation_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @tpm_plugins "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
# opens tmux and test it with `expect`
$CURRENT_DIR/expect_successful_plugin_download ||
fail_helper "[key-binding] plugin installation fails"
# check plugin dir exists after download
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding] plugin download fails"
teardown_helper
}
test_legacy_and_new_syntax_for_plugin_installation_work_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @tpm_plugins " \
tmux-plugins/tmux-example-plugin \
"
set -g @plugin 'tmux-plugins/tmux-copycat'
run-shell "$TPM_DIR/tpm"
HERE
# opens tmux and test it with `expect`
"$CURRENT_DIR"/expect_successful_multiple_plugins_download ||
fail_helper "[key-binding] multiple plugins installation fails"
# check plugin dir exists after download
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[key-binding] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[key-binding] plugin download fails (tmux-copycat)"
teardown_helper
}
# SCRIPT TESTS
test_plugin_installation_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @tpm_plugins "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-example-plugin" download success' ||
fail_helper "[script] plugin installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script] plugin download fails"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-example-plugin"' ||
fail_helper "[script] plugin already installed message fail"
teardown_helper
}
test_legacy_and_new_syntax_for_plugin_installation_work_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @tpm_plugins " \
tmux-plugins/tmux-example-plugin \
"
set -g @plugin 'tmux-plugins/tmux-copycat'
run-shell "$TPM_DIR/tpm"
HERE
script_run_helper "$TPM_DIR/bin/install_plugins" '"tmux-example-plugin" download success' ||
fail_helper "[script] multiple plugin installation fails"
check_dir_exists_helper "$PLUGINS_DIR/tmux-example-plugin/" ||
fail_helper "[script] plugin download fails (tmux-example-plugin)"
check_dir_exists_helper "$PLUGINS_DIR/tmux-copycat/" ||
fail_helper "[script] plugin download fails (tmux-copycat)"
script_run_helper "$TPM_DIR/bin/install_plugins" 'Already installed "tmux-copycat"' ||
fail_helper "[script] multiple plugins already installed message fail"
teardown_helper
}
run_tests

View File

@ -0,0 +1,78 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TPM_DIR="$PWD"
PLUGINS_DIR="$HOME/.tmux/plugins"
CUSTOM_PLUGINS_DIR="$HOME/foo/plugins"
source "$CURRENT_DIR/helpers/helpers.sh"
source "$CURRENT_DIR/helpers/tpm.sh"
check_binding_defined() {
local binding="$1"
tmux list-keys | grep -q "$binding"
}
create_test_plugin_helper() {
local plugin_path="$PLUGINS_DIR/tmux_test_plugin/"
rm -rf "$plugin_path"
mkdir -p "$plugin_path"
while read line; do
echo "$line" >> "$plugin_path/test_plugin.tmux"
done
chmod +x "$plugin_path/test_plugin.tmux"
}
check_tpm_path() {
local correct_tpm_path="$1"
local tpm_path="$(tmux start-server\; show-environment -g TMUX_PLUGIN_MANAGER_PATH | cut -f2 -d=)"
[ "$correct_tpm_path" == "$tpm_path" ]
}
test_plugin_sourcing() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "doesnt_matter/tmux_test_plugin"
run-shell "$TPM_DIR/tpm"
HERE
# manually creates a local tmux plugin
create_test_plugin_helper <<- HERE
tmux bind-key R run-shell foo_command
HERE
tmux new-session -d # tmux starts detached
check_binding_defined "R run-shell foo_command" ||
fail_helper "Plugin sourcing fails"
teardown_helper
}
test_default_tpm_path() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
run-shell "$TPM_DIR/tpm"
HERE
check_tpm_path "${PLUGINS_DIR}/" ||
fail_helper "Default TPM path not correct"
teardown_helper
}
test_custom_tpm_path() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set-environment -g TMUX_PLUGIN_MANAGER_PATH '$CUSTOM_PLUGINS_DIR'
run-shell "$TPM_DIR/tpm"
HERE
check_tpm_path "$CUSTOM_PLUGINS_DIR" ||
fail_helper "Custom TPM path not correct"
teardown_helper
}
run_tests

View File

@ -0,0 +1,60 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TPM_DIR="$PWD"
PLUGINS_DIR="$HOME/.tmux/plugins"
source "$CURRENT_DIR/helpers/helpers.sh"
source "$CURRENT_DIR/helpers/tpm.sh"
manually_install_the_plugin() {
mkdir -p "$PLUGINS_DIR"
cd "$PLUGINS_DIR"
git clone --quiet https://github.com/tmux-plugins/tmux-example-plugin
}
# TMUX KEY-BINDING TESTS
test_plugin_update_via_tmux_key_binding() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
manually_install_the_plugin
"$CURRENT_DIR/expect_successful_update_of_all_plugins" ||
fail_helper "[key-binding] 'update all plugins' fails"
"$CURRENT_DIR/expect_successful_update_of_a_single_plugin" ||
fail_helper "[key-binding] 'update single plugin' fails"
teardown_helper
}
# SCRIPT TESTS
test_plugin_update_via_script() {
set_tmux_conf_helper <<- HERE
set -g mode-keys vi
set -g @plugin "tmux-plugins/tmux-example-plugin"
run-shell "$TPM_DIR/tpm"
HERE
manually_install_the_plugin
local expected_exit_code=1
script_run_helper "$TPM_DIR/bin/update_plugins" 'usage' "$expected_exit_code" ||
fail_helper "[script] running update plugins without args should fail"
script_run_helper "$TPM_DIR/bin/update_plugins tmux-example-plugin" '"tmux-example-plugin" update success' ||
fail_helper "[script] plugin update fails"
script_run_helper "$TPM_DIR/bin/update_plugins all" '"tmux-example-plugin" update success' ||
fail_helper "[script] update all plugins fails"
teardown_helper
}
run_tests

81
config/tmux/plugins/tpm/tpm Executable file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BINDINGS_DIR="$CURRENT_DIR/bindings"
SCRIPTS_DIR="$CURRENT_DIR/scripts"
source "$SCRIPTS_DIR/variables.sh"
get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value="$(tmux show-option -gqv "$option")"
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
tpm_path_set() {
tmux show-environment -g "$DEFAULT_TPM_ENV_VAR_NAME" >/dev/null 2>&1
}
# Check if configuration file exists at an XDG-compatible location, if so use
# that directory for TMUX_PLUGIN_MANAGER_PATH. Otherwise use $DEFAULT_TPM_PATH.
set_default_tpm_path() {
local xdg_tmux_path="${XDG_CONFIG_HOME:-$HOME/.config}/tmux"
local tpm_path="$DEFAULT_TPM_PATH"
if [ -f "$xdg_tmux_path/tmux.conf" ]; then
tpm_path="$xdg_tmux_path/plugins/"
fi
tmux set-environment -g "$DEFAULT_TPM_ENV_VAR_NAME" "$tpm_path"
}
# Ensures TMUX_PLUGIN_MANAGER_PATH global env variable is set.
#
# Put this in `.tmux.conf` to override the default:
# `set-environment -g TMUX_PLUGIN_MANAGER_PATH "/some/other/path/"`
set_tpm_path() {
if ! tpm_path_set; then
set_default_tpm_path
fi
}
# 1. Fetches plugin names from `@plugin` variables
# 2. Creates full plugin path
# 3. Sources all *.tmux files from each of the plugin directories
# - no errors raised if directory does not exist
# Files are sourced as tmux config files, not as shell scripts!
source_plugins() {
"$SCRIPTS_DIR/source_plugins.sh" >/dev/null 2>&1
}
# prefix + I - downloads TPM plugins and reloads TMUX environment
# prefix + U - updates a plugin (or all of them) and reloads TMUX environment
# prefix + alt + u - remove unused TPM plugins and reloads TMUX environment
set_tpm_key_bindings() {
local install_key="$(get_tmux_option "$install_key_option" "$default_install_key")"
tmux bind-key "$install_key" run-shell "$BINDINGS_DIR/install_plugins"
local update_key="$(get_tmux_option "$update_key_option" "$default_update_key")"
tmux bind-key "$update_key" run-shell "$BINDINGS_DIR/update_plugins"
local clean_key="$(get_tmux_option "$clean_key_option" "$default_clean_key")"
tmux bind-key "$clean_key" run-shell "$BINDINGS_DIR/clean_plugins"
}
supported_tmux_version_ok() {
"$SCRIPTS_DIR/check_tmux_version.sh" "$SUPPORTED_TMUX_VERSION"
}
main() {
if supported_tmux_version_ok; then
set_tpm_path
set_tpm_key_bindings
source_plugins
fi
}
main

View File

@ -0,0 +1 @@
doc/tags

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Chris Toomey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,506 @@
Vim Tmux Navigator
==================
This plugin is a repackaging of [Mislav Marohnić's](https://mislav.net/) tmux-navigator
configuration described in [this gist][]. When combined with a set of tmux
key bindings, the plugin will allow you to navigate seamlessly between
vim and tmux splits using a consistent set of hotkeys.
**NOTE**: This requires tmux v1.8 or higher.
Usage
-----
This plugin provides the following mappings which allow you to move between
Vim panes and tmux splits seamlessly.
- `<ctrl-h>` => Left
- `<ctrl-j>` => Down
- `<ctrl-k>` => Up
- `<ctrl-l>` => Right
- `<ctrl-\>` => Previous split
**Note** - you don't need to use your tmux `prefix` key sequence before using
the mappings.
If you want to use alternate key mappings, see the [configuration section
below][].
Installation
------------
### Vim
If you don't have a preferred installation method, I recommend using [Vundle][].
Assuming you have Vundle installed and configured, the following steps will
install the plugin:
Add the following line to your `~/.vimrc` file
``` vim
Plugin 'christoomey/vim-tmux-navigator'
```
Then run
```
:PluginInstall
```
If you are using Vim 8+, you don't need any plugin manager. Simply clone this repository inside `~/.vim/pack/plugin/start/` directory and restart Vim.
```
git clone git@github.com:christoomey/vim-tmux-navigator.git ~/.vim/pack/plugins/start/vim-tmux-navigator
```
### lazy.nvim
If you are using [lazy.nvim](https://github.com/folke/lazy.nvim). Add the following plugin to your configuration.
```lua
{
"christoomey/vim-tmux-navigator",
cmd = {
"TmuxNavigateLeft",
"TmuxNavigateDown",
"TmuxNavigateUp",
"TmuxNavigateRight",
"TmuxNavigatePrevious",
"TmuxNavigatorProcessList",
},
keys = {
{ "<c-h>", "<cmd><C-U>TmuxNavigateLeft<cr>" },
{ "<c-j>", "<cmd><C-U>TmuxNavigateDown<cr>" },
{ "<c-k>", "<cmd><C-U>TmuxNavigateUp<cr>" },
{ "<c-l>", "<cmd><C-U>TmuxNavigateRight<cr>" },
{ "<c-\\>", "<cmd><C-U>TmuxNavigatePrevious<cr>" },
},
}
```
Then, restart Neovim and lazy.nvim will automatically install the plugin and configure the keybindings.
### tmux
To configure the tmux side of this customization there are two options:
#### Add a snippet
Add the following to your `~/.tmux.conf` file:
``` tmux
# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'
tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
"bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\' 'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
"bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\' 'select-pane -l'"
bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R
bind-key -T copy-mode-vi 'C-\' select-pane -l
```
#### TPM
If you prefer, you can use the Tmux Plugin Manager ([TPM][]) instead of
copying the snippet.
When using TPM, add the following lines to your ~/.tmux.conf:
``` tmux
set -g @plugin 'christoomey/vim-tmux-navigator'
```
To set a different key-binding, use the plugin configuration settings
(remember to update your vim config accordingly).
Multiple key bindings are possible, use a space to separate.
``` tmux
set -g @vim_navigator_mapping_left "C-Left C-h" # use C-h and C-Left
set -g @vim_navigator_mapping_right "C-Right C-l"
set -g @vim_navigator_mapping_up "C-k"
set -g @vim_navigator_mapping_down "C-j"
set -g @vim_navigator_mapping_prev "" # removes the C-\ binding
```
To disable the automatic mapping of `<prefix> C-l` to `send C-l` (which is
intended to restore the "clear screen" functionality):
```tmux
set -g @vim_navigator_prefix_mapping_clear_screen ""
```
Don't forget to run tpm:
``` tmux
run '~/.tmux/plugins/tpm/tpm'
```
Thanks to Christopher Sexton who provided the updated tmux configuration in
[this blog post][].
Configuration
-------------
### Custom Key Bindings
If you don't want the plugin to create any mappings, you can use the five
provided functions to define your own custom maps. You will need to define
custom mappings in your `~/.vimrc` as well as update the bindings in tmux to
match.
#### Vim
Add the following to your `~/.vimrc` to define your custom maps:
``` vim
let g:tmux_navigator_no_mappings = 1
nnoremap <silent> {Left-Mapping} :<C-U>TmuxNavigateLeft<cr>
nnoremap <silent> {Down-Mapping} :<C-U>TmuxNavigateDown<cr>
nnoremap <silent> {Up-Mapping} :<C-U>TmuxNavigateUp<cr>
nnoremap <silent> {Right-Mapping} :<C-U>TmuxNavigateRight<cr>
nnoremap <silent> {Previous-Mapping} :<C-U>TmuxNavigatePrevious<cr>
```
*Note* Each instance of `{Left-Mapping}` or `{Down-Mapping}` must be replaced
in the above code with the desired mapping. Ie, the mapping for `<ctrl-h>` =>
Left would be created with `nnoremap <silent> <c-h> :<C-U>TmuxNavigateLeft<cr>`.
##### Autosave on leave
You can configure the plugin to write the current buffer, or all buffers, when
navigating from Vim to tmux. This functionality is exposed via the
`g:tmux_navigator_save_on_switch` variable, which can have either of the
following values:
Value | Behavior
------ | ------
1 | `:update` (write the current buffer, but only if changed)
2 | `:wall` (write all buffers)
To enable this, add the following (with the desired value) to your ~/.vimrc:
```vim
" Write all buffers before navigating from Vim to tmux pane
let g:tmux_navigator_save_on_switch = 2
```
##### Disable While Zoomed
By default, if you zoom the tmux pane running Vim and then attempt to navigate
"past" the edge of the Vim session, tmux will unzoom the pane. This is the
default tmux behavior, but may be confusing if you've become accustomed to
navigation "wrapping" around the sides due to this plugin.
We provide an option, `g:tmux_navigator_disable_when_zoomed`, which can be used
to disable this unzooming behavior, keeping all navigation within Vim until the
tmux pane is explicitly unzoomed.
To disable navigation when zoomed, add the following to your ~/.vimrc:
```vim
" Disable tmux navigator when zooming the Vim pane
let g:tmux_navigator_disable_when_zoomed = 1
```
##### Preserve Zoom
As noted above, navigating from a Vim pane to another tmux pane normally causes
the window to be unzoomed. Some users may prefer the behavior of tmux's `-Z`
option to `select-pane`, which keeps the window zoomed if it was zoomed. To
enable this behavior, set the `g:tmux_navigator_preserve_zoom` option to `1`:
```vim
" If the tmux window is zoomed, keep it zoomed when moving from Vim to another pane
let g:tmux_navigator_preserve_zoom = 1
```
Naturally, if `g:tmux_navigator_disable_when_zoomed` is enabled, this option
will have no effect.
#### Tmux
Alter each of the five lines of the tmux configuration listed above to use your
custom mappings. **Note** each line contains two references to the desired
mapping.
### Additional Customization
#### Ignoring programs that use Ctrl+hjkl movement
In interactive programs such as FZF or the built-in Vim terminal, Ctrl+hjkl can be used instead of the arrow keys to move the selection up and down. If vim-tmux-navigator is getting in your way trying to change the active window instead, you can make it be ignored and work as if this plugin were not enabled. Just modify the `is_vim` variable(that you have either on the snipped you pasted on `~/.tmux.conf` or on the `vim-tmux-navigator.tmux` file). For example, to add the program `foobar`:
```diff
- is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
+ is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf|foobar)(diff)?$'"
```
#### Restoring Clear Screen (C-l)
The default key bindings include `<Ctrl-l>` which is the readline key binding
for clearing the screen. The following binding can be added to your `~/.tmux.conf` file to provide an alternate mapping to `clear-screen`.
``` tmux
bind C-l send-keys 'C-l'
```
With this enabled you can use `<prefix> C-l` to clear the screen.
Thanks to [Brian Hogan][] for the tip on how to re-map the clear screen binding.
#### Restoring SIGQUIT (C-\\)
The default key bindings also include `<Ctrl-\>` which is the default method of
sending SIGQUIT to a foreground process. Similar to "Clear Screen" above, a key
binding can be created to replicate SIGQUIT in the prefix table.
``` tmux
bind C-\\ send-keys 'C-\'
```
Alternatively, you can exclude the previous pane key binding from your `~/.tmux.conf`. If using TPM, the following line can be used to unbind the previous pane binding set by the plugin.
``` tmux
unbind -n C-\\
```
#### Disable Wrapping
By default, if you try to move past the edge of the screen, tmux/vim will
"wrap" around to the opposite side. To disable this, you'll need to
configure both tmux and vim:
For vim, you only need to enable this option:
```vim
let g:tmux_navigator_no_wrap = 1
```
Tmux doesn't have an option, so whatever key bindings you have need to be set
to conditionally wrap based on position on screen:
```tmux
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" { send-keys C-h } { if-shell -F '#{pane_at_left}' {} { select-pane -L } }
bind-key -n 'C-j' if-shell "$is_vim" { send-keys C-j } { if-shell -F '#{pane_at_bottom}' {} { select-pane -D } }
bind-key -n 'C-k' if-shell "$is_vim" { send-keys C-k } { if-shell -F '#{pane_at_top}' {} { select-pane -U } }
bind-key -n 'C-l' if-shell "$is_vim" { send-keys C-l } { if-shell -F '#{pane_at_right}' {} { select-pane -R } }
bind-key -T copy-mode-vi 'C-h' if-shell -F '#{pane_at_left}' {} { select-pane -L }
bind-key -T copy-mode-vi 'C-j' if-shell -F '#{pane_at_bottom}' {} { select-pane -D }
bind-key -T copy-mode-vi 'C-k' if-shell -F '#{pane_at_top}' {} { select-pane -U }
bind-key -T copy-mode-vi 'C-l' if-shell -F '#{pane_at_right}' {} { select-pane -R }
```
#### Nesting
If you like to nest your tmux sessions, this plugin is not going to work
properly. It probably never will, as it would require detecting when Tmux would
wrap from one outermost pane to another and propagating that to the outer
session.
By default this plugin works on the outermost tmux session and the vim
sessions it contains, but you can customize the behaviour by adding more
commands to the expression used by the grep command.
When nesting tmux sessions via ssh or mosh, you could extend it to look like
`'(^|\/)g?(view|vim|ssh|mosh?)(diff)?$'`, which makes this plugin work within
the innermost tmux session and the vim sessions within that one. This works
better than the default behaviour if you use the outer Tmux sessions as relays
to different hosts and have all instances of vim on remote hosts.
Similarly, if you like to nest tmux locally, add `|tmux` to the expression.
This behaviour means that you can't leave the innermost session with Ctrl-hjkl
directly. These following fallback mappings can be targeted to the right Tmux
session by escaping the prefix (Tmux' `send-prefix` command).
``` tmux
bind -r C-h run "tmux select-pane -L"
bind -r C-j run "tmux select-pane -D"
bind -r C-k run "tmux select-pane -U"
bind -r C-l run "tmux select-pane -R"
bind -r C-\ run "tmux select-pane -l"
```
Another workaround is to configure tmux on the outer machine to send keys to
the inner tmux session:
```
bind-key -n 'M-h' 'send-keys c-h'
bind-key -n 'M-j' 'send-keys c-j'
bind-key -n 'M-k' 'send-keys c-k'
bind-key -n 'M-l' 'send-keys c-l'
```
Here we bind "meta" key (aka "alt" or "option" key) combinations for each of
the four directions and send those along to the innermost session via
`send-keys`. You use the normal `C-h,j,k,l` while in the outermost session and
the alternative bindings to navigate the innermost session. Note that if you
use the example above on a Mac, you may need to configure your terminal app to
get the option key to work like a normal meta key. Consult your terminal app's
manual for details.
A third possible solution is to manually prevent the outermost tmux session
from intercepting the navigation keystrokes by disabling the prefix table:
```
set -g pane-active-border-style 'fg=#000000,bg=#ffff00'
bind -T root F12 \
set prefix None \;\
set key-table off \;\
if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
set -g pane-active-border-style 'fg=#000000,bg=#00ff00'
refresh-client -S \;\
bind -T off F12 \
set -u prefix \;\
set -u key-table \;\
set -g pane-active-border-style 'fg=#000000,bg=#ffff00'
refresh-client -S
```
This code, added to the machine running the outermost tmux session, toggles the
outermost prefix table on and off with the `F12` key. When off, the active
pane's border changes to green to indicate that the inner session receives
navigation keystrokes. When toggled back on, the border returns to yellow and
normal operation resumes and the outermost responds to the nav keystrokes.
The code example above also toggles the prefix key (ctrl-b by default) for the
outer session so that same prefix can be temporarily used on the inner session
instead of having to use a different prefix (ctrl-a by default) which you may
find convenient. If not, simply remove the lines that set/unset the prefix key
from the code example above.
#### netrw
Vim's builtin file explorer, named the netrw plugin, has a default keymapping
for `<C-l>`. When using `vim-tmux-navigator` with default settings,
`vim-tmux-navigator` will try to override the netrw mapping so that `<C-l>` will
still be mapped to `:TmuxNavigateRight` as it is for other buffers. If you
prefer to keep the netrw mapping, set this variable in your vimrc:
``` vim
let g:tmux_navigator_disable_netrw_workaround = 1
```
Alternatively, if you prefer to work around the issue yourself, you can add the
following to your vimrc:
``` vim
let g:tmux_navigator_disable_netrw_workaround = 1
" g:Netrw_UserMaps is a list of lists. If you'd like to add other key mappings,
" just add them like so: [['a', 'command1'], ['b', 'command2'], ...]
let g:Netrw_UserMaps = [['<C-l>', '<C-U>TmuxNavigateRight<cr>']]
```
Troubleshooting
---------------
### Vim -> Tmux doesn't work!
This is likely due to conflicting key mappings in your `~/.vimrc`. You can check
this by running `:verbose nmap <C-h>` (similar for each of the key bindings).
You should see vim-tmux-runner as the source listed for the key binding, but if
you see something else, you've got a conflict and will need to remove the other
key binding or otherwise restructure your vim config.
Another option is that the pattern matching included in the `.tmux.conf` is
not recognizing that Vim is active. To check that tmux is properly recognizing
Vim, use the provided Vim command `:TmuxNavigatorProcessList`. The output of
that command should be a list like:
```
Ss -zsh
S+ vim
S+ tmux
```
If you encounter a different output please [open an issue][] with as much info
about your OS, Vim version, and tmux version as possible.
[open an issue]: https://github.com/christoomey/vim-tmux-navigator/issues/new
### Tmux Can't Tell if Vim Is Active
This functionality requires tmux version 1.8 or higher. You can check your
version to confirm with this shell command:
``` bash
tmux -V # should return 'tmux 1.8'
```
### Switching out of Vim Is Slow
If you find that navigation within Vim (from split to split) is fine, but Vim
to a non-Vim tmux pane is delayed, it might be due to a slow shell startup.
Consider moving code from your shell's non-interactive rc file (e.g.,
`~/.zshenv`) into the interactive startup file (e.g., `~/.zshrc`) as Vim only
sources the non-interactive config.
### It doesn't work in Vim's `terminal` mode
Terminal mode is now supported :)
### It Doesn't Work in tmate
[tmate][] is a tmux fork that aids in setting up remote pair programming
sessions. It is designed to run alongside tmux without issue, but occasionally
there are hiccups. Specifically, if the versions of tmux and tmate don't match,
you can have issues. See [this
issue](https://github.com/christoomey/vim-tmux-navigator/issues/27) for more
detail.
[tmate]: http://tmate.io/
### Switching between host panes doesn't work when docker is running
Images built from minimalist OSes may not have the `ps` command or have a
simpler version of the command that is not compatible with this plugin.
Try installing the `procps` package using the appropriate package manager
command. For Alpine, you would do `apk add procps`.
If this doesn't solve your problem, you can also try the following:
Replace the `is_vim` variable in your `~/.tmux.conf` file with:
```tmux
if-shell '[ -f /.dockerenv ]' \
"is_vim=\"ps -o state=,comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?)(diff)?$'\""
# Filter out docker instances of nvim from the host system to prevent
# host from thinking nvim is running in a pseudoterminal when its not.
"is_vim=\"ps -o state=,comm=,cgroup= -t '#{pane_tty}' \
| grep -ivE '^.+ +.+ +.+\\/docker\\/.+$' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?)(diff)? +'\""
```
Details: The output of the ps command on the host system includes processes
running within containers, but containers have their own instances of
/dev/pts/\*. vim-tmux-navigator relies on /dev/pts/\* to determine if vim is
running, so if vim is running in say /dev/pts/<N> in a container and there is a
tmux pane (not running vim) in /dev/pts/<N> on the host system, then without
the patch above vim-tmux-navigator will think vim is running when its not.
### It Still Doesn't Work!!!
The tmux configuration uses an inlined grep pattern match to help determine if
the current pane is running Vim. If you run into any issues with the navigation
not happening as expected, you can try using [Mislav's original external
script][] which has a more robust check.
[Brian Hogan]: https://twitter.com/bphogan
[Mislav's original external script]: https://github.com/mislav/dotfiles/blob/master/bin/tmux-vim-select-pane
[Vundle]: https://github.com/gmarik/vundle
[TPM]: https://github.com/tmux-plugins/tpm
[configuration section below]: #custom-key-bindings
[this blog post]: http://www.codeography.com/2013/06/19/navigating-vim-and-tmux-splits
[this gist]: https://gist.github.com/mislav/5189704

View File

@ -0,0 +1,42 @@
*tmux-navigator.txt* Plugin to allow seamless navigation between tmux and vim
==============================================================================
CONTENTS *tmux-navigator-contents*
==============================================================================
INTRODUCTION *tmux-navigator*
Vim-tmux-navigator is a little plugin which enables seamless navigation
between tmux panes and vim splits. This plugin is a repackaging of Mislav
Marohinc's tmux=navigator configuration. When combined with a set of tmux key
bindings, the plugin will allow you to navigate seamlessly between vim and
tmux splits using a consistent set of hotkeys.
NOTE: This requires tmux v1.8 or higher.
==============================================================================
CONFIGURATION *tmux-navigator-configuration*
* Activate autoupdate on exit
let g:tmux_navigator_save_on_switch = 1
* Disable vim->tmux navigation when the Vim pane is zoomed in tmux
let g:tmux_navigator_disable_when_zoomed = 1
* If the Vim pane is zoomed, stay zoomed when moving to another tmux pane
let g:tmux_navigator_preserve_zoom = 1
* Custom Key Bindings
let g:tmux_navigator_no_mappings = 1
nnoremap <silent> {Left-mapping} :<C-U>TmuxNavigateLeft<cr>
nnoremap <silent> {Down-Mapping} :<C-U>TmuxNavigateDown<cr>
nnoremap <silent> {Up-Mapping} :<C-U>TmuxNavigateUp<cr>
nnoremap <silent> {Right-Mapping} :<C-U>TmuxNavigateRight<cr>
nnoremap <silent> {Previous-Mapping} :<C-U><C-U>TmuxNavigatePrevious<cr>
* Disable the <C-l> remapping on netrw buffers (use netrw's mapping)
let g:tmux_navigator_disable_netrw_workaround = 1
vim:tw=78:ts=8:ft=help:norl:

View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
#
# Collection of various test strings that could be the output of the tmux
# 'pane_current_comamnd' message. Included as regression test for updates to
# the inline grep pattern used in the `.tmux.conf` configuration
set -e
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0)
vim_pattern='(^|\/)g?(view|l?n?vim?x?|fzf)(diff)?$'
match_tests=(vim Vim VIM vimdiff lvim /usr/local/bin/vim vi gvim view gview nvim vimx fzf)
no_match_tests=( /Users/christoomey/.vim/thing /usr/local/bin/start-vim )
display_matches() {
for process_name in "$@"; do
printf "%s %s\n" "$(matches_vim_pattern $process_name)" "$process_name"
done
}
matches_vim_pattern() {
if echo "$1" | grep -iqE "$vim_pattern"; then
echo "${GREEN}match${NORMAL}"
else
echo "${RED}fail${NORMAL}"
fi
}
main() {
echo "Testing against pattern: ${YELLOW}$vim_pattern${NORMAL}\n"
echo "These should all ${GREEN}match${NORMAL}\n----------------------"
display_matches "${match_tests[@]}"
echo "\nThese should all ${RED}fail${NORMAL}\n---------------------"
display_matches "${no_match_tests[@]}"
}
main

View File

@ -0,0 +1,158 @@
" Maps <C-h/j/k/l> to switch vim splits in the given direction. If there are no more windows in that direction, forwards the operation to tmux.
" Additionally, <C-\> toggles between last active vim splits/tmux panes.
if exists("g:loaded_tmux_navigator") || &cp || v:version < 700
finish
endif
let g:loaded_tmux_navigator = 1
function! s:VimNavigate(direction)
try
execute 'wincmd ' . a:direction
catch
echohl ErrorMsg | echo 'E11: Invalid in command-line window; <CR> executes, CTRL-C quits: wincmd k' | echohl None
endtry
endfunction
if !get(g:, 'tmux_navigator_no_mappings', 0)
nnoremap <silent> <c-h> :<C-U>TmuxNavigateLeft<cr>
nnoremap <silent> <c-j> :<C-U>TmuxNavigateDown<cr>
nnoremap <silent> <c-k> :<C-U>TmuxNavigateUp<cr>
nnoremap <silent> <c-l> :<C-U>TmuxNavigateRight<cr>
nnoremap <silent> <c-\> :<C-U>TmuxNavigatePrevious<cr>
if !empty($TMUX)
function! IsFZF()
return &ft == 'fzf'
endfunction
tnoremap <expr> <silent> <C-h> IsFZF() ? "\<C-h>" : "\<C-w>:\<C-U> TmuxNavigateLeft\<cr>"
tnoremap <expr> <silent> <C-j> IsFZF() ? "\<C-j>" : "\<C-w>:\<C-U> TmuxNavigateDown\<cr>"
tnoremap <expr> <silent> <C-k> IsFZF() ? "\<C-k>" : "\<C-w>:\<C-U> TmuxNavigateUp\<cr>"
tnoremap <expr> <silent> <C-l> IsFZF() ? "\<C-l>" : "\<C-w>:\<C-U> TmuxNavigateRight\<cr>"
endif
if !get(g:, 'tmux_navigator_disable_netrw_workaround', 0)
if !exists('g:Netrw_UserMaps')
let g:Netrw_UserMaps = [['<C-l>', '<C-U>TmuxNavigateRight<cr>']]
else
echohl ErrorMsg | echo 'vim-tmux-navigator conflicts with netrw <C-l> mapping. See https://github.com/christoomey/vim-tmux-navigator#netrw or add `let g:tmux_navigator_disable_netrw_workaround = 1` to suppress this warning.' | echohl None
endif
endif
endif
if empty($TMUX)
command! TmuxNavigateLeft call s:VimNavigate('h')
command! TmuxNavigateDown call s:VimNavigate('j')
command! TmuxNavigateUp call s:VimNavigate('k')
command! TmuxNavigateRight call s:VimNavigate('l')
command! TmuxNavigatePrevious call s:VimNavigate('p')
finish
endif
command! TmuxNavigateLeft call s:TmuxAwareNavigate('h')
command! TmuxNavigateDown call s:TmuxAwareNavigate('j')
command! TmuxNavigateUp call s:TmuxAwareNavigate('k')
command! TmuxNavigateRight call s:TmuxAwareNavigate('l')
command! TmuxNavigatePrevious call s:TmuxAwareNavigate('p')
if !exists("g:tmux_navigator_save_on_switch")
let g:tmux_navigator_save_on_switch = 0
endif
if !exists("g:tmux_navigator_disable_when_zoomed")
let g:tmux_navigator_disable_when_zoomed = 0
endif
if !exists("g:tmux_navigator_preserve_zoom")
let g:tmux_navigator_preserve_zoom = 0
endif
if !exists("g:tmux_navigator_no_wrap")
let g:tmux_navigator_no_wrap = 0
endif
let s:pane_position_from_direction = {'h': 'left', 'j': 'bottom', 'k': 'top', 'l': 'right'}
function! s:TmuxOrTmateExecutable()
return (match($TMUX, 'tmate') != -1 ? 'tmate' : 'tmux')
endfunction
function! s:TmuxVimPaneIsZoomed()
return s:TmuxCommand("display-message -p '#{window_zoomed_flag}'") == 1
endfunction
function! s:TmuxSocket()
" The socket path is the first value in the comma-separated list of $TMUX.
return split($TMUX, ',')[0]
endfunction
function! s:TmuxCommand(args)
let cmd = s:TmuxOrTmateExecutable() . ' -S ' . s:TmuxSocket() . ' ' . a:args
let l:x=&shellcmdflag
let &shellcmdflag='-c'
let retval=system(cmd)
let &shellcmdflag=l:x
return retval
endfunction
function! s:TmuxNavigatorProcessList()
echo s:TmuxCommand("run-shell 'ps -o state= -o comm= -t ''''#{pane_tty}'''''")
endfunction
command! TmuxNavigatorProcessList call s:TmuxNavigatorProcessList()
let s:tmux_is_last_pane = 0
augroup tmux_navigator
au!
autocmd WinEnter * let s:tmux_is_last_pane = 0
augroup END
function! s:NeedsVitalityRedraw()
return exists('g:loaded_vitality') && v:version < 704 && !has("patch481")
endfunction
function! s:ShouldForwardNavigationBackToTmux(tmux_last_pane, at_tab_page_edge)
if g:tmux_navigator_disable_when_zoomed && s:TmuxVimPaneIsZoomed()
return 0
endif
return a:tmux_last_pane || a:at_tab_page_edge
endfunction
function! s:TmuxAwareNavigate(direction)
let nr = winnr()
let tmux_last_pane = (a:direction == 'p' && s:tmux_is_last_pane)
if !tmux_last_pane
call s:VimNavigate(a:direction)
endif
let at_tab_page_edge = (nr == winnr())
" Forward the switch panes command to tmux if:
" a) we're toggling between the last tmux pane;
" b) we tried switching windows in vim but it didn't have effect.
if s:ShouldForwardNavigationBackToTmux(tmux_last_pane, at_tab_page_edge)
if g:tmux_navigator_save_on_switch == 1
try
update " save the active buffer. See :help update
catch /^Vim\%((\a\+)\)\=:E32/ " catches the no file name error
endtry
elseif g:tmux_navigator_save_on_switch == 2
try
wall " save all the buffers. See :help wall
catch /^Vim\%((\a\+)\)\=:E141/ " catches the no file name error
endtry
endif
let args = 'select-pane -t ' . shellescape($TMUX_PANE) . ' -' . tr(a:direction, 'phjkl', 'lLDUR')
if g:tmux_navigator_preserve_zoom == 1
let l:args .= ' -Z'
endif
if g:tmux_navigator_no_wrap == 1
let args = 'if -F "#{pane_at_' . s:pane_position_from_direction[a:direction] . '}" "" "' . args . '"'
endif
silent call s:TmuxCommand(args)
if s:NeedsVitalityRedraw()
redraw!
endif
let s:tmux_is_last_pane = 1
else
let s:tmux_is_last_pane = 0
endif
endfunction

View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
get_tmux_option() {
local option value default
option="$1"
default="$2"
# NOTE: Older tmux versions (eg. 3.2a on Ubuntu 22.04) do not exit with an
# error code when an option is not defined. Therefore we need to first
# test if the option exists, and only then try to get its value or fall
# back to the default.
value="$([[ -n $(tmux show-options -gq "$option") ]] \
&& tmux show-option -gqv "$option" \
|| echo "$default")"
# Deprecated, for backward compatibility
if [[ $value == 'null' ]]; then
echo ""
return
fi
echo "$value"
}
bind_key_vim() {
local key tmux_cmd is_vim
key="$1"
tmux_cmd="$2"
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|l?n?vim?x?|fzf)(diff)?$'"
# sending C-/ according to https://github.com/tmux/tmux/issues/1827
tmux bind-key -n "$key" if-shell "$is_vim" "send-keys '$key'" "$tmux_cmd"
# tmux < 3.0 cannot parse "$tmux_cmd" as one argument, thus copying as multiple arguments
tmux bind-key -T copy-mode-vi "$key" $tmux_cmd
}
move_left="$(get_tmux_option "@vim_navigator_mapping_left" 'C-h')"
move_right="$(get_tmux_option "@vim_navigator_mapping_right" 'C-l')"
move_up="$(get_tmux_option "@vim_navigator_mapping_up" 'C-k')"
move_down="$(get_tmux_option "@vim_navigator_mapping_down" 'C-j')"
move_prev="$(get_tmux_option "@vim_navigator_mapping_prev" 'C-\')"
for k in $(echo "$move_left"); do bind_key_vim "$k" "select-pane -L"; done
for k in $(echo "$move_down"); do bind_key_vim "$k" "select-pane -D"; done
for k in $(echo "$move_up"); do bind_key_vim "$k" "select-pane -U"; done
for k in $(echo "$move_right"); do bind_key_vim "$k" "select-pane -R"; done
for k in $(echo "$move_prev"); do bind_key_vim "$k" "select-pane -l"; done
# Restoring clear screen
clear_screen="$(get_tmux_option "@vim_navigator_prefix_mapping_clear_screen" 'C-l')"
for k in $(echo "$clear_screen"); do tmux bind "$k" send-keys 'C-l'; done

View File

@ -61,4 +61,15 @@ set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'christoomey/vim-tmux-navigator'
run '~/.tmux/plugins/tpm/tpm'
# Pick a softer palette.
set -g @catppuccin_flavor 'mocha'
run ~/.config/tmux/plugins/catppuccin/tmux/catppuccin.tmux
# Make the status line more pleasant.
set -g status-left ""
set -g status-right '#[fg=#{@thm_crust},bg=#{@thm_teal}] session: #S '
# Ensure that everything on the right side of the status line
# is included.
set -g status-right-length 100

View File

@ -13,9 +13,9 @@ fi
echo Current hostname: $current_host
sed -i "s/#$current_host: //" .config/hypr/hyprland.conf
sed -i "s/#$current_host: //" config/hypr/hyprland.conf
for file in ".config"/*; do
for file in "config"/*; do
echo "rsync -av $file ~/.config --delete"
rsync -av $file ~/.config --delete
done
@ -24,4 +24,4 @@ DISPLAY=:0 kitty metapac sync
# Cleanup
sed -i "s/^monitor/#$current_host: monitor/" .config/hypr/hyprland.conf
sed -i "s/^monitor/#$current_host: monitor/" config/hypr/hyprland.conf