Saltar al contenido principal

Configuración y Personalización de Tmux

Tmux es altamente configurable. En este capítulo aprenderás a personalizar cada aspecto de Tmux para crear un entorno que se adapte perfectamente a tu workflow.

📁 Archivos de Configuración

Archivo principal: ~/.tmux.conf

# ~/.tmux.conf - Configuración principal de Tmux
# Las configuraciones se cargan al iniciar Tmux
# Para recargar: tmux source-file ~/.tmux.conf

Estructura recomendada

# ============================================================================
# CONFIGURACIÓN GENERAL
# ============================================================================

# ============================================================================
# TECLAS Y ATAJOS
# ============================================================================

# ============================================================================
# CONFIGURACIÓN DE VENTANAS Y PANELES
# ============================================================================

# ============================================================================
# BARRA DE ESTADO Y APARIENCIA
# ============================================================================

# ============================================================================
# PLUGINS
# ============================================================================

⌨️ Configuración de Teclas

Cambiar la prefix key

# Cambiar de Ctrl-b a Ctrl-a (más cómodo)
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# O usar Alt (sin Ctrl)
# set-option -g prefix M-a

Atajos para división de paneles

# División más intuitiva
bind | split-window -h # Vertical con |
bind - split-window -v # Horizontal con -
bind _ split-window -v # Alternativo horizontal

# Desactivar atajos por defecto
unbind '"'
unbind %

# División en directorio actual
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Navegación entre paneles
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Navegación entre ventanas
bind -n M-h previous-window
bind -n M-l next-window

# Redimensionado de paneles (repetible con -r)
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

Atajos adicionales útiles

# Recarga de configuración
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"

# Acceso rápido a ventanas
bind -n M-1 select-window -t 1
bind -n M-2 select-window -t 2
bind -n M-3 select-window -t 3
bind -n M-4 select-window -t 4
bind -n M-5 select-window -t 5

# Intercambio de paneles
bind > swap-pane -D
bind < swap-pane -U

# Sincronización de paneles (útil para servidores múltiples)
bind S set-window-option synchronize-panes

🎨 Configuración Visual

Configuración básica de apariencia

# Colores de 256 bits
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",xterm-256color*:Tc"

# Numeración desde 1
set -g base-index 1
setw -g pane-base-index 1

# Renumeración automática de ventanas
set -g renumber-windows on

# Mantener nombres de ventana
set-option -g allow-rename off

# Mostrar actividad en otras ventanas
setw -g monitor-activity on
set -g visual-activity on

Configuración de colores

# Colores de la barra de estado
set -g status-bg colour235
set -g status-fg colour137

# Colores de ventana activa/inactiva
setw -g window-status-current-format '#[fg=colour81,bg=colour238,bold] #I#[fg=colour250]:#[fg=colour255]#W#[fg=colour50]#F '
setw -g window-status-format '#[fg=colour138,bg=colour235] #I#[fg=colour237]:#[fg=colour250]#W#[fg=colour244]#F '

# Colores de paneles
set -g pane-border-style fg=colour238
set -g pane-active-border-style fg=colour51

# Colores del modo comando
set -g message-style fg=colour232,bg=colour166,bold

Barra de estado personalizada

# Configuración general de la barra
set -g status-interval 60
set -g status-left-length 30
set -g status-right-length 150

# Información del lado izquierdo
set -g status-left '#[fg=colour76,bg=colour241,bold] #S #[fg=colour241,bg=colour235,nobold]'

# Información del lado derecho
set -g status-right '#[fg=colour233,bg=colour241,bold] %d/%m #[fg=colour233,bg=colour245,bold] %H:%M:%S '

# Centrar la lista de ventanas
set -g status-justify centre

Barra de estado avanzada con información del sistema

# Función para obtener información del sistema (requiere scripts externos)
set -g status-right '#[fg=colour233,bg=colour241,bold] #(whoami) #[fg=colour233,bg=colour245,bold] #(hostname -s) #[fg=colour233,bg=colour249,bold] #(uptime | cut -f 4-5 -d " " | cut -f 1 -d ",") #[fg=colour233,bg=colour252,bold] %H:%M:%S '

