Linux notes, mostly done on ubuntu > 17


Restore removed branch

Accidently removed a branch, restore it with

$ git reflog --no-abbrev

Find the SHA of the commit you want to restore

$ git checkout -b BRANCH SHA

go mod get private modules

$ export GOPRIVATE=github.com/USER/*
and in ~/.netrc add
machine github.com login YOUR EMAIL password GENERATED KEY

Advanced prompt

To create a shell prompt with a line separator, add to ~/.bashrc

hrPS1() { local start=$'\e(0' end=$'\e(B' line='qqqqqqqqqqqqqqqq' local cols=${COLUMNS:-$(tput cols)} while ((${#line} < cols)); do line+="$line"; done printf '%s%s%s\n' "$start" "${line:0:cols}" "$end" PS1="\u@\h:\w\n$ " } export PROMPT_COMMAND="hrPS1"

Use alternate ssh port

Add port to ~/.ssh/config

Host some.domain.com Port PORT

Scale fonts on high resoltion display

Add line to ~/.Xresources

Xft.dpi: 122

and reload with

$ xrdb ~/.Xresources

You have to restart any application for it to take effect

Set default GTK font

Edit ~/.config/gtk-3.0/settings.ini and restart any GTK application to apply settings

[Settings] gtk-font-name = DejaVu Sans 12

Keep linux running even if lid is closed on laptop

$ emacs /etc/systemd/logind.conf

Find and set the following

HandleLidSwitch=ignore HandleLidSwitchExternalPower=ignore

Switch between main terminal and recent window

Set title of your main terminal, ie. with xtitle command line

#!/bin/bash # alt tab switches between currently focused window and # xterm with title "command line" active=$(xdotool getactivewindow) name=$(xdotool getactivewindow getwindowname) if [[ $name == "command line" ]]; then # switch back to last active xdotool windowactivate --sync $(cat /tmp/lastactive) exit 0 fi # save currently active echo $active > /tmp/lastactive # switch to command line xdotool windowactivate --sync $(xdotool search --name "command line")

Emacs navigation

Ctrl-c-j jump to symbol, and back with Ctrl-x-space
Ctrl-c . Creating Timestamps in orgmode

Magic f

Increase shell productivity. Put in .bashrc

f() { path=$1; cmd=$2; cmdargs=${@:3} # the rest of them [ "$path" == "" ] && path="." filepath=$(dirname "$path") fullfile=$(basename "$path") extension="${fullfile##*.}" case $cmd in apt) aptitude search $path ;; apti) sudo aptitude install $path ;; apte) sudo aptitude purge $path ;; b|browse) firefox $path ;; c|cd) cd "$path" ;; d|rm) rm -v $cmdargs "$path" ;; cp|copy) cp $path $cmdargs ;; e|edit) emacsclient -n $cmdargs "$path" ;; ee) closeall; sleep 0.02; emacsclient -n $cmdargs "$path.go" ${path}_test.go ;; f) clear ls -B --group-directories-first --color -lh1 "$path" \ | egrep -v "^total" | order -f .lsorder ;; se) sudo emacs $cmdargs "$path" ;; o|open) case ${extension,,} in # lowercase extension mkv) vlc "$path" ;; dia) dia "$path" ;; pdf) xdg-open "$path" ;; png|jpg|gif|html) $BROWSER "$path" > /dev/null 2>&1 ;; m4a|mp3) mpg123 "$path" ;; eda) veda "$path" ;; xlsx|pptx|docx) libreoffice "$path" ;; *) echo "No opener for $extension" ;; esac ;; *) if [ -f "$path" ]; then less "$path"; elif [ -d "$path" ]; then clear ls -B --group-directories-first --color -1 "$path" \ | egrep -v "^total" | order -f $path/.lsorder else first=""; open="yes"; for f in $(find . -name "*${path}*"); do [ ! -z $first ] && open="no" # found another [ -z $first ] && first=$f # found one echo $f done [ "$open" == "yes" ] && emacsclient -n $first fi ;; esac }

xterm colors

Put in .bashrc

trap xtermcontrol INT ssh() { hot; command ssh "$@"; norm; } norm() { xtermcontrol --title "command line"; light; } light() { xtermcontrol --fg='#3b2616' --bg='#ffffff' ;} dark() { xtermcontrol --fg="Alice Blue" --bg='#2E2E34' ;} black() { xtermcontrol --fg="Alice Blue" --bg='black' ;} green() { xtermcontrol --fg="Green" --bg='black' ;} hot() { xtermcontrol --fg="orange" --bg='darkred' ;} yellow() { xtermcontrol --fg='#3b2616' --bg='#d4c97d' ;} gray() { xtermcontrol --fg='#3b2616' --bg='#c3c2bd' ;} tan() { xtermcontrol --fg='#3b2616' --bg='#b3a087' ;}

Colored dmenu

In .config/i3/config edit dmenu binding (all on one line)

bindsym $mod+d exec dmenu_run -l 10 -i \ -fn 'Inconsolata:bold:pizelsize=28' -nb '#000000' \ -nf '#00ff00' -sf '#000000' -sb '#00ff00'

Change visudo editor

$ sudo update-alternatives --config editor
Then select the prefered editor from the list.

Mount mp3 player as non root

Create directory with permissions
$ mkdir -p /media/mp3
$ chown gregory /media/mp3
$ chmod 0777 /media/mp3
In /etc/fstab add mount point
/dev/sda /media/mp3 auto user,umask=000,utf8,noauto 0 0
Then when connected mount with
$ mount /media/mp3

Clear bash history

In each terminal enter the commands

$ history -c
$ history -w

Generate svg from graphviz .dot file

$ dot -Tsvg -o diagram.svg diagram.dot

Reload window with xdotool

Look in the title for the window name, below example is for surf browser whose name is @cgDISVMf, not really a name but that is what's in the title :-))
$ browser=$(xdotool search --name "@cgDISVMf")
$ back=$(xdotool getactivewindow)
$ xdotool windowactivate --sync $browser
$ xdotool key --window $browser --clearmodifiers "CTRL+R"
$ xdotool windowactivate --sync $back

Ignore first N lines in pipes

$ cat SOMEFILE | awk '{if(NR>7)print}'


Default to runlevel 3 at boot

$ man systemctl
          $ systemctl isolate multi-user.target
          $ systemctl enable multi-user.target
          $ systemctl set-default multi-user.target

Install new harddrive

Use parted to partition the drive. Format it with eg. ext4 and then find PARTUUID

$ sfdisk -l
          $ blkid /dev/sda1

Update /etc/fstab (all on one line)

PARTUUID=51d86c9d-3a01-4bb6-b173-b6ec70d76a02 \ /media/newdrive ext4 errors=remount-ro 0 2

Centralize logging

With syslog-ng. On the sender side change /etc/syslog-ng.d/remote.conf

destination d_remote-syslog { network("" transport("udp") port(514)); }; log { source(s_system); destination(d_remote-syslog); };
« Home