Saturday, June 23, 2018

Shell Configuration



Bash settings
https://www.thegeekstuff.com/2008/09/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie/
  • \!: The history number of the command
  • \h: hostname
  • $kernel_version: The output of the uname -r command from $kernel_version variable
  • \$?: Status of the last command

$ export PS1="\e[0;34m\u@\h \w> \e[m"
[Note: This is for light blue prompt]

$ export PS1="\e[1;34m\u@\h \w> \e[m"
[Note: This is for dark blue prompt]
  • \e[ – Indicates the beginning of color prompt
  • x;ym – Indicates color code. Use the color code values mentioned below.
  • \e[m – indicates the end of color prompt

PS1='\$ '
  • \u – Username
  • \h – Hostname
  • \w – Full path of the current working directory
-bash-3.2$ export PS1="\u@\h \w> "

export PS1="$(whoami)@$(hostname):$(pwd)"

http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/
bind -P

set -o vi
in your bash shell (to switch back to emacs editing mode, type set -o emacs).

| 0            | Move to the beginning of line                              |
'--------------+------------------------------------------------------------'
| ^            | Move to the first non-blank character of line.             |
'--------------+------------------------------------------------------------'
| $            | Move to the end of line.                                   |
'--------------+------------------------------------------------------------'
| %            | Move to the corresponding opening/closing bracket. 

CTRL-A Move cursor to the beginning of the line
CTRL-E Move to the end of the line
CTRL-K Kill forward to the end of a line
CTRL-U Kill the line from the beginning to this point

These are basic commands that use the CTRL and ESC keyboard buttons. These commands recall your history.

SHORTCUT WHAT IT DOES
CTRL-R Search Backwards
CTRL-P Move to the previous line
CTRL-N Move to the next line
ESC-< Move to the first line in the history file
ESC-> Move to the last line in the history file


^abc­^­def   Run previous command, replacing abc with def
 Ctrl + L   Clear the Screen, similar to the clear command

  Ctrl + r   Recall the last command including the specified character(s)
             searches the command history as you type.
             Equivalent to : vim ~/.bash_history. 
  Ctrl + p   Previous command in history (i.e. walk back through the command history)
  Ctrl + n   Next command in history (i.e. walk forward through the command history)

  Ctrl + s   Go back to the next most recent command.
             (beware to not execute it from a terminal because this will also launch its XOFF).
  Ctrl + o   Execute the command found via Ctrl+r or Ctrl+s
  Ctrl + g   Escape from history searching mode
        !!   Repeat last command
      !n     Repeat from the last command: args n e.g. !:2 for the second argumant.
      !n:m   Repeat from the last command: args from n to m. e.g. !:2-3 for the second and third.
      !n:$   Repeat from the last command: args n to the last argument.
      !n:p   Print last command starting with n
        !$   Last argument of previous command
   ALT + .   Last argument of previous command
        !*   All arguments of previous command
https://askubuntu.com/questions/162247/why-does-ctrl-left-arrow-not-skip-words
2 - Or in ~/.bahrc
bind '"\e[1;5D" backward-word' 
bind '"\e[1;5C" forward-word
... And now ctrl + left / right jump words in byobu / tmux.
3 - Don't mess with inputrc.
https://unix.stackexchange.com/questions/424471/whats-the-difference-between-bashrc-and-inputrc/424475
Settings that are in the .inputrc file affect all programs that use the GNU readline library, not just bash. Think of the .inputrc file as being a configuration file for GNU Readline similar to how .bashrc is a configuration file for bash.
GNU Readline is described as:
... A set of functions for use by applications that allow users to edit command lines as they are typed in.
To clarify a bit further, settings that are in the .bashrc file only affect bash. If you'd like to optionally run bash without GNU Readline support or usage, you can invoke bash with the --noediting option as follows: bash --noediting. You can find out more about bash options here.
To answer your last question of
How do I distinguish when to put what where?
If you'd like an option to affect all programs on your system that use the GNU Readline library, .inputrc would be the prime choice. Elsewise, if you simply want to configure your bash session, .bashrc would be the best choice.

Then use ^X ^R to reload the configuration.
https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File.html
Although the Readline library comes with a set of Emacs-like keybindings installed by default, it is possible to use a different set of keybindings. Any user can customize programs that use Readline by putting commands in an inputrc file, conventionally in his home directory. The name of this file is taken from the value of the shell variable INPUTRC. If that variable is unset, the default is ~/.inputrc. If that file does not exist or cannot be read, the ultimate default is /etc/inputrc.
When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.
In addition, the C-x C-r command re-reads this init file, thus incorporating any changes that you might have made to it.
set completion-ignore-case on

https://www.topbug.net/blog/2017/07/31/inputrc-for-humans/
$include /etc/inputrc
"\C-p":history-search-backward
"\C-n":history-search-forward

set colored-stats On
set completion-ignore-case On
set completion-prefix-display-length 3
set mark-symlinked-directories On
set show-all-if-ambiguous On
set show-all-if-unmodified On
set visible-stats On
Here is the explanation.
$include /etc/inputrc: This line carries over site-wide readline configuration to the user configuration. Usually /etc/inputrc includes some goodies.
"\C-p":history-search-backward and "\C-n":history-search-forward: These two lines set CtrlP/CtrlN to search backward/forward through the history for the string of characters between the start of the current line and the current cursor position.
set colored-stats On: This line sets readline to display possible completions using different colors to indicate their file types. The colors are determined by the environmental variable LS_COLORS, which can be nicely configured.
set completion-ignore-case On: This line sets auto completion to ignore cases.
set completion-prefix-display-length 3: This line sets 3 to be the maximum number of characters to be the common prefix to display for completions. If the common prefix has more than 3 characters, they are replaced by ellipsis. For example, assume we are in a directory with files Screenshot_20170730.png and Screenshot_20170731.png. If using the default option, an auto completion prints Screenshot_20170730.png Screenshot_20170731.png. If this option is set, then the auto completion prints ...0.png ...1.png. However, if the common prefix is very short (fewer or equal to 3 characters), the full file names are still printed. For example, in a directory with files S10.png and S11.png, an auto completion still prints S10.png S11.png. This can be very useful when auto completing in a directory in which a lot of files have a long common prefix, such a camera image directory.
set mark-symlinked-directories On: This line sets every completion which is a symbolic link to a directory to have a slash appended.
set show-all-if-ambiguous On: This line sets the completions to be listed immediately instead of ringing the bell, when the completing word has more than one possible completion.
set show-all-if-unmodified On: This line sets the completions to be listed immediately instead of ringing the bell, when the completing word has more than one possible completion but no partial completion can be made.
set visible-stats On: This lines sets completions to be appended by characters that indicate their file types reported by the stat system call.
As an example of text navigation bindings, you’ll commonly see these:
"\e[1;5C": forward-word
"\e[1;5D": backward-word
COPY
The first part (before the colon) is the keybinding, the second half is the command. In this case, they’re Readline commands for moving the cursor by word. \e represents the Option key (Alt on a PC), and the escape sequences after them are for the left and right arrow keys. This pair allows you to move the cursor left and right by word boundaries using Option-Left and Option-Right.

Jump to the target folder of the last command and run ls with Option-x

"\ex": 'cd !$ \015ls\015'
COPY
This is great right after you run an extract (tar, unzip, etc.) to a folder or git clone repository destination command. Pressing it right away will change to the folder where you extracted or cloned to and give you a directory listing.
The \015 represents the enter key, but you can also use \C-m for the same result.

Undo a directory change with Option-z

"\ez": 'cd -\015'
COPY
This one is simple: when Option-z is pressed, run cd - to return to the previous directory. Again, the \015 presses Enter after inserting the command text so that it runs immediately.
http://vim.wikia.com/wiki/Use_vi_shortcuts_in_terminal

Many Unix shells (bash, zsh, etc.) come with keyboard shortcuts enabled: C-a jumps to the start of the line, C-e jumps to the end, and so on. However, these are taken from Emacs. It's possible to tell your shell to use Vi shortcuts instead. Not only will the keybindings match what you're used to, but you'll also have modal editing, just like Vi.

To enable Vi-mode, edit (or create) the file ~/.inputrc or /etc/inputrc and add the following lines:

set editing-mode vi
set keymap vi-command

# Use Vi, not Emacs, style editing
set editing-mode vi

# Show all completions as soon as I press tab, even if there's more than one
set show-all-if-ambiguous on
# Ignore case
set completion-ignore-case on
# on menu-complete, first display the common prefix, then cycle through the 
# options when hitting TAB
menu-complete-display-prefix on

###########################################################
# Keymaps for when we're in command mode (e.g., after hitting ESC)
set keymap vi-command

# Insert the arguments from the last command
"p": "i !!*\r"

# When hitting option-up/option-down, cycle through the previous commands
# which start with the prefix you've entered, rather than just cycling through
# the last entered commands.
# In OS X's Terminal.app preferences, I have 'Use option as meta key' off, and
# have mapped "option cursor up" to "\033\033[A" and "option cursor down" to 
# "\033\033[B".
# Feel free to bind to whatever you want. Delete the first '\e' in the keymap
# to set plain up/down to do a history search by default.
"\e\e[A": history-search-backward
"\e\e[B": history-search-forward

###########################################################
# Keymaps for when we're in insert (i.e., typing stuff in) mode
set keymap vi-insert

# Pressing tab will list all completions & select the first one. Pressing it 
# again will cycle through available completions.
TAB: menu-complete
# Shift-TAB cycles completions backward
"\e[Z": menu-complete-backward

# Option-up/option-down should also apply to insert mode
"\e\e[A": history-search-backward
"\e\e[B": history-search-forward

# Needed because binding 'p' in command mode above wipes its insert mode
# function, too. This fixes that, and will insert 'p' when you type 'p'.
"p": self-insert


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts