Thursday, August 30, 2012

Configure bash prompt

I like a simple prompt that shows only the working directory rather than the full path. I don't do a lot of remote work, so I don't need the user and host information.  I color my prompt purple because ls uses that color only for symbolic links (magenta) so my prompt is not similar to typical ls ouput.  This differentiates the prompt from a wall of green (commands and files) and blue (directories) text.  Configure the bash prompt in the /home/USER/.bashrc file.  First, uncomment the force_color_prompt option as shown below:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
   The comments are part of the /home/USER/.bashrc file and the variable is commented out by default.  Next change the PS1 variable for both color and non color options from the default USER@HOST:FULLPATH$ to a simple CURRENTDIRECTORY$.  Comment out the default prompt and add the customized prompt below it.  That way you can revert if you make a mistake.
if [ "$color_prompt" = yes ]; then
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\W\$\[\033[0;0m\] '
else
#  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
   PS1='${debian_chroot:+($debian_chroot)}\[\033[01;0m\]\W\$ '
fi
unset color_prompt force_color_prompt
   To shorten the prompt change '\u@\h:\w\$ ' to '\W$'. The escape character '\033' (or \e) opens the color code, multiple color codes are separated with ';',  and the color code command is closed with 'm'.  I set the color prompt purple by changing the "\033[01;32m" (in the commented out line) to "\033[01;35m".  IBM has a good summary of color prompts, with a color chart, although they don't give the code, so I posted an example below.  The linux documentation project has thorough documentation of bash configuration.
   Here is a simple script to show the basic 8 color and 8 background combinations available to most shells.   The result of this script is shown in the subsequent screenshot.
#!/bin/bash
#show combinations of 8 colors and 8 backgrounds available to most shells

echo -ne "    "
for c in {0..7} 
do 
    echo -ne "  \033[1;37m 4${c}  \033[0m"
done

echo -e "\n"
for b in {0..7} 
do 
    echo -ne "\033[1;37m  3${b} \033[0m"
    for c in {0..7} 
    do 
    echo -ne "\033[4${c}m \033[3${b}m norm " 
    done
    echo -ne "\033[0m\n\033[1;37m  3${b} "
   for c in {0..7}
    do
    echo -ne "\033[4${c}m \033[1;3${b}m bold " 
    done
    echo -e "\033[0m\n"
done
   I change the /root/.bashrc file to have a prompt with ROOT in red, folder in yellow, and red text.  It is obnoxious, and is meant make a root shell obviously different so I don't do anything dumb.  I do not change my /root/.emacs file so a root emacs frame is obvious, for the same reasons.  The screenshot shows a user emacs frame on the left, then on the right (descending) a root emacs frame, a root xterm, and a user xterm.
You can get really crazy with 256 colors and other features.  Run this script to test if your shell supoprts 256 colors, and see if you like the results.
#!/bin/bash
#show 256 colors
for c in {0..255} ; do
    echo -e "\e[38;05;${c}m ${c} bash colors" 
done

Sunday, August 26, 2012

Change xmonad modkey

Copy this example xmonad.hs to your /home/USER/.xmonad/xmonad.hs file. Note the .xmonad directory is hidden.  Xmonad defaults the alt key as the modkey, which conflicts with emacs key binding.  Fortunately the developers cover this right in the example file as shown in this excerpt:
-- modMask lets you specify which modkey you want to use. The default
-- is mod1Mask ("left alt").  You may also consider using mod3Mask
-- ("right alt"), which does not conflict with emacs keybindings. The
-- "windows key" is usually mod4Mask.
--
myModMask       = mod1Mask
So change that last line to
myModMask       = mod4Mask
and the windows key is now the xmonad mod key.  But because my keyboard doesn't have a right windows key, and I like symmetry I want the menu key (the key with the cursor and drop down menu icon) to also function as my mod key.  This can be done with the xmodmap command.  First run xev from the terminal, which will pop up a blank window.  While the blank window is the focus, keyboard events (presses and releases) will be captured and the information about them displayed in the terminal window that called xev.    
   For example, this is the result when I press the menu key:
KeyPress event, serial 27, synthetic NO, window 0x1200001,
    root 0x160, subw 0x0, time 29169837, (62,330), root:(741,331),
    state 0x0, keycode 135 (keysym 0xff67, Menu), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 27, synthetic NO, window 0x1200001,
    root 0x160, subw 0x0, time 29169893, (62,330), root:(741,331),
    state 0x0, keycode 135 (keysym 0xff67, Menu), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False
The important bit is the " (keysym 0xff67, Menu)", which is the keysim information.  Now, run xmodmap to check the existing modifier keys.
xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3      
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)
This shows that Menu is not a modifier.  I want to make the menu key function identical to the windows key (Super_L) such that either functions as mod key for xmonad.  Do this in the following /home/USER/.xmodmap file.
! Comments start with !
! translate Menu into Super_L keycodes.
keysym Menu = Super_L
Test with by running xmodmap .xmodmap command in the terminal, then pressing mod-q which 'restarts' xmonad but has no effect on the current display.  However, the menu key should function as the mod key now.  Add a call to xmodmap to .xinitrc to call this translation on startup.
if [ -f $HOME/.Xresources]; then
    xrdb -merge ~/.Xresources
