如何为/bin/bash配置Ctrl+C:选中文本复制,未选中则发送SIGINT
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:
Option 1: Use Your Terminal Emulator's Built-in Features (Recommended)
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
- Right-click anywhere in the terminal window and select Preferences
- Switch to the Profiles tab, pick your active profile, then click Edit
- Go to the Text tab and check the box labeled "Copy text to clipboard when selected"
- Next, head to the Shortcuts tab:
- Find the "Copy" action, remove the default
Ctrl+Shift+Cshortcut, and set it toCtrl+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.
- Find the "Copy" action, remove the default
Konsole (KDE Terminal)
- Open Konsole's settings via Settings → Configure Konsole
- Go to the Profiles tab, select your preferred profile, and click Edit
- Navigate to the Mouse tab and check "Copy selected text to clipboard" under the Selection section
- 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)
- Open iTerm2's preferences via iTerm2 → Settings
- Go to the Profiles tab, select your profile, then switch to the Keys tab
- 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.
- Set the keyboard shortcut to
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.
First, install
xclip(for clipboard access on Linux):# Debian/Ubuntu sudo apt install xclip # Fedora/RHEL sudo dnf install xclipCreate a script named
smart_ctrl_c.shin 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 $$)" fiMake the script executable:
chmod +x ~/smart_ctrl_c.shBind 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 assignCtrl+Cas 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

