Thursday, September 5, 2013

No syntax highlighting in vimdiff

Vim Syntax highlight often mixed with diff color, which makes text unreadable.

Add following line in ~/.vimrc to turn off syntax off in vimdiff

if &diff | syntax off | endif

Wednesday, August 28, 2013

IO Redirection - Swapping stdout and stderr (Advanced)

% (sh myscript.sh 3>&2 2>&1 1>&3) 2>/dev/null
I'm stderr
% (sh myscript.sh 3>&2 2>&1 1>&3) >/dev/null 
I'm stdout

Thursday, August 8, 2013

python 3 line to solve 8 queen

from itertools import *
c = range(8)
print len([v for v in permutations(c) if 8==len(set(v[i]+i for i in c))==len(set(v[i]-i for i in c))])

Tuesday, June 25, 2013

Python overwrite printed line, e.g. text progress

\r will reset the cursor to the beginning of the line.
>>> for i in range(100):
...    time.sleep(1)
...    sys.stdout.write("\r%d%%" %i)    # or print >> sys.stdout, "\r%d%%" %i,
...    sys.stdout.flush()
... 

Tuesday, May 7, 2013

Thursday, March 7, 2013

brackets, parentheses, curly braces in BASH


In Bash, test and [ are builtins.
The double bracket enables additional functionality. For example, you can use && and || instead of-a and -o and there's a regular expression matching operator =~.
The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like:
  • Truncate the contents of a variable
    $ var="abcde"; echo ${var%d*}
    abc
  • Make substitutions similar to sed
    $ var="abcde"; echo ${var/de/12}
    abc12
  • Use a default value
    $ default="hello"; unset var; echo ${var:-$default}
    hello
  • and several more

Wednesday, March 6, 2013

Vim folding, auto fold xml, fold, unfold


To allow folds based on syntax add something like the following to your .vimrc:
set foldmethod=syntax
set foldlevelstart=1

let javaScript_fold=1         " JavaScript
let perl_fold=1               " Perl
let php_folding=1             " PHP
let r_syntax_folding=1        " R
let ruby_fold=1               " Ruby
let sh_fold_enabled=1         " sh
let vimsyn_folding='af'       " Vim script
let xml_syntax_folding=1      " XML

zo -- To open a fold block
zc -- To fold a block
zr -- Fold less
zm -- Fold more