# Información de CPU y memoria (requiere scripts)
set -g status-right '#[fg=colour137,bg=colour234] CPU: #(top -bn1 | grep "Cpu(s)" | awk "{print $2}" | cut -d"%" -f1)% #[fg=colour174,bg=colour234] MEM: #(free | grep Mem | awk "{printf \"%.1f%%\", $3/$2 * 100.0}") #[fg=colour109,bg=colour234] %H:%M '

🔧 Configuración de Comportamiento

Configuración del mouse

# Habilitar soporte para mouse
set -g mouse on

# Comportamiento del scroll
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M

Configuración de copia y pegado

# Usar modo vi para copia
setw -g mode-keys vi

# Configuración de selección y copia
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel

# Integración con portapapeles del sistema (macOS)
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'pbcopy'
bind-key -T copy-mode-vi Enter send -X copy-pipe-and-cancel 'pbcopy'

# Integración con portapapeles del sistema (Linux)
# bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

# Pegar desde portapapeles
bind-key p run "pbpaste | tmux load-buffer - && tmux paste-buffer"

Configuración de historial

# Aumentar historial de líneas
set -g history-limit 10000

# Configuración de escape time (importante para Vim)
set -sg escape-time 0

# Tiempo de repetición para teclas
set -g repeat-time 600

🔌 Plugins de Tmux

Tmux Plugin Manager (TPM)

# Instalar TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# En ~/.tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# Inicializar TPM (debe estar al final del archivo)
run '~/.tmux/plugins/tpm/tpm'

Plugins recomendados

# Plugins esenciales
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# Navegación mejorada
set -g @plugin 'christoomey/vim-tmux-navigator'

# Temas
set -g @plugin 'jimeh/tmux-themepack'
set -g @themepack 'powerline/default/cyan'

# Utilidades
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'tmux-plugins/tmux-open'

# Inicializar plugins
run '~/.tmux/plugins/tpm/tpm'

Configuración de plugins específicos

tmux-resurrect (guardar/restaurar sesiones)

set -g @plugin 'tmux-plugins/tmux-resurrect'

# Restaurar programas específicos
set -g @resurrect-processes 'vi vim nvim emacs man less more tail top htop irssi weechat mutt'

# Guardar y restaurar paneles de Vim
set -g @resurrect-strategy-vim 'session'

# Directorio para guardar
set -g @resurrect-dir '~/.tmux/resurrect'

tmux-continuum (auto-guardar sesiones)

set -g @plugin 'tmux-plugins/tmux-continuum'

# Auto-guardar cada 15 minutos
set -g @continuum-save-interval '15'

# Auto-restaurar al iniciar tmux
set -g @continuum-restore 'on'

# Auto-iniciar tmux al arrancar
set -g @continuum-boot 'on'

vim-tmux-navigator (navegación Vim+Tmux)

set -g @plugin 'christoomey/vim-tmux-navigator'

# En tu .vimrc también necesitas:
# Plug 'christoomey/vim-tmux-navigator'

📝 Configuración Completa de Ejemplo

~/.tmux.conf básico pero completo

# ============================================================================
# CONFIGURACIÓN GENERAL
# ============================================================================

# Prefix key
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# Terminal y colores
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",xterm-256color*:Tc"

# Numeración y renombrado
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
set-option -g allow-rename off

# Mouse y historia
set -g mouse on
set -g history-limit 10000
set -sg escape-time 0

# ============================================================================
# ATAJOS DE TECLADO
# ============================================================================

# División de paneles
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# Navegación estilo Vim
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Redimensionado
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Recarga de configuración
bind r source-file ~/.tmux.conf \; display-message "Config reloaded!"

# ============================================================================
# APARIENCIA
# ============================================================================

# Colores de la barra de estado
set -g status-bg colour235
set -g status-fg colour137
set -g status-interval 60

# Información de la barra
set -g status-left '#[fg=colour76,bg=colour241,bold] #S '
set -g status-right '#[fg=colour233,bg=colour241,bold] %d/%m %H:%M '
set -g status-justify centre

