Skip to content
Jakub T. Jankiewicz edited this page Sep 12, 2021 · 2 revisions

Terminal have a bunch of events, you can specify them in options, most events have only terminal as with few exceptions like keypress or keydown, which was old API before keymap was added, that have event as first argument and send is terminal. There are also events that have current command as first argument or error object.

List of all events can be found in API reference

Adding CTRL+X shortcut

It will probably never be added to the library. But here is the code that allows adding the cut shortcut.

$('body').terminal({
}, {
    greetings: false,
    checkArity: false,
    keymap: {
        'CTRL+X': function(e) {
            var selection = window.getSelection();
            if (selection.toString() !== '') {
                var start = $(selection.anchorNode);
                var end = $(selection.focusNode);

                var before = start.closest('.cmd [role="presentation"]').prevUntil('.cmd-prompt');
                var count = 0;
                if (before.length > 1) {
                    count = before.find('[data-text]').length;
                }
                var s = start.closest('.cmd [role="presentation"] [data-text]');
                var e = end.closest('.cmd [role="presentation"] [data-text]');

                var cmd = this.get_command();
                start = s.length ? count + s.index() : 0;
                end = e.length ? count + e.index() + 1 : cmd.length;
                var cut = cmd.substring(start, end);
                if (navigator.clipboard) {
                    this.set_command(cmd.substring(0, start) + cmd.substring(end));
                    this.set_position(start);
                    navigator.clipboard.writeText(cut);
                    selection.removeAllRanges();
                }
            }
        }
    }
});
Clone this wiki locally