Site perso : Emmanuel Branlard

Subsections

1. Unix commands


1.1 Commands

    * sort Sorts lines in ascending, descending and unique order
    * grep Searches for regular expressions in strings or files
    * basename Strips the path from a path string to leave just the filename
    * dirname Removes the file from a path string to leave just the pathname
    * cut Chops up a text string by characters or fields
    * wc Count the characters, words, or lines
    * [ (test) ] Predicate or conditional processor
    * tr 'a' 'b' Transform characters
    * expr Simple arithmetic processor
    * bc Basic Calculator
    * eval Evaluate variables
    * echo Output strings
    * date Create date strings
    * nawk Manipulate text strings
    * head | tail Access lines in files
    * sed stream editor, performs search and replace

1.2 Vi Vim or gVim

%%%Motions:
h j k l 0 $ ^   $%si multilignes : utiliser gk gj g$ g0   
12G : go to line 12
M : go to the middle of the screen
%z stuff -  combine it with: set scrolloff=3  for a smooother screen offset
zt : will scroll so that the current cursor is at the top
zz : will scroll so that the current cursor is at the middle
zb : will scroll so that the current cursor is at the bottom
CTRL-E CTRL-Y : scroll down
CTRL-B CTRL-F : page backwards page forward
CTRL-U CTRL-D : half page up half page down, or whatever you set with scroll (default scroll=0)
CTRL-o CTRL-I : go to older / newr position
w b e: next/previous word 
W B E : same but uses spaces as delimiters

%%% Basic
<C-r> : redo!!!
u U : undo
w : save
wq : save and quit 
ZZ : save and close file

%%% Insertions (ne pas se limiter a i!!!)
a i o
A I O

%%% Deletion/ Change / Selection / Copy - Same behavior
x
xp : reverse two letters, usefull for typos like : fro
dd
dj : delete current line and below
dw : delete the rest of the word and next space (good for full words)
de : delete rest of the word but not the space (good for the end of a word)

%%% Block stuff (don't know proper term)
di} or di{ : delete everything between brackets
dt} : delete toll the next braket but does not delete the }
df} : same but delete the bracket
vi} : select everything between braces (same as vi{)
va} : idem but includes braces
yi} : copy content between braces
ci} : etc ..

% Upercase -> till the end
D or d$ : delete till end of line $
The same apply for c and C, v V

%%% Replacement
r
cw : change word
bde : delete current word in totallity
bce : change current word in totallity
~ : switch uppercase lowercase

