Thursday 22 August 2013

Getting efficient with Readline

GNU Readline is a utility used by different programs to have a consistent command-line interface.
A few of these programs :
  • Bash (zsh does not use readline)
  • some Python interactive interpreters
  • rlwrap
By default, the end user interface of readline comes with really interesting keybindings. They are documented but the page layout is not really user-friendly so here's a summary :


TypeNameEmacs shortcutActionObjectDirectionEquivalent Windows/Gnome/KDE
Mode emacs-editing-mode^ Ctrl+eChangemodefrom vi to emacs (set -o emacs)
vi-editing-mode^ Ctrl+⎇ Alt+jChangemodefrom emacs to vi (set -o vi)
Move backward-char^ Ctrl+bMove the cursorone characterto the left
forward-char^ Ctrl+fMove the cursorone characterto the right
backward-word⎇ Alt+bMove the cursorone wordto the left^ Ctrl+
forward-word⎇ Alt+fMove the cursorone wordto the right^ Ctrl+
beginning-of-line^ Ctrl+aMove the cursorto the start of the line
end-of-line^ Ctrl+eMove the cursorto the end of the line
exchange-point-and-mark^ Ctrl+x ^ Ctrl+xMove the cursorto the start of the line/to original position
Swaptranspose-char^ Ctrl+tSwapthe characterbefore the cursor with current position
transpose-word⎇ Alt+tSwapthe wordbefore the cursor with current word
Searchcharacter-search^ Ctrl+]Searchthe characterand move to next occurence
character-search-backward^ Ctrl+⎇ Alt+]Searchthe wordand move to previous occurence
Casedowncase-word⎇ Alt+lLowercasethe wordfrom cursor
upcase-word⎇ Alt+uUppercasethe wordfrom cursor
capitalize-word⎇ Alt+cCapitalizethe wordfrom cursor
Killdelete-char^ Ctrl+dDeletethe character
unix-word-rubout^ Ctrl+wDeletethe wordleft until after the previous word boundary
backward-kill-word⎇ Alt+⌫ BackspaceDeletethe word
kill-word⎇ Alt+dDeletethe wordright until before the next word boundary
unix-line-discard^ Ctrl+uDeleteeverythingfrom the cursor to the start of the line
kill-line^ Ctrl+kDeleteeverythingfrom the cursor to the end of the line
Yankyank^ Ctrl+yYankthe kill ringat point
yank-pop⎇ Alt+yYankthe kill ringat point (going through older content)
Undoundo^ Ctrl+_ (or ^ Ctrl+x ^ Ctrl+u)Undolast change
revert-line⎇ Alt+rUndoall changesmade to this line
Completioncomplete↹ TabAttempt completion

possible-completions⎇ Alt+?List completions
insert-completions⎇ Alt+*Insert completions
History previous-history^ Ctrl+pMove in historyone lineup
next-history^ Ctrl+nMove in historyone linedown
beginning-of-history⎇ Alt+<Move in historyall linesto the top
end-of-history⎇ Alt+>Move in historyall linesto the bottom
reverse-search-history^ Ctrl+rIncremental searchbackward, going up the history
forward-search-history^ Ctrl+sIncremental searchforward, going down the history

^ Ctrl+jStopincremental search
abort^ Ctrl+gStopincremental searchand restore original line
non-incremental-reverse-search-history⎇ Alt+pNon-incremental search
backward, going up the history
non-incremental-forward-search-history⎇ Alt+nNon-incremental search
forward, going down the history
yank-nth-arg^ Ctrl+⎇ Alt+yYankfirst argumentof the previous command
yank-last-arg⎇ Alt+. or ⎇ Alt+_Yanklast argumentof the previous command
Refresh clear-screen^ Ctrl+lClearthe screen
redraw-current-lineNoneRefreshthe current line

⎇ Alt is also called "meta" prefix and written "M-" or "\M-".
^ Ctrl is also called "control" prefix and written "C-" or "\C-".

Loose convention is that Control operates on characters while Meta operates on words.

Numeric arguments can be passed to readline commands. Type meta digits (⎇ Alt+digit, digit can be - for negative arguments) and then the command. Once the first meta digit is typed, the reminder (if any) can be typed normally. For instance, ⎇ Alt+10^ Ctrl+d will delete the next 10 characters.
Numeric arguments can also be used to repeat normal inputs. For instance, ⎇ Alt+10k inserts "kkkkkkkkkk". If the input is a digit, ^ Ctrl+v can be used to insert it : ⎇ Alt+10^ Ctrl+v2 inserts "2222222222".

These bindings can be changed with a configuration file called inputrc.

Also, in Bash, the bind builtin command can be used to display (bind -P) or modify the readline key bindings. The read builtin command can be used to see the string corresponding to special key inputs. For instance, F5 gives "^[[15~" and then bind '"\e[15~":"top\C-m"' would map F5 to "top\n"

Monday 19 August 2013

Programming tips and tricks for different programming languages

Useful resources to learn new things and/or to avoid re-inventing the wheel.

Shell :
Catonmat - "Awk One-Liners Explained"
Catonmat - "Bash One-Liners Explained"
Catonmat - "Commandlinefu One-Liners Explained"
Catonmat - "Perl One-Liners Explained"
Catonmat - "Sed One-Liners Explained"
Catonmat - "Unix Utilities You Should Know About"
/r/linux/ - "Give me that one command you wish you knew years ago"
StackOverflow - "Hidden features of Bash"
StackOverflow.com - "Hidden features of Windows batch files"

Bit manipulation :
Catonmat - "Low Level Bit Hacks You Absolutely Must Know"

C :
StackOverflow : "What is your favorite C programming trick?"
StackOverflow - "Hidden features of C"

C++ :
StackOverflow - "Hidden features of C++"

Python :
/r/Python/ - "What's the one code snippet/python trick/etc did you wish you knew when you learned python?"
StackOverflow - "Hidden features of Python"

C# :
StackOverflow - "Hidden features of C#"

Delphi :
StackOverflow - "Hidden features of Delphi"

Java :
StackOverflow - "Hidden features of Java"

Javascript :
StackOverflow - "Hidden features of JavaScript"

HTML :
StackOverflow - "Hidden features of HTML"

Ruby :
StackOverflow - "Hidden features of Ruby"

PHP :
StackOverflow - "Hidden features of PHP"

Perl :
StackOverflow - "Hidden features of Perl"

CSS :
StackOverflow - "Hidden features of CSS"

VB.Net :
StackOverflow - "Hidden features of VB.Net"

Misc :
/r/AskReddit/ - "what are some computer tricks everyone should know"
/r/AskReddit/ - "What is one cool internet trick you've learned?"


Introduction

This blog is written by a software developer for software developers and one of them especially : me. Indeed, I'll use it to store/comment/tag technical resources I want to keep handy :
  • snippets of code
  • programming tips
  • Github page for interesting projects
  • cool ideas
  • documentation for various languages and tools
  • adresses of neat online tools
  • etc
This being said, you are more than welcome to keep on reading this. If you do see something wrong or anything to be improved in any way, please let me know via comments.

Let the geekiness begin...
(Updates will be really irregular)