Для комфортной работы в консоли требуется не так много – привычные алиасы, переменные окружения, настройки локали, настройки readline (файл ~/.inputrc), screen и т.д. И желательно, чтобы все эти настройки были одинаковые, независимо от того в какой консоли вы работаете прямо сейчас, в Ubuntu, CentOS, macOS или Synology.

Я для себя постарался унифицировать файлы с этими настройками, для того, чтобы можно было скопировать их в домашнюю папку на свежеустановленную систему и продолжить работать в привычном окружении.

Подготовка папок для конфигурационных файлов:

$ mkdir -p ~/.config/bashrc
$ mkdir -p ~/.config/bashrc.d

В папке ~/.config/bashrc будут находиться стандартные файлы из домашней папки (.inputrc, .screenrc, .bash_profile и .bash_logout). В самой домашней папке будут симлинки на эти файлы. А в папке ~/.config/bashrc.d – будут находиться различные конфигурационные файлы, которые будут считываться во время выполнения файла ~/.bash_profile автоматически.

Настройка readline:

$ nano -w ~/.config/bashrc/.inputrc
"\e[1;3C": forward-word
"\e[1;3D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[7~": beginning-of-line
"\e[8~": end-of-line
"\eOH": beginning-of-line
"\eOF": end-of-line
"\e[H": beginning-of-line
"\e[F": end-of-line
"\e[5~": history-search-backward
"\e[6~": history-search-forward
$ ln -s ~/.config/bashrc/.inputrc ~/.inputrc

Настройка скриптов bash:

#----------------------------------------------------------------------------
# ~/.bash_profile: executed by bash(1) for login shells.
# $Revision: 2.30 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
  # Shell is non-interactive.  Be done now!
  return
fi

# Source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# Include ~/.config/bashrc.d/* if it exists
if [ -d ~/.config/bashrc.d ]; then
  for i in `ls ~/.config/bashrc.d/` ; do
    if [[ -f ~/.config/bashrc.d/${i} ]]; then
      . ~/.config/bashrc.d/${i}
    fi
  done
  unset i
fi

