It's more convenient to have all of this little details that you normally do day-to-day already pre-scripted - especially if you use it on a computer that you shutdown in a day-to-day fashion.

That's why I decided to improve my day-to-day blogging experience with tmux and use Tmuxp.

Tmuxp is a tmux session manager created with python. It uses tmuxinator and teamocil style as it says in README docs.

I'm amused how much features it contains!

To The Point

Installation of Tmuxp

As my personal preference I use pipsi to install some of the script-alike stuff - just like a tmuxp:

pipsi install tmuxp

You can also install tmuxp with pip (in a virtualenv if you prefer, or not, up to you):

pip install tmuxp

Usage of Tmuxp

Once you will create your first configuration, you can load it as a tmux session with:

tmuxp load yourtmuxconfig.yaml # or yourtmuxconfig.json

For the example below, it will create 4 panes that are tiled - meanig as a 4 equaly defined boxes.

BTW - did you know that if you have already one session with the same name as the one you are loading, tmuxp will ask you if you want to attach to that session?

That's very convenient! So instead of making tmux attach -t nameofsession you can actually use the tmux load yourconfig.json and it will attach after answering y to that created session.

Yaml and Json config files

So in Tmuxp you can actually use two type of files - yaml and json.

Simple example of that:

YAML:

session_name: 4-pane-split
windows:
- window_name: dev window
  layout: tiled
  shell_command_before:
    - cd ~/
  panes:
    - shell_command:
        - cd /var/log
        - ls -al | grep \.log
    - echo hello
    - echo hello
    - echo hello

JSON:

{
"windows": [
    {
        "panes": [
            {
            "shell_command": [
                "cd /var/log", 
                "ls -al | grep \\.log"
            ]
            }, 
            "echo hello",
            "echo hello",
            "echo hello"
        ], 
        "shell_command_before": [
            "cd ~/"
        ], 
        "layout": "tiled", 
        "window_name": "dev window"
    }
], 
"session_name": "4-pane-split"
}

So comparing those two - it feels like YAML is easier to use and less coding needed to have a working solution. But with my vim and python experience I find it less handy. It's probably because I have predefined strangly tab to always insert as 4 spaces no matter what type of file I'm editing (python requirements). Yeah I will fix that someday, but for now it makes my experience with YAML files less pleasant so I'd better be off with JSON as a personal preference.

Configuration

Tmuxp configuration consists of sessions and windows and panes.

Sessions and windows

Your sessions are at the bottom.

Creating new session with one pane and only session name using JSON file:

{
"windows":[ ],
"session_name": "4-pane-split"
}

Multiple Windows

{
"windows":[
    {
        "panes": []
    },{
        "panes": []
    }
],
"session_name": "2-pane-split"
}

Multiple Windows with focus specified

{
"windows":[
    {
        "panes": [
            "echo 'first pane'"
        ]
    },
    {
        "panes": [
            "echo 'second pane'"
        ],
        "focus": true
    },
    {
        "panes": [
            "echo 'third pane'"
        ]
    }
],
"session_name": "focus-pane"
}

Configuration options that you can use

  • shell_command_before - this is used before making call on each of the pane's scripts.
  • window_name - this is a name for each of the windows that are within this session.
  • suppress_history - to override default comamnd suppression.
  • window_index - if you want to change the default sorting and placement of windows at your session
  • start_directory - you can globally use this variable as well as locally for specific pane scripts and nesting one to another as you can see in my configuration
  • focus - option to make one of the windows and panes to be focused after loading config - unfortunatelly it did not worked for me and as listed in this issue, i'm not the only one having this bug.
  • options and global_options- this contains changing settings (per window or globally) for:
    • automatic-rename - you can set if you want your panes and windows to autotically change name or not
    • default-shell - you can set your default shell that will be run on panes
    • default-command - you can set your default command on panes
    • main-pane-height - you can set height of pane.

There is probably a lot more but that should be enough to make some sophisticated tmux session configuration for tmuxp.

Check out more at the example section in documentation of tmuxp

My personal usage for creating this blog

For now I decided create tmuxp config like that:

{
    "start_directory": "~/projects/anselmos_blog/",
    "windows": [
        {
            "window_index": 0,
            "window_name": "content",
            "start_directory": "./data/content/articles",
            "panes": [
                {
                   "shell_command": [
                       "vim ."
                    ],
                    "focus": true
                }
            ],
            "focus": true
        },
        {
            "window_index": 1,
            "window_name": "dev",
            "start_directory": "./data",
            "shell_command_before": [
                "cd pelican-plugins/pelican_link_to_title/",
                "sh runredis.sh",
                "cd -"
            ],
            "panes": [
                "pipenv run make devserver"
            ]
        },
        {
            "window_index": 2,
            "window_name": "publish",
            "shell_command_before": [
                "cd ~/projects/anselmos_blog/"
            ],
            "panes": [
                "echo 'make publish_ftp'"
            ]
        },
        {
            "window_index": 3,
            "window_name": "social",
            "shell_command_before": [
                "cd ~/projects/day_to_day_automation/blog/"
            ],
            "panes": [
                "echo 'pipenv run python get_post_summary.py ~/projects/anselmos_blog/data/content/articles/'" 
            ]
        }
    ], 
    "session_name": "blog"
}

This creates 4 panes: - one with vim opened at articles directory - second with pelican starting development-ready server that automatically detects changes in .md files. - third with echoing make publish_ftp for my convenience to have it already there. - last one that is for re-using the get_post_summary.py as seen in this article

Asciinema

4 pane split example:

2 empty panes (check it in a fullscreen to see bottom pane with tmux name of panes):

3 empty panes with focus on the middle one (check it in a fullscreen to see bottom pane with tmux name of panes):

Acknowledgements

Auto-promotion

Related links

Thanks!

That's it :) Comment, share or don't - up to you.

Any suggestions what I should blog about? Post me a comment in the box below or poke me at Twitter: @anselmos88.

See you in the next episode! Cheers!



Comments

comments powered by Disqus