Friday, August 29, 2014

Emacs

open
emacs& RET     or click a graphical icon
emacs -nw RET     start in terminal, not in a windowing system
cancel
C-g     a command
M-g g    Go to line #

Text size
Shrink/Grow: C - - / C - + 

Copy & Paste

C-<space>
    Start selection
C-w
    Cut
M-w
    Copy
C-y
    Paste

12.1.2 Killing by Lines

C-k
Kill rest of line or one or more lines (kill-line).
C-S-backspace
Kill an entire line at once (kill-whole-line)

M-: evaluate function in minibuffer
(vi yy): C-a (beginning) C-spc (highlight) C-e (end) C-y (yank)

M-<Move to the top of the buffer (beginning-of-buffer). With numeric argument n, move to n/10 of the way from the top.
M->Move to the end of the buffer (end-of-buffer). 

15.10.4 Query Replace


M-% string <RET> newstring <RET>
Replace some occurrences of string with newstring. 
C-M-% regexp <RET> newstring <RET>
Replace some matches for regexp with newstring.

26.3.2 Indenting Several Lines

C-M-q
Reindent all the lines within one parenthetical grouping. 
C-u <TAB>
Shift an entire parenthetical grouping rigidly sideways so that its first line is properly indented. 
M-x indent-code-rigidly
Shift all the lines in the region rigidly sideways, but do not alter lines that start inside comments and strings.
C-u 4 M-x indent-rigidly to indent the region by four spaces, C-u -4 M-x indent-rigidly to remove four spaces

Also, in Python mode, with a marked region, use C-c > and C-c <

Displaying in Another Window

C-x 4 is a prefix key for commands that select another window (splitting the window if there is only one) and select a buffer in that window. Different C-x 4commands have different ways of finding the buffer to select.

C-x 4 b bufname RET
Select buffer bufname in another window. This runs switch-to-buffer-other-window.
C-x 4 f filename RET
Visit file filename and select its buffer in another window. This runs find-file-other-window. See section Visiting Files.
C-x 4 d directory RET
Select a Dired buffer for directory directory in another window. This runs dired-other-window. See section Dired, the Directory Editor.
C-x 4 m
Start composing a mail message in another window. This runs mail-other-window, and its same-window version is C-x m (see section Sending Mail).
C-x 4 .
Find a tag in the current tag table in another window. This runs find-tag-other-window, the multiple-window variant of M-. (see section Tag Tables).

Deleting and Rearranging Windows


C-x 0
Get rid of the selected window (kill-window). That is a zero.
C-x 1
Get rid of all windows except the selected one (delete-other-windows).
C-x ^
Make the selected window taller, at the expense of the other(s) (enlarge-window).
C-x }
Widen the selected window (enlarge-window-horizontally).

Friday, March 09, 2012

Linux handling of sockets and open files

# kill all processes with open TCP sockets
kill -9 `lsof | grep ESTABLISHED | awk '{print $2}'`

# kill processes by port number of the TCP socket
fuser -k 22/tcp

Thursday, February 02, 2012

Teradata: Normalize BTEQ output

bteq <<EOF | 
awk 'BEGIN { 
   start=0; 
} 
{ 
   if (start==1) { 
      if (/^$/) { 
         exit; 
      } else { 
         print; 
      } 
   } 
   if (/^---/) { 
      start=1; 
   } 
}'
.logon host/user,password
select top 3 tablename
from dbc.tables
where databasename = 'myDB'
and tablekind='T'
order by 1
;
.logoff
.exit
EOF

Tuesday, September 06, 2011

bash: randomize order of arguments

n=$#
c=0
declare -a s
for r in $(seq 1 $n)
do
while [ -z $i ] || [ $((${s[$i]})) -eq 1 ] && [ $c -lt $n ]
do
u=$(od -N2 -tu2 -An /dev/urandom)
i=$(( $u % $n + 1))
done
s[$i]=1
c=$(($c + 1))
echo $i
i=''
done

Tuesday, July 05, 2011

BASH: Random text file sample (linewise)

num=$(wc -l < file.txt)
for i in $(seq 1 1000)
do
   n=$(( $RANDOM * $RANDOM % $num ))
   sed -n "${n}p" file.txt >> /tmp/file.txt.sample
done

Tuesday, February 01, 2011

Teradata: timestamp literal

select cast('04/10/2009 02:56:12.937' AS TIMESTAMP(3) FORMAT 'DD/MM/YYYYBHH:MI:SSDS(3)')

Tuesday, October 26, 2010

perl: Lookahead REs

Convert:
...
1|30|1|201002|745.11267931439|84.34214120933
1|30|1|201003|864.95194019049|322.6665129525
1|30|1|201006|831.27144204046|56.168021427546
...

into (truncate fractionals after 3 digits):

1|30|1|201002|745.112|84.342
1|30|1|201003|864.951|322.666
1|30|1|201006|831.271|56.168

use:

cat myfile | perl -pe 's/(?<=\.[0-9]{3})[0-9]*//g;'

note:

-p implies

while (<>) {
...
} continue {
print or die "-p destination: $!\n";
}