$ nano -w ~/.config/bashrc/.bash_logout
#----------------------------------------------------------------------------
# ~/.bash_logout: executed by bash(1) when login shell exits.
# $Revision: 2.20 (CentOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# When leaving the console clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
  [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
  [ -x /usr/bin/clear ] && /usr/bin/clear
fi

$ ln -s ~/.config/bashrc/.bash_profile ~/.bash_profile
$ ln -s ~/.config/bashrc/.bash_logout ~/.bash_logout
$ ln -s ~/.bash_profile ~/.bashrc

$ nano -w ~/.config/bashrc.d/00-aliases
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/00-aliases: executed by .bash_profile for aliases
# $Revision: 2.30 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# Alias definitions.
alias cp='cp -i'
alias df='df -Ph'
alias du='du -h'
alias less='less -r'
alias ll='ls -lp'
alias mc='mc -x -S xoria256'
alias mcdiff='mcdiff -x -S xoria256'
alias mcedit='mcedit -x -S xoria256'
alias mcview='mcview -x -S xoria256'
alias md='mkdir'
alias mtr='mtr -o "LRSD NBAWV"'
alias mv='mv -i'
alias nano='nano -Scmw'
alias pman='pinfo -m'
alias px='ps xa'
alias rd='rmdir'
alias rm='rm -i'
alias scp='scp -p -r'
alias screen='[ -z "$HUSHLOGIN" ] && TERM=xterm-256color screen -D -RR || TERM=linux screen -D -RR'
alias ssh-suppress='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
alias su='su -'
alias sudo='sudo -s '
alias showextip='curl http://ipecho.net/plain; echo'
alias watch='watch '
alias who='who -HuT'
#[[ $(wget --help) == *hsts* ]] && alias wget='wget --no-hsts'

flushdns () {
    if [ "$OSTYPE" == "linux-gnu" ]; then
  if hash systemd-resolve 2>/dev/null; then
          sudo systemd-resolve --flush-caches;
  else
      sudo rndc flush;
  fi
    else
  sudo dscacheutil -flushcache;
  sudo killall -HUP mDNSResponder;
  say "cache flushed";
    fi
}

tail () {
    if hash ccze 2>/dev/null; then
  /usr/bin/tail "$@" | ccze -A -o lookups=no;
    else
  /usr/bin/tail "$@";
    fi
}

telnet () { 
    if hash gtelnet 2>/dev/null; then
        /opt/local/bin/gtelnet "$@";
    else
        /usr/bin/telnet "$@";
    fi
}

wget () {
    if [ "$OSTYPE" == "linux-gnu" ]; then
        WGET=/usr/bin/wget
    else
        WGET=/opt/local/bin/wget
    fi
    if [ "$($WGET --help)" == "*hsts*" ]; then
        $WGET --no-hsts "$@";
    else
        $WGET "$@";
    fi
}

$ nano -w ~/.config/bashrc.d/01-locale
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/01-locale: executed by .bash_profile for locale settings
# $Revision: 2.30 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# Locale definitions.
export LC_ALL=""
export LANG="ru_RU.UTF-8"
export LC_NUMERIC="C"
export LC_TIME="POSIX"
export TIME_STYLE="+%Y-%m-%d"

# Set english messages for root.
[ ${EUID} == 0 ] && export LC_MESSAGES="POSIX"

$ nano -w ~/.config/bashrc.d/02-terminal
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/02-terminal: executed by .bash_profile for terminal settings
# $Revision: 2.31 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
    vt220) color_prompt=yes;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  # We have color support; assume it's compliant with Ecma-48
  # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
  # a case would tend to support setf rather than setaf.)
  color_prompt=yes
    else
  color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    if [ ${EUID} == 0 ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
  if [ "${TERM_PROGRAM}" == "Apple_Terminal" ]; then
      PS1='${debian_chroot:+($debian_chroot)}\[\033[01;36m\]\u\[\033[00m\]@\[\033[01;36m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  else
      PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\u\[\033[00m\]@\[\033[01;32m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  fi
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

$ nano -w ~/.config/bashrc.d/03-userfiles
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/03-userfiles: executed by .bash_profile for user files
# $Revision: 2.30 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# User specific environment and startup programs
if [ -d $HOME/.local/bin ]; then
  PATH=$PATH:$HOME/.local/bin
  export PATH
fi
# do the same with MANPATH
if [ -d $HOME/.local/share/man ]; then
  MANPATH=${MANPATH:-:}:$HOME/.local/share/man
  export MANPATH
fi

XDG_CONFIG_HOME=$HOME/.config
XDG_CACHE_HOME=$HOME/.local/share
XDG_DATA_HOME=$HOME/.local/share
export XDG_CONFIG_HOME
export XDG_CACHE_HOME
export XDG_DATA_HOME

$ nano -w ~/.config/bashrc.d/04-bash_completion
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/04-bash_completion: executed by .bash_profile for user files
# $Revision: 1.10 (CentOS Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

$ nano -w ~/.config/bashrc.d/05-apache
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/05-apache: executed by .bash_profile for user files
# $Revision: 1.10 (CentOS Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

[[ ! -d /etc/httpd/vhost.d ]] && return

__a2ensite() {
    if [ "$1" == "" ]; then
        echo "Usage: a2ensite www.example.com"
    elif [ ! -f "/etc/httpd/vhost.d/_vhost-$1" ]; then
        echo "ERROR: Site $1 does not exist!"
    else
        mv -v /etc/httpd/vhost.d/{_,}vhost-$1
        echo "Run 'systemctl reload httpd' to activate new configuration!"
    fi
}
__a2dissite() {
    if [ "$1" == "" ]; then
        echo "Usage: a2dissite www.example.com"
    elif [ ! -f "/etc/httpd/vhost.d/vhost-$1" ]; then
        echo "ERROR: Site $1 does not exist!"
    else
        mv -v /etc/httpd/vhost.d/{,_}vhost-$1
        echo "Run 'systemctl reload httpd' to activate new configuration!"
    fi
}

alias a2ensite=__a2ensite
alias a2dissite=__a2dissite

$ nano -w ~/.config/bashrc.d/06-serial
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/06-serial: executed by .bash_profile for serial console
# $Revision: 1.10 (CentOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

res() {
    old=$(stty -g)
    stty raw -echo min 0 time 5

    printf '\0337\033[r\033[999;999H\033[6n\0338' > /dev/tty
    IFS='[;R' read -r _ rows cols _ < /dev/tty

    stty "$old"

    # echo "cols:$cols"
    # echo "rows:$rows"
    stty cols "$cols" rows "$rows"
}

res2() {
    old=$(stty -g)
    stty raw -echo min 0 time 5

    printf '\033[18t' > /dev/tty
    IFS=';t' read -r _ rows cols _ < /dev/tty

    stty "$old"

    # echo "cols:$cols"
    # echo "rows:$rows"
    stty cols "$cols" rows "$rows"
}

TTY=$(tty)
TMP=${TTY:5}

if [[ "$TMP" == "ttyS"* ]]; then
    eval "$(TERM=linux dircolors -b)"
    unalias mc >/dev/null
    unalias mcdiff >/dev/null
    unalias mcedit >/dev/null
    unalias mcview >/dev/null
    alias mc='TERM=xterm mc --color -S default'
    alias mcdiff='TERM=xterm mcdiff --color -S default'
    alias mcedit='TERM=xterm mcedit --color -S default'
    alias mcview='TERM=xterm mcview --color -S default'

    res
fi

$ nano -w ~/.config/bashrc.d/90-entware-ng
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/90-entware-ng: executed by .bash_profile for entware-ng
# $Revision: 1.20 (Synology Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# PATH environment variable for use with entware-ng
[ -d /opt/sbin ] && export PATH="/opt/sbin:$PATH"
[ -d /opt/bin ] && export PATH="/opt/bin:$PATH"

$ nano -w ~/.config/bashrc.d/90-macports
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/90-macports: executed by .bash_profile for MacPorts settings
# $Revision: 1.20 (macOS Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# PATH environment variable for use with MacPorts
[ -d /opt/local/sbin ] && export PATH="/opt/local/sbin:$PATH"
[ -d /opt/local/bin ] && export PATH="/opt/local/bin:$PATH"
export CLICOLOR=1

# Bash completion for use with MacPorts
if [ -f /opt/local/etc/profile.d/bash_completion.sh ]; then
  . /opt/local/etc/profile.d/bash_completion.sh
fi

$ nano -w ~/.config/bashrc.d/98-synology
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/98-synology: executed by .bash_profile for other settings
# $Revision: 1.00 (Synology Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

[[ ! -d /usr/syno ]] && return

export SHELL=/bin/bash

$ nano -w ~/.config/bashrc.d/99-other
#----------------------------------------------------------------------------
# ~/.config/bashrc.d/99-other: executed by .bash_profile for other settings
# $Revision: 2.20 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

# Don't put duplicate and garbage lines from Midnight Commander to the history.
export HISTCONTROL="ignoreboth"

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# if set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

Настройка screen:

screen в данном примере настроен таким образом, что при его запуске – создаются 10 консолей, и переключаться между ними так же удобно как если бы вы сидели за рельной консолью Linux, по Alt+F1, Alt+F2 и т.д. до Alt+F10.

$ nano -w ~/.config/bashrc/.screenrc
#----------------------------------------------------------------------------
# ~/.screenrc: executed by screen for user settings
# $Revision: 2.20 (CentOS/macOS/Ubuntu Edition by Wakko Warner) $
# $Comment: Any comments please send to wakko@acmelabs.spb.ru $
#----------------------------------------------------------------------------

  hardstatus string "[screen %n%?: %t%?] %h"
# defshell -bash
  shell -$SHELL

# VARIABLES
# ===============================================================
# Automatically detach on hangup. 
  autodetach on       # default: on
# Don't display the copyright page
  startup_message off     # default: on
# Affects the copying of text regions
  crlf off        # default: off
# Change default scrollback value for new windows
  defscrollback 1000      # default: 100
# Define the time that all windows monitored for silence should 
# wait before displaying a message. Default 30 seconds.
  silencewait 15      # default: 30
# UTF-8 support
  defutf8 on
# PuTTY tweaks
  termcapinfo xterm* 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
  termcapinfo xterm* ti@:te@
  term xterm-256color
# term xterm
  attrcolor b ".I"
  defbce on

# KEYBINDINGS
# ==============================================================
# Alt+Fn keys.
  bindkey "^[^[OC" next
  bindkey "^[^[OD" prev
  bindkey "^[^[OP" select 0
  bindkey "^[^[OQ" select 1
  bindkey "^[^[OR" select 2
  bindkey "^[^[OS" select 3
  bindkey "^[^[[15~" select 4
  bindkey "^[^[[17~" select 5
  bindkey "^[^[[18~" select 6
  bindkey "^[^[[19~" select 7
  bindkey "^[^[[20~" select 8
  bindkey "^[^[[21~" select 9

# STARTUP SCREENS
# ===============================================================
# Uncomment one/some following lines to automatically let
# SCREEN start some programs in the given window numbers:
# screen -t IRC         1 irssi
# screen -t EDIT        1 vim
# screen -t GOOGLE      2 links http://www.google.com
# screen -t NEWS        3 slrn
# screen -t WWW         4 links http://rt.com
  screen -t F1    0
  screen -t F2    1
  screen -t F3    2
  screen -t F4    3
  screen -t F5    4
  screen -t F6    5
  screen -t F7    6
  screen -t F8    7
  screen -t F9    8
  screen -t F10   9
  select 0

$ ln -s ~/.config/bashrc/.screenrc ~/.screenrc

Следующая запись Предыдущая запись