Muffinresearch Labs by Stuart Colville

BASH: Using brace expansion | Comments (0)

Posted in Code, Linux/Unix on 22nd June 2009, 10:17 pm by Stuart

There’s a nice feature of BASH which is to use a comma delimited list of strings inside of curly braces to reduce the amount of typing:

here’s an example of using brace expansion to create log files for apache:

sudo touch {access,error}.log

Something seen less often is a blank entry like so:

cp foo{,.bck}

Which is shorthand for cp foo foo.bck. This is really useful when copying and moving files around using long paths. Using brace expansion can minimise the amount of typing and help avoid errors caused by typos too.

Another use of brace expansion since BASH v3.0 is the possibility of using ranges within the lists like so:

for foo in {1..10}
> do
> echo $foo
> done
1
2
3
4
5
6
7
8
9
10

Or alternatively with letters too:

echo {a..f}
a b c d e f

See the advanced bash scripting guide for more examples.

Post Tools

vim: automatically highlight long lines | Comments (0)

Posted in Code, Linux/Unix on 22nd June 2009, 9:50 pm by Stuart

This .vimrc snippet highlights lines when you exceed 77 columns - this is especially useful if you are trying to adhere to PEP8 with Python development.

The if statement makes this work for older vims as well as more recent versions which is handy if you put your .vimrc on remote boxes as vim below v7.1.40 doesn’t have the matchadd function.

" Highlight lines over 77 columns
if has('matchadd')
    :au BufWinEnter * let w:m1=matchadd('Search', '\%<81v.\%>77v', -1)
    :au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
else
    :au BufRead,BufNewFile * syntax match Search /\%<81v.\%>77v/
    :au BufRead,BufNewFile * syntax match ErrorMsg /\%>80v.\+/
endif

To configure highlighting to work manually see the vim wiki for more details http://vim.wikia.com/wiki/Highlight_long_lines

Whilst I’ve often heard people snub the PEP8 recommendation for keeping line length to under 79 chars, I’ve personally found it leads to more readable code whether you live in a terminal or use gui editors. YMMV

Post Tools

Bash: turn on case-insensitive tab completion | Comments (0)

Posted in Linux/Unix on 6th June 2009, 10:24 am by Stuart

I always forget this one, but it’s dead handy as it allows you to type “py” hit tab and it will auto complete for “Python” or “python” for example. Without this setting you’d have to type explicitly what you want. To enable run:

echo set completion-ignore-case on | sudo tee -a /etc/inputrc

tee -a appends to the file - though clearly proceed with caution and if in any doubt backup /etc/inputrc first.

Post Tools

Ubuntu: Turn off changing workspace with mouse wheel|(0)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Ubuntu: Mounting a TrueCrypt volume at startup or from the CLI|(0)

All you need is a mount point e.g: mkdir /media/ and then use the following: truecrypt /path/to/truecrypt/diskimage /media/. This is handy when you want to mount a truecrypt image at start-up or just do it quickly from a shell. It’s far more direct than using the GUI.

Photos on Flickr

© Copyright 2004-09 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.660s.