DEV Community

Yusuke Minami
Yusuke Minami

Posted on • Updated on

Tired of copying text in terminal?

Paint point

As an engineer, it has been my pain point to copy text in terminal.
The situation occurs frequently while navigating files in remote data storage such as S3 and HDFS which does not support cd .

Solution

A solution I found is quick select feature of Wezterm, which assigns keyboard shortcut (1-2 characters) to copy quickly in the terminal.

In default, however, the selection does not work as expected.

Here is my config.

  • Ctrl + Shift + y >> copy a text separated by whitespace characters
  • Ctrl + Shift + l >> copy a line (useful to copy and paste error message into Google search or LLM)
  • Ctrl + Shift + p >> copy a text separated by whitespace/slash/quotation/bracket characters (useful to copy only file names, values in JSON, etc.)

Image description

How to set up

  1. Install Wezterm following https://wezfurlong.org/wezterm/installation.html
  2. Create ~/.wezterm.lua file including the following content:
-- Pull in the wezterm API
local wezterm = require 'wezterm'

-- This table will hold the configuration.
local config = {}

-- In newer versions of wezterm, use the config_builder which will
-- help provide clearer error messages
if wezterm.config_builder then
    config = wezterm.config_builder()
end

-- This is where you actually apply your config choices


config.quick_select_patterns = {
    -- match things that look like sha1 hashes
    -- (this is actually one of the default patterns)
    '\\S{6,}',
}

config.keys = {
    {
        key = 'Y',
        mods = 'CTRL',
        action = wezterm.action.QuickSelectArgs {
            patterns = {
                '\\S{6,}',
            },
            scope_lines = 0,
            label = 'copy a text separated by whitespace characters',
        },
    },
    {
        key = 'L',
        mods = 'CTRL',
        action = wezterm.action.QuickSelectArgs {
            patterns = {
                '.{6,}',
            },
            scope_lines = 0,
            label = 'copy a line',
        },
    },
    {
        key = 'P',
        mods = 'CTRL',
        action = wezterm.action.QuickSelectArgs {
            patterns = {
                '[^\\s\\/\'\"\\<\\>\\{\\}\\(\\)\\[\\]]{6,}',
            },
            scope_lines = 0,
            label = 'copy a text separated by whitespace/slash/quotation/bracket characters',
        },
    },
}

config.disable_default_quick_select_patterns = true

config.colors = {
    quick_select_label_fg = { Color = '#ff0000' },
    quick_select_match_fg = { Color = '#aaaaaa' },
    scrollbar_thumb = '#aaaaaa',
}

config.enable_scroll_bar = true

-- and finally, return the configuration to wezterm
return config
Enter fullscreen mode Exit fullscreen mode

Reference

https://github.com/wez/wezterm/issues/4219#issuecomment-1697977950

Top comments (0)