Wednesday, June 27, 2012

Howto change Desktop fonts

Out of the box both GNOME and KDE applications are configured with the default Desktop Fonts of something like Sans 10 which looks ridiculously tiny and small on a 1440x900 LCD panel.
Here is how to fix it:

GNOME
  • Start GNOME Configuration Editor with Applications > System Tools > Configuration Editor
  • Drill down to desktop > gnome > interface
  • Change font_name attribute to Sans 14.
  • Set monospace_font_name attribute to Monospace 14.

KDE
  • Start KDE Control Center with kcontrol.
  • Go to Appearance & Themes > Fonts category.
  • Set Desktop fonts to Sans 14.

Saturday, June 23, 2012

How to test for an empty env var in a shell script

Recently, I came across one of my own bash scripts that goes way back. It relies on an environment variable being set, and if not, the script exits without a warning. I learned that the most elegant way to test if an environment variable is empty or unset is this:
 
# ${parameter:?word}
#
: ${BUILDDIR:?"Need to set BUILDDIR non-empty"}  

Here, if parameter ($BUILDDIR) is empty or unset, the expansion of word is written to the standard output and shell exits. The entire expression is an argument to the "colon command" (:) which simply evaluates its argument. According to UNIX historians, it is a derelict of good old days of the Bourne shell scripts which had every command start with colon.

Tuesday, June 19, 2012

How to find and replace a string in multiple files

To find and replace a tag across multiple files from command-line in linux, consider this simple shell script:
#!/bin/sh  
today=`date -R`
find . -type f \( -name "*.h" -o -name "*.cpp" \) -print | \
   xargs sed -i 's/{DATE}/'"$today"'"/g'

We scan for regular C++ header and source files. We then pipe the list of files found into 'sed' editor that does line-by-line search and replace.

The only non-trivial part here is the replacement string, '"$today"'. The inside double quotes are needed to let shell expand $today to its value.

Wednesday, June 13, 2012

How to insert spaces with Tab key with Vim

Some projects require for one reason or another to use spaces instead of a Tab character to indent the C/C++ code. Most of the time it is related to the diffs that are generated by the CM systems. If you are a Vim user, you can configure your editor to insert spaces with Tab keystroke by adding these commands to your ~/.vimrc:

 set ts=4  
 set shiftwidth=4  
 set expandtab  
 set nowrap  

This assumes that the width of desired tab is 4 characters. gVim is a popular GUI font-end for Vim editor. However, I found the way gVim stores its configuration into ~/.vimrc non-intuitive:
  • Go to your $HOME directory.
  • Start the shell terminal.
  • Start gvim from command-line.
  • Configure your settings the way you like them to be.
  • Save the setting to .vimrc with command
     :mkvimrc  
    
The trick here is to run gvim from your home directory. Otherwise, gVim writes .vimrc file in the current directory where it was started from.