How to Enable Block Text Selection in Linux Terminals (Like Windows CMD Rectangular Selection)


11 views

Many developers switching from Windows to Linux miss the rectangular text selection feature available in cmd.exe. While most Linux terminal emulators only support line-based selection, there are solutions to achieve similar functionality.

Some terminal emulators natively support rectangular text selection:

  • Konsole (KDE's terminal): Press Ctrl+Alt while selecting
  • Terminator: Enable through Preferences > Profiles > General > Selection
  • Alacritty: Supports block selection with proper configuration

For terminals without native support, consider these workarounds:

Using Screen or Tmux

Both screen and tmux offer copy mode with rectangular selection:

# In tmux:
Ctrl+b [  # Enter copy mode
Ctrl+Space  # Start selection
Alt+w  # Rectangle toggle
Enter  # Copy selection

Script-Based Solution

This Python script simulates block selection by processing terminal output:

#!/usr/bin/env python3
import sys
from collections import defaultdict

def block_select(text, start_row, start_col, end_row, end_col):
    lines = text.split('\n')
    result = []
    for i in range(start_row, end_row + 1):
        if i < len(lines):
            line = lines[i]
            selected = line[start_col:end_col + 1]
            result.append(selected)
    return '\n'.join(result)

# Example usage
if __name__ == "__main__":
    sample_text = """Line 1: abcdefg
Line 2: hijklmn
Line 3: opqrstu"""
    print(block_select(sample_text, 0, 5, 2, 7))

For better selection experience:

  • Enable mouse support in your shell (add set mouse=a to ~/.vimrc for Vim users)
  • Consider using middle-click paste instead of Ctrl+Shift+V
  • Adjust scrollback buffer size to maintain selection context

Even without block selection, these can improve efficiency:

  • Ctrl+Shift+C/V: Copy/paste in most terminals
  • Shift+Insert: Alternative paste method
  • Ctrl+u/k: Cut to beginning/end of line

Many developers switching from Windows to Linux miss the rectangular (block) text selection feature available in cmd.exe. While most Linux terminal emulators only support line-based selection, there are workarounds to achieve similar functionality.

Some terminal emulators do support rectangular selection:

  • Konsole: KDE's terminal supports block selection with Ctrl+Alt+MouseDrag
  • Terminator: Allows rectangular selection via right-click context menu
  • MATE Terminal: Includes block selection in its edit menu

For terminals without native support, consider these solutions:

Using tmux

The terminal multiplexer tmux offers copy mode with rectangular selection:


# Start tmux
tmux

# Enter copy mode
Ctrl+b [

# Start block selection
Alt+Space

# Move cursor to define rectangle
Arrow keys

# Copy selection
Enter

# Paste
Ctrl+b ]

Vim/Neovim Terminal Mode

When using terminal mode in Vim, you can leverage Vim's visual block mode:


# In Vim's terminal mode
Ctrl+w N

# Enter visual block mode
Ctrl+v

# Select rectangle
hjkl movement

# Yank selection
y

# Paste in insert mode
p

For advanced users, here's a Python script that can extract rectangular text from terminal output:


#!/usr/bin/env python3
import sys
import re

def extract_block(text, start_line, start_col, end_line, end_col):
    lines = text.split('\n')
    result = []
    for i in range(start_line-1, end_line):
        if i < len(lines):
            line = lines[i]
            segment = line[start_col-1:end_col]
            result.append(segment)
    return '\n'.join(result)

if __name__ == "__main__":
    if len(sys.argv) != 5:
        print("Usage: block_select.py start_line start_col end_line end_col")
        sys.exit(1)
    
    coords = list(map(int, sys.argv[1:5]))
    text = sys.stdin.read()
    print(extract_block(text, *coords))

Usage: cat file.txt | python3 block_select.py 3 5 10 20

For keyboard-centric workflows, consider these CLI tools:

  • grep -o for pattern-based extraction
  • cut -c for column-based selection
  • awk with field manipulation

While Linux terminals traditionally favor line-based selection, modern terminal emulators and tools provide multiple ways to achieve rectangular text selection. The best approach depends on your specific workflow and terminal preferences.