Random code snippets

Random but oddly useful code snippets that I use from time to time.

  • To display images as a preview and a clickable link in HTML:
<a href="http://dl.dropbox.com/u/54881717/screenshots/DSL/dsl2.jpg"><img alt="DSL Creeper BG" src="http://dl.dropbox.com/u/54881717/screenshots/DSL/dsl2.jpg" width="400" height="300" /></a>

Width and height need to be manually set.

 

  • Main function for a “do something to every file (including those in subdirectories) within specified folder”, written in bash:
#!/bin/sh

echo "Enter target dir"
read -p ">" workdir
while [ ! -d $workdir ]; do
echo "That directory does not exist. Please enter target dir"
read workdir
done
cd $workdir
for i in $(find -type f -name '*'); do
echo "Ho." > $i
done

Emacs tips and hacks

Suppress splash screen

Default behavior for emacs is to display the splash screen upon opening, even if you’re opening a file in it (which results in an annoying split screen). To force emacs to NOT show the splash screen, add this to ~/.emacs

(setq inhibit-splash-screen t)

Change default save directory (Windows)

To change the default save directory in Windows (ugh), simply edit your shortcut. Go to Properties>Shortcut> and change “Start in” to the directory of your choice.

Setting Custom Keybindings

C-c char is reserved for custom keybindings. You can set a keybinding for a long command with options by adding a new function and calling it.

(defun mycommand ()
  (interactive)
  (command_here)
  )
(global-set-key (kbd "C-c i") 'mycommand)

If it’s a single command with no options, you don’t need to create a new function and can call it as shown.


Random Regexes

Grep regex for extracting URLS from HTML source:

"(?<=href\=\")(http)[^\"]*(?=\")"

This should also work in a perl script.


Pip – Boy zsh .envrc

For z shell. This file should be saved as ~/.envrc. Feel free to tweak it to fit your needs. I had originally given the text of the file here, but WordPress editing messed it up- therefore, you can get it by clicking here.
Screenshot:

Pip-Boy Zsh


Pip – Boy zsh .zshrc

I’m currently using Z shell, or zsh for short. It seems to pretty much be bash + extra goodies, such as extended auto-complete and spell check (yes, spell check, in a terminal. To make sure you type the right command.)

It’s pretty easy to configure zsh prompts and profiles. Zsh uses two files: .zshrc and .envrc.

Installing zsh is quite easy, as it’s available in the repositories of all major distros.

sudo apt-get install zsh

for Debian users or

sudo yum install zsh

for RPM’ers will get it for you.

Posted below is my .zshrc file. It’s modeled vaguely after the Pip Boy in Fallout: New Vegas, simply because I admire the retro styling of the Pip Boy. Copy the code and save it as ~/.zshrc. I based mine off of bodhi.zazen’s, although they don’t resemble each other very much any more. Feel free to tweak it, play with it, make it suit your own needs.

Originally I had the text of the file posted here, but WordPress markup apparently messes it up. You can get it by clicking here.

 

Screenshot:

Pip-Boy Zsh


Display Switching Script

This is my script for detecting if an external monitor is plugged in, and, if so, making it primary. Add this to your startup applications and your desktop will automatically configure itself after you log in.

#!/bin/sh

chdmi=xrandr | grep "HDMI1 connected"
if [ -n $chdmi ] ;
then xrandr --output HDMI1 --primary ;
else
xrandr --auto
fi

Note that you can simply change HDMI1 to VGA1 or DVI1, according to what port you use for your external monitor.