fi

if [ -f $HOME/.xmodmap ]; then
    /usr/bin/xmodmap $HOME/.xmodmap
fi
xmonad
Then reboot and test it out.  Warning, I'm pretty sure this renders any bindings to the Menu keysym useless, but do not care since I never use it.  Your needs may differ.

Grant users shutdown and reboot privilege

This far in my Xmonad setup I've had to su as root to shutdown or reboot.  Use sudo to allow normal users to shutdown the system. Install the sudo package.
ROOTPROMPT$> apt-get install sudo

UPDATE: The /etc/sudoers file (at least since sudo 1.8.5) suggests adding local content to /etc/sudoers.d instead of altering the /etc/sudoers file with visudo.  I created the following power_conf file to have the same effect as the visudo method described later:
#/etc/sudoers.d/poweroff_conf
[USERNAME] [HOST] = NOPASSWD: /sbin/shutdown -h now, /sbin/poweroff, /sbin/reboot

The manual suggest 0440 permissions for all files in this directory.
ROOTPROMPT$> chmod 0440 /etc/sudoers.d/power_conf

Previous Method: The old way to grant normal users these privileges is to directly modify the /etc/sudoers file using visudo (installed with the sudo package).
#/etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=(ALL) ALL
[USERNAME] [HOSTNAME] = NOPASSWD: /sbin/shutdown -h now, /sbin/poweroff, /sbin/reboot
#
#includedir /etc/sudoers.d
Add the highlighted line to your /etc/sudoers file, substituting your username for [USERNAME] and your hostname for [HOSTNAME].  The 'NOPASSWD' command lets the user issue these commands without entering their password.  This command gives shutdown, poweroff, and reboot privileges to the user, but only for the commands issued exactly as entered in this file.  For example, to shutdown as user:

USERPROMPT$ sudo /sbin/shutdown -h now 

will work, but

USERPROMPT$ sudo /sbin/shutdown

will not.  I learned how to set this up from http://www.debian-administration.org/articles/33.  Be careful which privileges you grant to users.

Saturday, August 25, 2012

Customize emacs

I've switched to Xmonad as a window manager.  Previous entries covered the xmonad install and xterm configuration.  I'll now configure emacs with a black background and grey text, since green is already used by xterm and I like a soft text color but want my emacs windows easily differentiable from the xterm windows.  I turn off the menu bar, the tool bar, and the scroll bar since I limit my use of the mouse and these widgets just waste space.  I also turn off the emacs startup screen.  The /home/USER/.emacs file contains the user specific configurations.  Mine is pasted below.
(set-background-color "black")
(set-foreground-color "grey")
(setq inhibit-startup-message t)
(menu-bar-mode -1) 
(tool-bar-mode -1)
(scroll-bar-mode -1)
   Each of these commands - except for the startup message - can be tested in the emacs window by typing m-x then the command.  For example m-x set-foreground-color then return will prompt you for the new color setting.  The commands with true/false and on/off options will not prompt you but simply toggle between the settings.
   The screenshot below shows an emacs frame on the left (with two windows open), a chrome browser window on the top right and an xterm on the bottom right.  The top emacs window is the filesize tutorial using the boost filesystem, and shows the C/C++ syntax highlighting in this color scheme.  The bottom emacs window is my .emacs file and shows the Lisp syntax highlighting in this color scheme.
   If you ever need to access the emacs menu bar you can pop it up using ctrl-mouse3.  http://emacswiki.org/ and http://www.masteringemacs.org/ are my main sources for emacs knowledge.