%%% Programming
% : go to next brace
zo : open folded code
zf in verbose mode to fold
zR : unfold all levels
zM : fold all
za : toggle paragraph folding, vim-latex only?
< >  : indent , use . to repeat
>>< << : indent
]p  : like p but adjusts indent
== : autoindent selection ?
= : autoindent line
If on a first braket { then : =% will indent everythin to the matchning bracket
If withing a code block then =a{ will work as well

%%% Paragraph reformating
set textwidth=80  0 to cancel
gqap : current paragraph
gggqG : all paragraphs in file
J   : join with line below; whole file : use cmd paste
rENTER : split ..

%%% Copy paste cut
p : put after  (uses the register ", i.e. equivalent to ""p)
P : put before
yy p : copy paste one line
dd p : cut paste one line
v y P  : select copy paste
"+y et "+gp ou "+gP copie entre documents ????
"0p : paste the register 0 (i.e. the one that has been yanked)
:reg display registers

%%% Marks (usefull when copying smthg far away)
ma : mark 'a', not a command no :
`a : go to a

%%% Search and replace
/ : search, moving with n and N
:s/aa/bb/g : all in current line
:%s/aa/bb/g : in all document
:l1,l2s/aa/bb/g : all between l1 and l2
:set hlsearch
:nohlsearch

% insert at beginning of line
Select beginning of line using CTR-V then SHIFT+I then type text to insert, then ESC (not CTRL+C)
% at end of line:
:<,>s/$/TEXT/g    bla bla-$

%%% Buffers
All files open in vim are listed as buffers. They can be opened, in tab or windows, it doesnt matter. This means that at any time you can access any buffer and put it in a tab or a window

:ls : list
:bn
:b number : go to buffer number
:bd : closes current buffer

%%% .Vimrc file
:so %  : source the file you are editing. if it's .vimrc, then config is reloaded automatically

%%% Windows 
:sp : opens in a split window
<C-W> s  : splits horizontally
<C-W> v :splits vertically
<C-W> w : loop through windows
<C-W> jkhl :move between windows
<C-W> _ : maximize window
<C-W> c : close window

%%% Tabs
:tabnew  :new tab
:tabe  :open a file in a new tab
:tabc
:tabn or gT  :next tab
:tabN or gt :previous tab

%%% INSERT mode
CTRL+C CTRL+[ or ESC to escape
CTRL+N CTRL+P : Word/variable/command completion!!!
CTRL+H : backspace
LOOK at my vimrc for my Insert settings

%%% the g command on a current word
% go to definition
gd gD
g* : look for this word (it starts immediately.. so use CTRL-O to go to previous position)

%%% Recording
qa record a macro in recording a
q  stops recording
@a : repeat recording

%%% Command history
in many situations, a better solution is the command-line window which you can open in two ways:

    Type q: for commands, or q/ for searches; or
    Type : or / to start entering a command or search, then press the 'cedit' key (default is Ctrl-f :help 'cedit'). 

The advantage of the command-line window is that you can use all Vim's editing power, including searching with '/' in normal mode, or using whole-line completion (:help compl-whole-line) in insert mode. After editing a command, you can:

    Press Enter to execute the current line (and close the command-line window); or
    Press Ctrl-c twice to close the command-line window (cancel).

1.3 Vim Colorscheme

for tests (:help hi)
:so $VIMRUNTIME/syntax/hitest.vim  $

1.4 Vim QuickFix window

:cn : go to next error
:cp : go to previous error

:cw : open quickfix window :cope
:cl : close quickfix window

1.5 Vim-latex

<-->: press CTRL-J to go to

%%% Vim Latexcommands with switch \
\ll : compile
\lv : view
\rf : refresh folds 

%%% Completion 
<F9> and grep installed 

%%% Latex in insert mode
SCH : chapters
SSE : sections
SSS : sub
SPG : paragraph

%environments
EIF : insert figure
EIT  : itemize   then <A-I> for items
EEQ
# where I put the Imaps and some shortcuts. On windows, in MyImpas.vim in the folder plugin
after/ftplugin/tex.vim 

% maths stuffs
`/ :frac
`I  :int
<A-L> after of before a parenthesis, completes it with \left ...

%%% References and citations completion with <F9>!!!

%%% LATEX VISUAL mode
`{  puts \left{ around selection
`( 

%%% Bib files
BBB : new entry

1.6 Vim en color

Dans le home mettre un fichier .vimrc avec :
syntax enable
set background=dark
filetype on
A part ca, pour un simple fichier taper ":syntax enable" suffit

- Si on a ce message:
E319: Sorry, the command is not available in this version: syntax on
alors faire un aptitude install vim (car tous les runtime ne sont pas presents)

1.7 Grep - List of my REGEXP

-Inrsi : my typical option
-sIrni

Basics options:
-r : recursive in folders
--color=auto : for better display
-I : ignore binary files
-E : will interpret the pattern as an extended regexp
-F : will interpret the pattern as a fixed string
-i : ignore case
-n : line number
-s : remove error messages

grep -rI --color=auto pattern ./*

Geany:
-nHIiF

%%% Regexp
Remember:
. : match one unique char, does not depend on what is before
* : zero, one or several times what is before
? : zero or one time 
+ : one time or more
(),n} : n times 
(){m,n} : between m and n

%Dates:
echo "1 18 19 20 21 1665 1620 1865 1995 2010 2100"| grep -E "(18|19|20)[0-9]{2}"
%Extensions 3-4 characters
echo "sdf.png" |grep -E "[.][a-zA-Z]{3,4}$"
%file name ugly
echo "sdfl_d séf - klkf.ds fskfjs "|grep -E "^[a-Z0-9\_ -]*[.]"
filename = ${f%.*}

1.8 FORLOOPS/ FIND / XARGS and SPACECHARACTERS

#! /bin/bash
echo "Looking for "$1

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for i in `find -name "*.pdf" -printf '%h/%f\n'  2> /dev/null`
do
        echo "----------------------------"
        echo "$i";
        echo "----------------------------" 
        pdftotext $i - |grep --color=always $1
#        strings "$i"  |grep $1
done;
IFS=$SAVEIFS

1.9 TEST

Look at Compound!

%%% String is empty
[ -z ${var} ]

Conditionnals tests are in bracket [ ]
See man test for details, like :
Expression comparison : !EX1 -a Ex2, Ex1 -o Ex2
String comp : S1 = S2, S1!=S2
Int comp :    I1 -ne I2

IF  :
if [] || [] && [] ; then ...;  elif [] ; then ...; else ...; fi

WARNING !! you need spaces around [ and = !!!!!!!!!!

1.10 TEST - COMPOUND

%%% String matching
[[ "abc def .d,x--" == a[abc]*\ ?d* ]]; echo $?

1.11 STANDARD IN, STANDARD OUT

| : PIPE
./compile_astra 2>&1 | tee log.loG

1.12 Shell

extension = ${f#*.}
filename = ${f%.*}

%%% location for local scripts 
% if local partition
(/usr/local/bin) 
% or in .bash_profile
PATH=$PATH:/home/manu/scripts/

1.13 Command FIND - application to album cover file

#find ./ -name "*.jpg"  -exec echo "`dirname "{}"`/cover.jpg" \;
toCover.sh :
#! /bin/sh

a=$1
echo "$a"
path=`dirname "$a"`
echo "$path"
cp "$a" "$path/cover.jpg"
echo ""

find ./ -name "*.jpg"  -exec ./toCover.sh '{}' \;
find ./ -name "*.JPG"  -exec ./toCover.sh '{}' \;

find ./ -name "*.wma"  -exec wma2mp3 '{}' \;

find ./* -size 0b -type f -exec rm {} \;

1.14 Remove consecutive blank lines - duplicate empty line

http://unstableme.blogspot.com/2008/12/remove-duplicate-blank-lines-bash-awk.php
%%% Way1:
cat(1) gives an option named
-s, --squeeze-blank
which says : never more than one single blank line
$ cat -s file.txt
%%% Way2:
$ awk '/^$/{ if (! blank++) print; next } { blank=0; print }' file.txt

1.15 AWK

awk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Usage: awk -f progfile [--] file 
Usage: awk 'program' file 
Usage: ./awkfscript

-F,      : field separator comma(,)
-F ";"   : field separator semicolumn(;)
-F ";|," : field separator semicolumn or comma(;)

#first 1000 lines
awk 'NR<1000 {print}' radial.ini>radi     

#lignes paires
NR % 2 == 1 { print $0 }   

#affiche le numero de ligne 
{ print NR, $0 }           

# First three fields
awk -F, 'NR>1 {print $1,$2,$3;}' tab.csv > tab2

# Not matching a pattern
awk '!/Distribution/ {print $0 } ' drift.in 

# Cut a line every 20 fields
awk -F, '{for(i=1;i<=NF; i=i+1){ if((i%20)==0  ){ printf $i";\n"  }else{ printf $i";"} } printf "\n"  }' p.csv >r.csv

# print sum of first record
awk '{ sum += $1 }; END { print sum }' file

awk -F= 'NR<10 {for(i=1;i<=NF;i++){printf $i"=" }; print "" }' drift.in

#printf "&NEWRUN\n  Distribution='$bunchdir/$3'\n" |tee $2>/dev/null

#awk 'BEGIN{IGNORECASE=1; FS=",|="}; /distribution/{for(i=1;i<NF;i=i+2){ if($i~/distribution/){}else{  print " ",$i,"=",$(i+1)}  }   } NR>1 && !/distribution/{print} ' $casedir/$2 > $tempBuffer
#cat $tempBuffer |tee -a $2>/dev/null

awk 'BEGIN{IGNORECASE=1; FS=",|="}; /distribution/{for(i=1;i<NF;i=i+2){ if($i~/distribution/){split(ARGV[2],a,"="); print a[1]=a[2;] }else{  print " ",$i,"=",$(i+1)}  }   } NR>1 && !/distribution/{print} ' $casedir/$2 Distribution=\'$bunchdir/$3\'> $2

#! /bin/awk 
 
BEGIN {
  IGNORECASE=1
  FS=",|="
  split(ARGV[2],a,"=");
  b=tolower(a[1])
}

$0 ~ b  {for(i=1;i<NF;i=i+2){ if($i~b){split(ARGV[2],a,"="); print " ",a[1],"=",a[2]; }else{  gsub(" ","",$i); print " ",$i,"=",$(i+1)}  }   }
$0 !~ b {print }

1.16 DU and DF

%%%DU
du -sh FOLDER/FILE  : disk utilisation, readable by human, s : include subfolders
the above will return the total

#listing space used by the subfolders in the current folder
alias duf='du --max-depth=1 -h '

%%%DF
df -al : disk space 
alias df='df -hT |egrep -i "file|^/"'

1.17 Patches - apply patch - create patch -reverse patch

%apply a patch
patch -p1 < /path/to/file.patch

man patch

%reverse a patch
patch -R < /path/to/file.path

% create a patch
diff -crB Folder1 Forlder2 > Folder.patch

1.18 Git

% download latest version
git clone git://git.gnome.org/evince

git branch : list branches
git branch -a : all 

% create a local branch corresponding to the remote branch
git checkout -b local-gnome-2-30  remotes/origin/gnome-2-30
% switch to a specific branch (updates files) (useless after previous command and the switch -b)
git checkout local-gnome-2-30
% Make directory according to this branch
%git pull







Emmanuel Branlard