# Colores de ventanas
setw -g window-status-current-format '#[fg=colour81,bg=colour238,bold] #I:#W '
setw -g window-status-format '#[fg=colour138,bg=colour235] #I:#W '

# Colores de paneles
set -g pane-border-style fg=colour238
set -g pane-active-border-style fg=colour51

# ============================================================================
# MODO COPIA
# ============================================================================

setw -g mode-keys vi
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel 'pbcopy'

# ============================================================================
# PLUGINS
# ============================================================================

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'christoomey/vim-tmux-navigator'

# Configuración de plugins
set -g @resurrect-strategy-vim 'session'

# Inicializar TPM (debe estar al final)
run '~/.tmux/plugins/tpm/tpm'

🚀 Scripts y Automatización

Script de configuración automática

#!/bin/bash
# setup-tmux.sh

echo "Configurando Tmux..."

# Backup de configuración existente
if [ -f ~/.tmux.conf ]; then
cp ~/.tmux.conf ~/.tmux.conf.backup
echo "Backup creado: ~/.tmux.conf.backup"
fi

# Crear directorios necesarios
mkdir -p ~/.tmux/plugins
mkdir -p ~/.tmux/resurrect

# Instalar TPM
if [ ! -d ~/.tmux/plugins/tpm ]; then
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
echo "TPM instalado"
fi

# Copiar configuración
cp tmux.conf ~/.tmux.conf
echo "Configuración copiada"

# Instalar plugins
~/.tmux/plugins/tpm/bin/install_plugins
echo "Plugins instalados"

echo "¡Tmux configurado! Reinicia tmux para aplicar cambios"

Función para temas dinámicos

# En ~/.tmux.conf
bind T run-shell "~/.tmux/scripts/toggle-theme.sh"

# ~/.tmux/scripts/toggle-theme.sh
#!/bin/bash
current_theme=$(tmux show-options -gqv @current_theme)

if [ "$current_theme" = "dark" ]; then
# Cambiar a tema claro
tmux set-option -g status-bg colour255
tmux set-option -g status-fg colour232
tmux set-option -g @current_theme "light"
else
# Cambiar a tema oscuro
tmux set-option -g status-bg colour235
tmux set-option -g status-fg colour137
tmux set-option -g @current_theme "dark"
fi

tmux refresh-client

📝 Ejercicios Prácticos

Ejercicio 1: Configuración básica

  1. Cambia la prefix key a tu preferencia
  2. Configura atajos para división intuitiva de paneles
  3. Personaliza los colores de la barra de estado
  4. Agrega información útil a la barra (fecha, hora, nombre de usuario)

Ejercicio 2: Configuración avanzada

  1. Instala TPM y al menos 3 plugins
  2. Configura tmux-resurrect para guardar sesiones
  3. Personaliza completamente la apariencia
  4. Crea atajos personalizados para tu workflow

Ejercicio 3: Scripts de automatización

  1. Crea un script que configure automáticamente tmux
  2. Implementa un sistema de temas intercambiables
  3. Crea funciones para layouts específicos
  4. Automatiza la creación de sesiones para proyectos

🏆 Tips Pro

1. Configuración condicional

# Configuración específica por OS
if-shell 'test "$(uname)" = "Darwin"' \
'set -g @plugin "tmux-plugins/tmux-battery"'

# Configuración por versión de tmux
if-shell -b '[ "$(echo "$TMUX_VERSION >= 2.4" | bc)" = 1 ]' \
'set -g default-terminal "screen-256color"'

2. Variables de entorno

# Pasar variables al shell
set-environment -g TMUX_VERSION '#{version}'

3. Debugging de configuración

# Ver todas las opciones
tmux show-options -g
tmux show-options -w

# Ver atajos de teclado
tmux list-keys

4. Performance

# Optimizaciones para performance
set -g status-interval 30 # Actualizar barra menos frecuentemente
set -g monitor-activity off # Deshabilitar en sesiones grandes

¡Una configuración bien pensada de Tmux puede transformar completamente tu productividad en terminal!