Configuring GNU Screen to Replicate Native Bash Shell Behavior: Terminal Settings and Workarounds


2 views

When launching GNU Screen after SSHing into an Ubuntu server from macOS Terminal, you'll notice immediate differences in:

  • Prompt coloration and formatting
  • Tab completion functionality
  • Terminal capability support

The root cause typically stems from TERM variable changes:

# Before screen:
echo $TERM
xterm-color

# Inside screen:
echo $TERM
screen

Add this to your ~/.screenrc:

term xterm-256color
defshell -bash
altscreen on

Alternatively for dynamic detection:

shell -$SHELL
term $TERM

Ensure your bashrc gets sourced in screen sessions by adding to ~/.bash_profile:

if [ -n "$STY" ]; then
    source ~/.bashrc
fi

For broken tab completion, add this to your bashrc:

if [[ "$TERM" == screen* ]]; then
    bind '"\C-i": complete'
    complete -o bashdefault -o default -o nospace -F _bash_complete_expand complete
fi

To maintain consistent colors between sessions:

# In ~/.screenrc
attrcolor b ".I"
defbce on
termcapinfo xterm* 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'

Verify with these diagnostic commands:

# Check terminal capabilities
tput colors

# Test completion
type 

# Verify prompt
echo $PS1

Here's a comprehensive ~/.screenrc example:

# Base terminal configuration
term xterm-256color
defshell -bash

# UI customization
caption always "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= %c %D %Y-%m-%d"
hardstatus alwayslastline

# Key bindings
bindkey -k k1 select 1  # F1 = window 1
bindkey -k k2 select 2  # F2 = window 2

# Terminal behavior
altscreen on
defbce on
attrcolor b ".I"

When working with GNU Screen on Linux systems, many developers notice their terminal behavior changes significantly from their familiar bash environment. The key differences typically include:

  • Altered color schemes
  • Missing tab completion
  • Different prompt appearance
  • Modified keyboard shortcuts

The root cause lies in how Screen handles terminal emulation. When you examine your environment variables:

# Outside screen:
echo $TERM  # Typically shows xterm-color or xterm-256color

# Inside screen:
screen
echo $TERM  # Shows screen or screen-256color

Add this to your ~/.screenrc:

# Force screen to use 256 colors
term screen-256color

# Import your existing bash environment
shell -$SHELL

# Preserve your original TERM value
defshell -bash

Ensure your bash profile checks for Screen sessions:

# In ~/.bashrc
if [ -n "$STY" ]; then
    # Screen-specific configurations
    export TERM=screen-256color
    # Load your normal prompt setup
    source ~/.bash_prompt
fi

To restore normal behavior:

# In ~/.inputrc (for readline compatibility)
$if term=screen
    set enable-keypad on
    # Ensure tab completion works
    TAB: complete
$endif

After making these changes:

  1. Exit all Screen sessions
  2. Reload your bash profile: source ~/.bashrc
  3. Start a new Screen session
  4. Verify with: echo $TERM and test tab completion

For complex setups, consider session-specific configurations:

# Create a custom screen startup script
#!/bin/bash
SCREENRC=~/.screenrc-work screen -S work

Then in ~/.screenrc-work:

# Work-specific screen configuration
term xterm-256color
startup_message off
# Additional work-related screen settings