Customize xterm

Edit the /home/USER/.Xresources to customize the xterminal.  I like green text on a black background, and the default font size is too small for me.  First, run the xfontsel from the terminal, and adjust the parameters to your liking and system availability. The image below shows my fontsel with fmly set to 'fixed',  wght set to 'medium', and pxlsz set to 14.  The english alphabet appears in the window if you have a font that matches your settings.  If the english alphabet is not visible (Chinese or some other alphabet appears) you need to adjust your settings until the english alphabet appears (assuming you want english).  The string between the parameters and the alphabet is used to set the xterm font.  Copy it.

In this case, I copy "-*-fixed-medium-*-*-*-14-*-*-*-*-*-*-*".  Now open or create the /home/USER/.xresources file.  Mine is pasted below, and sets green text - size medium font - on a black background.  My terminal saves the previous 512 lines in the history.
!Comments start with !

xterm*background: black
xterm*foreground: green
xterm*font: -*-fixed-medium-*-*-*-14-*-*-*-*-*-*-* 
xterm*saveLines:  512
See http://invisible-island.net/xterm/ for full xterm documentation.
   The following /home/USER/.xinitrc file checks for an xresources file, then runs xmonad.  This will find your xterm customizations once you run the startx command.
if [ -f $HOME/.xresources ]; then
    xrdb -merge $HOME/.xresources
fi

xmonad

Tuesday, August 21, 2012

Xmonad on a fresh debian install

   I've decided to try Xmonad as my window manager.  Xmonad is a tiling window manager that automatically arranges the application windows in the display such that the entire screen is used.  Unlike the more common stacking or compositing window managers, Xmonad does not allow overlap.  Since the entire screen is used, I don't need a desktop environment either.  The numerous default keyboard commands and the ease of customization can reduce the need to use the mouse.
   Rather than suffer through removing the desktop and related packages I will do a fresh install of the core debian system.  Grab the iso for your architecture from the debian website.  Proceed through the install as normal except when you get to the tasksel menu, de-select the "Desktop Environment" option, which is at the top of the list.  The taskel menu is shown in the figure below, and you want all options unchecked for a bare bones debian install.
   Reboot the machine after the install is complete.  You will login on a TTY window, then su to login as root, and install the following packages.  'ROOTPROMPT$' is just your TTY prompt.
ROOTPROMPT$> apt-get install xorg 
ROOTPROMPT$> apt-get install xmonad
ROOTPROMPT$> apt-get install alsa-base alsa-utils
ROOTPROMPT$> apt-get install chromium-browser
ROOTPROMPT$> apt-get install emacs
   Feel free to substitute your browser and text editor of choice in those last two commands.  Configure alsa with the following command, then exit root.
ROOTPROMPT$> alsactl init
ROOTPROMPT$> exit
Your system should now have sound. UPDATE: Running through my own install instructions for a new machine, I did not have sound after this step.  I fixed my low volume with snd_hda_intel module problem based on this snd_hda_intel howto.
   The following command sets xmonad to be called as the window manager when X is started.  If you have other window managers installed you will need to comment them out.  However, if you are following this tutorial that should not be the case.  Read more about the debian specifics of .xinitrc, or a general synopsis of .xinitrc.
$> echo "xmonad" >> /home/USER/.xinitrc
Now you can start X11, which will call xmonad as the window manager.
$> startx
You will get a blank screen, and need to press alt+shift+enter to start xterm (the default terminal emulator).  From xterm you can launch your browser and start reading through the xmonad default key commands.  This screenshot shows my simple desktop with a chromium window as the master (on the left) and a root terminal (top) and a user terminal (bottom) on the right.  Following only the preceding instructions will get you an xterm with black text on a white background.  Subsequent posts will cover xterm and bash customization, as well as some additional xmonad configuration.