Bring Your Shortcuts to Any SSH Sessions

Developers have their own tools to work. May not be the coolest but best suit their workflow. Let’s write a Terminator (v. 0.97) plugin and source those shortcuts into any environment.

The Terminator Plugin

Paste file below in ~/.config/terminator/plugins/source_helpers.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python
"""source_helpers.py - Terminator Plugin to source shared cli shortcuts"""

import os
import gtk
import terminatorlib.plugin as plugin
from terminatorlib.translation import _
from terminatorlib.util import widget_pixbuf

# Every plugin you want Terminator to load *must* be listed in 'AVAILABLE'
AVAILABLE = ['SourceHelper']

class SourceHelper(plugin.MenuItem):
    """Add custom commands to the terminal menu"""
    capabilities = ['terminal_menu']
    dialog_action = gtk.FILE_CHOOSER_ACTION_SAVE
    dialog_buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                      gtk.STOCK_SAVE, gtk.RESPONSE_OK)

    def __init__(self):
        plugin.MenuItem.__init__(self)

    def callback(self, menuitems, menu, terminal):
        """Add our menu items to the menu"""
        item = gtk.MenuItem(_('Terminal SourceHelper'))
        item.connect("activate", self.terminal_source_helpers, terminal)
        menuitems.append(item)

    def terminal_source_helpers(self, _widget, terminal):
        """Handle the source_helpers of terminal"""
        f = open(os.getenv("HOME") + '/loki/env/bash_aliases_shared', 'r')
        output = ''
        for line in f:
            line = line.strip()
            if len(line) > 0 and line[0] != "#":
                output = output + line + " ; "
        output = base64.b64encode(output.encode('zlib'))
        output = "echo " + output + " | base64 --decode | python -c 'import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))' > /tmp/source; source /tmp/source; rm /tmp/source; clear"
        terminal.vte.feed_child(" " + output + "\n")

Terminator is not a must, a key binding for tmux will do if you get the idea.

Aliases and Functions

Extract aliases and functions that should be shared into a separated file, e.g. ~/.bash_aliases_shared.

1
2
3
4
5
6
7
8
9
# aliases / functions defined here will be usable in both local and remote machine

# do not store cmd started with whitespace into history
HISTCONTROL=ignoreboth

# better output json
alias json='python -mjson.tool'

# you can add your own stuffs here, even auto-completion setup

Add line below in your ~/.bash_aliases, so they can be used in local as well.

1
source ~/.bash_aliases_shared

Bonus

Terminator usually come in handy when reading local settings on remote environment. One use case is to reset the terminal size. You should encountered that ssh session won’t resize when the terminal window is resized. New area cannot be filled. Simply craft another Terminator plugin to solve this problem. It is much like the previous plugin but we resize the session with stty instead.

1
2
3
4
5
6
# ... plugin instantiation and menu registration ...

def terminal_resize(self, _widget, terminal):
    cols = terminal.vte.get_column_count()
    rows = terminal.vte.get_row_count()
    terminal.vte.feed_child("stty cols " + str(cols) + "; stty rows " + str(rows) + "\n")

Comments