Recipes for linux/unix

A mixed bag of tricks I learned over the past decades.

Emacs.   In file ~/.emacs I have:

    ;; custom-set-variables was added by Custom.
    ;; If you edit it by hand, you could mess it up, so be careful.
    ;; Your init file should contain only one such instance.
    ;; If there is more than one, they won't work right.
   '(blink-cursor-mode nil)
   '(inhibit-startup-screen t)
   '(save-place t nil (saveplace))
   '(show-paren-mode t)
   '(tooltip-mode nil))

    ;; custom-set-faces was added by Custom.
    ;; If you edit it by hand, you could mess it up, so be careful.
    ;; Your init file should contain only one such instance.
    ;; If there is more than one, they won't work right.
   '(default ((t (:inherit nil :stipple nil :background "ivory" :foreground "black" :inverse-video nil :box nil 
    :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 106 :width normal 
    :foundry "unknown" :family "DejaVu Sans Mono")))))

  ;; set window geometry = size and position 
  ;;   do here instead with --geometry option in Xwindow call 
  ;;   to avoid the irritating superfluous gtk error message in Ubuntu 14.04
  (add-to-list 'initial-frame-alist '(height . 38))
  (add-to-list 'initial-frame-alist '(width . 78))
  (add-to-list 'initial-frame-alist '(top . 1))
  (add-to-list 'initial-frame-alist '(left . 1000)) ;RR = flush right

  ;; color theme 
  (require 'color-theme)

  ;; take all bars off (get menu instead by CNTRL + rightclick)
  (tool-bar-mode -1)
  (menu-bar-mode -1)
  (scroll-bar-mode -1)

  ;; general settings
  (global-unset-key "\M-t") 
  (global-unset-key "\C-z")  ;; take out minimize to hidden

  ;; general settings copied from Han Uitenbroek
  (setq meta-flag t)                           ;; enable the Meta key
  (setq default-major-mode 'text-mode)  ;; make text the default mode
  (setq require-final-newline t)              ;; terminate the file with a newline
  (setq next-line-add-newlines nil)          ;; don't add new lines at end
  (global-unset-key "\C-h")                  ;; don't want help
  (global-set-key "\M-n" 'goto-line)        ;; faster goto line: ALT N
  (global-set-key "\M-h" 'help-command)       ;; use M-h for help
  (global-set-key "\b" 'backward-delete-char)   ;; restore backspace
  (setq-default case-fold-search nil)         ;; make searches case sensitive

  ;; text mode for my own extensions and for mutt
  (setq auto-mode-alist (cons '("\\.jot$" . text-mode) auto-mode-alist))
  (setq auto-mode-alist (cons '("\\.pln$" . text-mode) auto-mode-alist))
  (setq auto-mode-alist (cons '("\\.man$" . text-mode) auto-mode-alist))
  (setq auto-mode-alist (cons '("\\.idl\\'" . idlwave-mode) auto-mode-alist))
  (setq auto-mode-alist (cons '("mutt\\$" . text-mode) auto-mode-alist))

  ;; IDLWAVE preferences for IDL
  (setq idlwave-shell-automatic-start t)     ;; t = yes, nil = no
  (setq idlwave-shell-use-dedicated-frame t)
  (setq idlwave-shell-save-command-history t)
  (setq idlwave-shell-automatic-electric-debug nil)
  (setq idlwave-main-block-indent 0)  ;; indentation of a pro body
  (setq idlwave-block-indent 2)  ;; indentation of a begin-end subblock 
  (setq idlwave-end-offset -2)  ;; align the end with the begin
  (setq idlwave-continuation-indent 2) ;; indent a line after $ continuation
  (setq idlwave-max-extra-continuation-indent 10)  ;; no far-right alignment
  (setq idlwave-special-lib-alist '("/rutten/idl/rridl/" . "rridl"))
  (setq idlwave-shell-debug-modifiers   ;; H-c = compile (in separate shell)
     ;; (H = hyper, for me the redefined Windows key) 
  (setq idlwave-no-change-comment ";")  ;; don't change my comment layout
  (setq idlwave-expand-generic-end t)   ;; convert END to ENDIF etc
  (setq idlwave-shell-frame-parameters  ;; wide low frame for shell window
    (quote ((top . 400) (left . 50) (height . 20) (width . 140) 
  (add-hook 'idlwave-mode-hook (lambda ()
    (idlwave-auto-fill-mode 0)  ;; no auto filling
  (add-hook 'idlwave-shell-mode-hook (lambda ()
    ;; custom mouse examine commands (click twice)
    (idlwave-shell-define-key-both [H-mouse-1]    ;; print value(s)
      (idlwave-shell-mouse-examine "print,___"))     
    (idlwave-shell-define-key-both [H-mouse-2]    ;; print dimensions
      (idlwave-shell-mouse-examine "print, size(___,/DIMENSIONS)"))
    (idlwave-shell-define-key-both [H-mouse-3]    ;; generic print/plot/play
      (idlwave-shell-mouse-examine "sv,___"))     ;; sv = my routine to show variable

  ; cutout the annoying yes/no caveat at C-x C-c closure that there are
  ;   still active processes running (namely in the **IDL** frame from IDLWAVE)
  (add-hook 'comint-exec-hook (lambda () 
    (process-kill-without-query (get-buffer-process (current-buffer)))))
  ; comment/uncomment 
  (global-set-key "\M-c" 'comment-region)       ;; comment region
  (global-set-key "\M-u" 'uncomment-region)     ;; uncomment region

  ;; insert date, from  Han Uitenbroek
  (defun insert-date()    
    "*insert the current date in the text buffer"
     (let ((now (current-time-string)))
       (concat (substring now 4 8)
               (substring now 8 11)
               (substring now 20)
               '" "))))
  (global-set-key "\C-xd" 'insert-date)         ;; C-x d (instead of dired)
  (global-set-key "\C-x\C-d" 'insert-date)      ;; C-x C-d too

  ;; bibtex mode
  (autoload 'reftex-mode "reftex" "RefTeX Minor Mode" t)
  (autoload 'turn-on-reftex "reftex" "RefTeX Minor Mode" nil)
  (add-hook 'LaTeX-mode-hook 'turn-on-reftex)

  ;; fill out 
  (defun my-fill-sentence ()
    "Fill sentence separated by punctuation or blank lines."
    (let (start end)
        (re-search-backward "\\(^\\s-*$\\|[.?!]\\)" nil t)
        (skip-syntax-forward "^w")
        (setq start (point-at-bol)))
        (re-search-forward "\\(^\\s-*$\\|[.?!]\\)" nil t)
        (setq end (point-at-eol)))
        (narrow-to-region start end)
        (fill-paragraph nil))))
  (setq normal-auto-fill-function 'my-fill-sentence)

  ;; redefine ESC q in .tex files = new-sentence-on-new-line paragraph reform 
  ;; Modified from
  (defadvice LaTeX-fill-region-as-paragraph (around LaTeX-sentence-filling)
    "Start each sentence on a new line."
    (let ((from (ad-get-arg 0))
          (to-marker (set-marker (make-marker) (ad-get-arg 1)))
      (while (< from (marker-position to-marker))
        ;; might have gone beyond to-marker---use whichever is smaller:
        (ad-set-arg 1 (setq tmp-end (min (point) (marker-position to-marker))))
        (ad-set-arg 0 (setq from (point)))
        (unless (or (looking-back "^\\s *")
                    (looking-at "\\s *$"))
      (set-marker to-marker nil)))
  (ad-activate 'LaTeX-fill-region-as-paragraph)
I start emacs sessions with a csh alias em:
   alias em 'env XMODIFIERS="@im=none" emacs --title !^ !^ &'
where the XMODIFIERS bug repair needed in Ubuntu 14.04 makes the Compose Key (CAPS LOCK for me) work also in emacs.  I have a special alias for my todo file:
    alias emtodo 'emacs -Q -D --title TODO -bg LightPink
      -fn "-*-dejavu sans mono-*-*-*-*-*-106-*-*-*-*"
      -fg black -geometry 78x40+250+50 ~/todo &'

Remote machine use without password.   Great for fast and unattended use of ssh, scp, and rsync.  Go to (or mkdir) ~/.ssh, type ssh-keygen -t dsa, press enter until you get the prompt again, skipping the requested passphrase, then append the content of your newly made local file ~/.ssh/ into the remote file ~/.ssh/authorized_keys2.

Remote backup.   I use scripts as

  # script: syncwork2server
  xhost +
  setenv RSYNC_RSH ssh
  rsync --delete -bvauz --backup-dir=~/syncbck-work  ~/work 
    | tee ~/synclogs/sync-work.txt
which makes directory ~/work on a large server disk elsewhere identical to the directory ~/work in my laptop, with the files that are deleted or changed in the remote machine back-upped into a directory syncbck-work there and with a log of rsync's latest doings written in file synclogs/sync-work.txt in my laptop.  I run such scripts daily from a startup "good-morning" script wherever I am, to ensure that I have an up-to-date and always reachable backup in a safer place than my laptop whereabouts.

Mutt.   For my mailing I was advised many years ago to transit from elm to less-sucking mutt ( and have been happy with it ever since.  It gives great flexibility in cleaning out html duplicates and attachments and it stores mails as straight text files that can easily be searched and edited.  I use mutt with msmtp serving to Google's gmail as IMAP server.  [Install:  mutt], [install:  msmtp], [install:  mp], add a file ~/.mutt_alias with your aliases in the form alias doe John Doe <> and a file ~/.muttrc specifying your preferences, see the mutt manual.  Pertinent parts of my .muttrc (modified to John Doe where appropriate):

  source ~/.mutt_alias
  set sendmail="/usr/bin/msmtp -a google"
  set sendmail_wait=1
  set imap_user = ''
  set imap_pass = 'JohnDoegmailpassword'
  set spoolfile = imaps://
  set folder = imaps://
  set smart_wrap        # wrap long lines at word boundary
  set pager_stop        # Don't jump to next message at end of message
  set weed              # weed headers, when forwarding with forward_decode
  set reverse_alias       # use my person names through reverse alias lookup
  set from=""  # always same sender wherever it comes form
  set use_from=yes
  set envelope_from=yes
  set connect_timeout=-1
  ignore *
  unignore from date subject to cc   # copy minimal headers in forwards
  color header black default .       # I don't like color crowding
  color body black default .
  color quoted black default
  color attachment black default
  color tree black default
  color indicator black default
  color status black default
  color tilde black default
  color normal black default
  color hdrdefault black default
  color quoted black default
  color signature black default
  color error black default
  color tree black default 
  color tilde  black default
  color message  black default
  color markers  black default
  color attachment  black default
  color search  black default
  color underline black default
  color body black default "(ftp|http|https)://[^ ]+"   # point out URLs
  color body black default [-a-z_0-9.]+@[-a-z_0-9.]+    # e-mail addresses
  macro index \cb "|urlview\n" "open url with urlview" 
  macro pager \cb "|urlview\n" "open url with urlview"
  set use_8bitmime
  set abort_nosubject=ask-yes
  set abort_unmodified=no
  set alias_file="~/.mutt_alias"
  set allow_8bit
  set arrow_cursor
  unset askcc
  set attach_sep="\n"
  set attribution="On %d, %n wrote:\n"
  unset autoedit
  set auto_tag
  set bounce_delivered
  unset beep_new
  unset confirmappend
  unset confirmcreate
  set copy=yes
  set date_format="!%a, %b %d, %Y"
  set delete=yes
  set editor="emacs -nw --color=no" 
  unset fcc_attach         # do not save my own sent attachments
  set forward_decode
  set forward_format="%s [fwd]"
  set include=ask-no
  set index_format="%4C %Z %{%b %d %Y} %-15.15L (%4c) %s"
  set ispell="/usr/lib/ispell"
  set mail_check=120
  set mime_forward=ask-no
  unset mime_forward_decode
  set move=no
  set postpone=yes                   # keep mails that weren't sent
  set postponed="~/mail/postponed"   # go there with R (recover)
  set print=yes   # no question whether to print just do it
  set print_command="/usr/bin/mp -a4 -left 60 | lpr -Pprintername"
  unset prompt_after
  set realname="John Doe"
  set recall=no
  set record="~/mail/sent"
  set reply_to=yes
  unset save_empty
  unset save_name
  set sort=reverse-date-sent
  set status_format="Mutt: folder %f Msgs:%?M?%M/?%m%?n? New:%n?%?o? Old:%o?%?d? "
  set status_on_top
  bind pager <Up> previous-line  # up-down cursoring within mail
  bind pager <Down> next-line    # left-right = previous and next mails
  unset suspend
  set timeout=60
  bind pager <Up> previous-line  # cursor & page up/down, wheel in messages 
  bind pager <Down> next-line
  auto_view text/html
Pertinent parts of my ~/.msmtprc file:
  # defaults
  tls on
  tls_starttls on
  tls_certcheck off
  logfile ~/.msmtp.log
  # gmail
  account google
  port 587
  auth on 
  password johndoegmailpassword
Pertinent parts of my ~/.mailcap file:
  text/html; elinks %s; nametemplate=%s.html 
  text/html; elinks -dump %s; nametemplate=%s.html; copiousoutput
  application/pdf; mutt_bgrun acroread -tempFile -openInNewWindow %s; test=sh -c 'test $DISPLAY'
  macro attach E <save-entry><kill-line>/home/johndoe/attachmentsdir/<enter>
  image/*;        xv %s > /dev/null

Photos.   I unload the SD-card of my camera by sticking it into the card reader of my laptop and using a script as:

  # script: unloadcamera, thanks to Cees Bassa
  cd /media/*/DCIM
  cp -uprv *PANA ~/photos/camera/origs
  foreach dir (*PANA)
    cd $dir
    chfil JPG jpg
    jhead -n%y%m%d-%H%M%S-%f -autorot  *.jpg
    cd ~/photos/camera/origs
  setenv TODAY `date "+20%y-%m-%d"`
  mkdir -p $TODAY
  mv *_PANA/* $TODAY
  rmdir *_PANA
  rm -rf  /media/*/DCIM/\*
where [install:  jhead] is used to redefine photo filenames starting with date-time.  They go into an camera/origs subdirectory with the present unload date as name.  I run similar scripts for other cameras so that merging their files puts all photographs in sequential time order (if the camera clocks were synchronous; in group travel one should ask everybody to reset their camera to local time, but if this was not done one can readjust the exif time stamps afterwards with the jhead -ta and jhead -da options).  Script chmodopen resets directory and file protections in the current directory and underneath to read permission for others:
  # script: chmodopen, from Wim Lavrijsen
  chmod 755 .
  find . -maxdepth 10 -type f -exec chmod 644 {} \; 
  find . -maxdepth 10 -type d -exec chmod 755 {} \;
of which the reverse chmodclose has chmod settings 600 and 700.  Script chfil modifies filenames in the current directory by replacing filename parts:
  # script: chfil, by Alfred de Wijn
  foreach file (*$1*)
    mv ${file} `echo ${file} | sed -e "s|$1|$2|g"`
and has a counterpart chfildeep to do so also in underlying directories:
  # script: chfildeep, by Alfred de Wijn
  find . -mxdepth 10 -type f -name "*$1*" | sed -e "s/\(.*\)$1\(.*\)/mv \1$1\2 \1$2\2/" | bash
After manually weeding out bad photos in the origs dir with qiv, I use the following script to produce XVGA (1024x768) versions for spreading and projection in another directory:
  # file: cameraconvertall 
  #       usage: cameraconvertall dir/filenamestart
  foreach file (*.jpg)
    setenv FILETAIL `echo $file | sed -e "s|.jpg||"`
    convert -linear-stretch 0.1%x0.1% -sharpen 2.0x2.0 -geometry x768 -quality 90 $file ${1}${FILETAIL}-NS.jpg
ImageMagick used by convert has lots of worthwhile commands, e.g., convert -sigmoidal-contrast which emulates the nonlinear growth-curve response of photographic emulsions that made my Kodachrome slides span large contrasts much better than digital projection does.  For example, convert -sigmoidal-contrast 3,0% brightens too dark objects.

I then use [Install:  album] from to generate linked albums like my travel albums and the DOT solar image album and the DOT photograph album with:
    album -clean -index album.html -no_image_pages -no_embed -geometry 300x300 -no_crop -columns 3 .
generating captions with a file captions.txt.

Movies.   I use [Install:  mpv] as versatile successor to mplayer. I call movies in pdflatex with (for example):

where I obtain the linker image movie-snap.jpg as a snap from album.  I also use album to add .avi and .mov versions of solar .mpg movies made with IDL from DOT image sequences to the DOT movie album with:
  # script: dotmovie2album = convert .mpg movie into .avi and .mov
  # params: $1 = desired fps (f.s) playing rate, $2 = filename.mpg
  ffmpeg  -sameq  -i $2  $2:gr.avi
  avifix  -f $1,1  -i $2:gr.avi
  ffmpeg -sameq  -i $2:gr.avi  $
  mv $2:gr.avi ../dot-albums/movies
  mv $ ../dot-albums/movies
  cd ../dot-albums/movies
  album -clean -index album.html -no_embed -geometry 400x400 -no_crop -columns 2 .
needing [install:  ffmpeg] and [install:  transcode] which contains avifix.  They may need non-free codecs from the medibuntu repository:
  sudo echo "deb jaunty free non-free"  >> /etc/apt/sources.list
  sudo apt-get update && sudo apt-get install medibuntu-keyring && sudo apt-get update
  sudo apt-get install non-free-codecs
The .mov versions play even in the simplest QuickTime players.

Grep manual.   I often use a special alias rr meaning "Rob-asks-Rob-a-question": 
   alias rr 'grep \!^ ~/manuals/rrmanual.txt'
which answers my questions "rr some-term" such as "rr chmod".  File rrmanual.txt has many one-liner linux reminders to myself, including:

  colors Ubuntu   em /usr/share/texmf-texlive/tex/latex/graphics/dvipsnam.def
  xfontsel &                select a font
  xlsfonts > fonts.fil      write all font names to file
  chmod 644 file       -rw-  r--  r--    others may read (umask 022 default))
  chmod 600 file       -rw-  ---  ---    others may not read file
  chmod 755 dir        drwx  r-x  r-x    others may enter (umask 022 default)
  chmod 755 script     drwx  r-x  r-x    others may execute 
  chmod 700 dir        drwx  ---  ---    others may not enter dir
  chmod 744 script     drwx  r--  r--    others may read; I may execute  
  chmod 444 file       -r--  r--  r--    even I may read only, protection
  chmod 711 dir        drwx  --x  --x    others may enter but not list dir
  chmod root.root file                   reset to owner/group both root  
  cp -uprv dirname dir > cp dirnamedir.txt    copy update all, with log file
  convert -density 300 -quality 80 -crop 0x0 -geometry x768 file.jpg
  convert 'vid:*.jpg' directory.miff     make thumbnail index album 
  display directory.miff &               show thumbnail index album 
  gqview                                 image browser, slide show
  qiv -fim *jpg                          fast image viewer, d=delete to subdir
  qiv -fis -d 1.5 *jpg                   fullscreen slide show 1.5s cadence
  feh                                    fast image viewer 
  eog                                    eye-of-gnome image viewer
  jpeg2ps -h fig.jpg > fig.eps           good image converter
  ps2pdf -dPDFsettings=/prepress file.pdf          like acrodist
  psnup -2 >            2A4-on-one pdf talk printout 
  ps  612 792 <-> 595 842 (pt)           hack ps to convert USA B4 <-> A4   
  ps x | grep processname                list all processes called processname
  ifconfig                      check ethernet 
  iwconfig                      check wireless 
  recode ibmpc:latin1 file      decode windows file to unix
  xset s off                    screensaver off
  screen (in remote terminal) = start window for remote processing
    CNTRL-a        screen commando
    CNTRL-a c      screen new terminal
    CNTRL-a space  screen cycle terminal
    CNTRL-a d      screen detach terminal (then log out of remote)
    screen -r      screen recover detached terminal (after remote login)
  renice +18 pid   put pid job in batch, sort of (eg when using screen)
  tar cvf *17Jul96.???           tar with joker/wildcard: xx.100-999
  tar hcvf dir.tar dir           tar dir with all under, also linked files
  tar xvf files.tar              extract files into directory structure 
  tar tf files.tar               list files only
  wget -r http://etc             get everyting below and above this URL
  wget -r -l 1 -nd http://etc    get everything in this URL dir only  
  lftp ftp://... or lftp http://...    better than wget
  lftp mget -d *                       get all subdirs and files
  lftp mirror -n .                     get update with newer files only
  mgdiff file1 file2              linux: differences between two files
  diff -y -W170 --suppress-common-lines file1 file2    differences files
  aspell -t --dont-tex-check-comments check file.tex   latex spell checker
  env TZ=UTC date                 give current date and time in UT