You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为/bin/bash配置Ctrl+C:选中文本复制,未选中则发送SIGINT

Configure Ctrl+C for /bin/bash: Smart SIGINT or Clipboard Copy Based on Text Selection

Got it, let's tackle this exact scenario you're describing—having Ctrl+C act as both a standard SIGINT trigger (to kill running processes and bring up a new prompt) and a clipboard copy shortcut, depending on whether text is selected in your bash terminal. As you noticed with PHPStorm's terminal, this logic is totally achievable—here's how to set it up for your regular /bin/bash sessions:

Most modern terminal emulators have native support for this smart behavior, often via a "Copy on Selection" setting that works alongside the default Ctrl+C SIGINT action. Here's how to enable it for popular terminals:

GNOME Terminal

  1. Right-click anywhere in the terminal window and select Preferences
  2. Switch to the Profiles tab, pick your active profile, then click Edit
  3. Go to the Text tab and check the box labeled "Copy text to clipboard when selected"
  4. Next, head to the Shortcuts tab:
    • Find the "Copy" action, remove the default Ctrl+Shift+C shortcut, and set it to Ctrl+C
    • GNOME Terminal will automatically prioritize the copy action when text is selected; if no text is highlighted, Ctrl+C will fall back to sending the standard SIGINT signal.

Konsole (KDE Terminal)

  1. Open Konsole's settings via Settings → Configure Konsole
  2. Go to the Profiles tab, select your preferred profile, and click Edit
  3. Navigate to the Mouse tab and check "Copy selected text to clipboard" under the Selection section
  4. Switch to the Shortcuts tab, find the "Copy" action, and set its shortcut to Ctrl+C
    • Konsole handles the logic out of the box: if text is selected, Ctrl+C copies it to clipboard; if not, it sends SIGINT as usual.

iTerm2 (macOS)

  1. Open iTerm2's preferences via iTerm2 → Settings
  2. Go to the Profiles tab, select your profile, then switch to the Keys tab
  3. Click the + button to add a new shortcut:
    • Set the keyboard shortcut to Ctrl+C
    • For the action, select "Select Menu Item" and choose Edit → Copy
    • Check the box labeled "Ignore unless text is selected"
    • This ensures that when no text is selected, the default SIGINT behavior remains intact.

Option 2: Custom Script for Full Control (For Older Terminals)

If your terminal doesn't have native support, you can create a small script that checks for selected text and acts accordingly, then bind it to Ctrl+C.

  1. First, install xclip (for clipboard access on Linux):

    # Debian/Ubuntu
    sudo apt install xclip
    
    # Fedora/RHEL
    sudo dnf install xclip
    
  2. Create a script named smart_ctrl_c.sh in your home directory with this content:

    #!/bin/bash
    # Check if there's text selected in the terminal's primary clipboard
    SELECTED_TEXT=$(xclip -o -selection primary 2>/dev/null)
    
    if [ -n "$SELECTED_TEXT" ]; then
        # Copy selected text to the system clipboard
        echo "$SELECTED_TEXT" | xclip -selection clipboard
    else
        # No selection—send SIGINT to the current foreground process
        kill -INT "$(ps -o ppid= -p $$)"
    fi
    
  3. Make the script executable:

    chmod +x ~/smart_ctrl_c.sh
    
  4. Bind Ctrl+C to run this script in your terminal's shortcut settings:

    • Open your terminal's preferences → Shortcuts section
    • Remove any existing Ctrl+C shortcut for "Copy"
    • Add a new custom shortcut, set the command to ~/smart_ctrl_c.sh, and assign Ctrl+C as the key combination

This script will detect if text is selected, copy it to clipboard if so, and send the standard SIGINT signal otherwise.

内容的提问来源于stack exchange,提问作者Michael Käfer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:17:47