Archive for the 'howto' Category

Linux: Quick and Dirty Way to Take Screenshots

Wednesday, September 26th, 2007

Random linux tip - if you don’t feel like dealing with the regular KDE or Gnome based applications or mechanisms for taking snapshots of your screen, there is a simpler and quicker way. You can do it directly from the console:

import -window root screenshot.png

Of course screenshot.png is the filename you want to assign to your screenshot. It’s quick and easy, and can be triggered at any time. You can even put it into a script, or run a cron job that will take a snapshot of your current workspace at certain time interval.

The command above takes a snapshot of the whole desktop. If you want to just grab a rectangular region, skip the window parameter:

import screenshot.png

You cursor should change into a cross, and allow you to draw a rectangle over the desired area and save it as the file name you specify.

Note that you will need to have the imagemagick package installed. On ubuntu do:

aptitude install imagemagick

Make sure you have universe enabled in your apt sources.

Latex: Squeezing the Vertical White Space

Wednesday, September 19th, 2007

Here are some tips on how to “compress” your paper vertically by minimizing white space gaps between elements. I had to do this few days ago when school refused to duplicate my syllabus because it was 14 pages long. I got it down to 6 without any cuts, and then down to 3 after doing some reductions in text.

So how do we squeeze the vertical whitespace? There are many ways to do this, and some are more complicated than the other. My tips range on the easy side, so you won’t need to write custom .sty files or redefine commands using intricate tex sequences.

First we want to set the spacing between paragraphs as small as possible. The commands below should kill just about any space inducing setting in your paper unless you are doing something fancy:

\setlength{\parskip}{0pt}
\setlength{\parsep}{0pt}
\setlength{\headsep}{0pt}
\setlength{\topskip}{0pt}
\setlength{\topmargin}{0pt}
\setlength{\topsep}{0pt}
\setlength{\partopsep}{0pt}

Now the paragraphs are snugly against each other so let’s take a look at line spacing. The \linespread command is usually used to increase the line spacing but we can exploit it to make it smaller by passing in a value smaller than 1:

\linespread{0.5}

You may want to play around with that value - if you set it to small, LaTex will just reject it. Below certain threshold some lines may start running into each other. For me 0.5 value did the trick, and slurped up swaths of white space.

Our next offender are section headings. By default they have huge gaps above and below them. Totally wasteful, especially if you are trying to save trees by decreasing your page count without sacrificing content. So what do you do? There are very complex ways to change the spacing above and below the section headings but we are lazy bums and don’t feel like using them. So let’s use the titlesec package and zero out all the spaces:

\usepackage[compact]{titlesec}
\titlespacing{\section}{0pt}{*0}{*0}
\titlespacing{\subsection}{0pt}{*0}{*0}
\titlespacing{\subsubsection}{0pt}{*0}{*0}

If you look int the documentation, the attributes to \titlespacing are command, left margin, above-skip and below-kip respectively. The * notation replaces the formal notation using plus/minus and etc. If you set it to zero, headings will snug up to the paragraphs above and below them.

Enumerations and itemizations are horrible space wasters too. By default, all the lists are double-spaced. Why? Don’t ask me, but it’s easy to get rid of that by using “compacted” lists provided by the mdwlist package. In your preamble add:

\usepackage{mdwlist}

Then instead of using normal lists use:

\begin{enumerate*}
	\item
\end{enumerate*}
 
\begin{itemize*}
	\item
\end{itemize*}

This is not as straightforward as the other steps, as it will require some search and replace in a pre-existing text. If you know a better way to do this, please let me know.

Last thing I did was to change my margins using the geometry package. My command looked like this:

\usepackage[left=2cm,top=1cm,right=2cm,nohead,nofoot]{geometry}

My document had no headers or footers so I was able to disable them. You should probably experiment a bit with the values above to see what is the maximum range of your printer. Most devices won’t print all the way to the paper edge so you must set margins appropriate to your printer.

My document had no formulas or figures, but had several long item lists. Some of them were very narrow - 2-3 words per item. These types of lists are major space wasters so I set them in multi-column mode. Depending on your list you can either use 2, 4 or either 4 columns. For me 2 columns were the right fit. I recommend using the multicol package. In your preamble add:

\usepackage{multicol}

Then surround your text to be “columnized” (shut up, it’s a new word I just mad up) using:

\begin{multicols}{2}
	% your stuff goes here
\end{multicols}

Once I did all of that, the page count of my document was cut roughly in half. Feel free to add your vertical space squeezing tips in the comments.

Resurecting Compaq Presario 1240 with Damn Small Linux

Thursday, September 6th, 2007

In December I got two old, broken laptops from the relatives. The older, crappier one of them was a 200 MHz Compaq Presario 1240.

Compaq Presario 1240

The machine had only 98 MB of RAM, and a crappy on-board video card. In fact, it was all kinds of funky. For example, check out the weird Targus power adapter that came with it. I have no clue if this was the original AC adapter that shipped with the laptop, or if this was bought at a later time as a replacement. I know one thing though - this thing is so big it basically takes over every wall plug or power strip you plug it into for itself:

Compaq Presario 1240 Power Supply

When I got it it had Windows XP installed, and essentially it took about an hour to boot. I tried installing Windows 2000 on it but it was still sluggish. The system was barely crawling even after I set it up to use bblean as the default shell.

The only way to actually make it usable was to either install Windows 98 (not a good idea) or some minimalistic Linux account. The machine was ancient so I basically limited myself to the distros that claim to be fast, lean and optimized for old hardware:

  1. Damn Small Linux - the 50MB live distro that can be installed on the HD
  2. Deli Linux - specifically built to run on antique hardware
  3. Puppy Linux - another minimalistic distro recommended by K. Mandla
  4. Arch Linux - also recommended, but bit more hefty in size than the other 3 pocket distros.

After doing some digging, I noticed that Deli ships without the ndiswrapper package which I needed to make my WPC54G v1.2 working. This is the same card I use on my Ubuntu rig, so I knew it would work as long as I had something that would wrap around the windows driver. So Deli, while very promising was out the window.

Since I wasn’t sure if my hardware will handle Arch will handle arch, I was torn between Puppy and DSL. I picked the later, because I had more experience with it and I could verify that the 4.0rc3 version definitely had a recent release of ndiswrapper on it.

Booting DSL posed some problems on the hardware. DSL ships with two tiny X server modes: Xvesa and Xfbdev. The default one is Xvesa which gives you access to the higher resolution modes. I set it into 800×600 (which is the lowest possible resolution in Xvesa) but no matter how many times I tried, I could not get it working. The desktop would show up, but the colors were messed up, and the image was distorted and pixelated. Ramping down color depth and resolution did not help - below 16 bit certain threshold I just couldn’t tell if it the color distortions were caused by my settings or if I still was having problems.

Xfbdev did work, and booted into a usable desktop - or rather one that you could actually look at without getting seizures or eye strain. But the resolution was still somewhat wrong - the whole bottom of the screen was cut off making the taskbar and the pager invisible. When I killed X I noticed that this was also the case for the TTY - I could not see what I was typing most of the time. The only way to get DSL to conform to my display geomentry was to run DSL in frame-buffer mode with the cheatcode fb800×600, and then pick Xfbdev as the X server. It doesn’t look very pretty, but it works.

Is this an issue with my video hardware, or is this a DSL thing? I’m guessing it’s the combination of the two. Neither Xvesa nor Xfbdev really talk to the video hardware. Xvesa uses standard BIOS VGA video modes and Xfbdev draws directly in the frame buffer. So I’m guessing that whatever Xvesa is doing is not directly supported in the POS video card that is installed on this machine. Perhaps a full X server that actually attempts to talk to the hardware would fare better here?

The installation to hard drive went smoothly. It was actually very fast, since the whole distro is only 50 MB. You literally run it, go make yourself a cup of coffee and before you come back it is long done, and asking to confirm a reboot.

Before I started I partitioned the drive with cfdisk creating a 128 swap partition as /dev/hda1 (type 82 in cfdisk) and made the rest into a type 83 (ext2) partition /dev/hda2.

Then I created swap partition on /dev/hda1:

mkswap /dev/hda1
swapon /dev/hda1

Finally, I performed the actual install using dsl-hdinstall command, and specifying /dev/hda2 as my installation drive. I choose Grub to be my bootloader and rebooted.

Note that the default entry in Grub is the Xvesa, non frame-buffered mode. So when you boot, you need to make sure you choose the fb800×600 option. The boot was pretty seamless and relatively fast. My next task was setting up wireless.

I went and downloaded the windows drivers from the linksys ftp site. I think I wrote about this before - thanks to Linksys’ brilliant versioning WPC54G v1.2 is really v1.3. Go figure. I downloaded the zip file, and transferred the lsbcmnds.inf and bcmwl5.sys files to the laptop on a USB stick.

Here is the little caveat - watch closely what I did. I copied both files to my home drive:

# ndiswrapper -i lsbcmnds.inf
Installing lsbcmnds
# ndiswrapper -l
lsbcmnds   Invalid Driver!

Apparently you need the absolute path here:

# ndiswrapper -i /home/dsl/lsbcmnds.inf

I don’t remember having to do this on Ubuntu, but go figure. Then I of course loaded the ndiswrapper driver:

ndiswrapper -m
modprobe ndiswrapper
echo ndiswrapper >> /etc/modules

Once I did that I ran iwconfig from the GUI menu. Strangely enough you can’t use ifup to bring up your wifi - for some reason it does not recognize wlan0 as a valid interface, even though it does show up when you do ifconfig. The GUI tool works though. If you specify wlan0 as your interface, and input your channel, ssid and WEP key and hit ok, all should be well.

So now I have a working DSL install. The resolution is low, and everything looks bit ugly but it is quite speedy compared to the sluggishness of Win2k. The base install with just the window manager and a single instance of rxvt running takes only around 12MB of RAM (no swap) and less than 2% of CPU time to run which is quite impressive. Of course Fifrefox will happily eat up all your remaining RAM as soon as you touch it, so it’s usually better to run the incredibly speedy Dillo to browse the web. To bad that some pages look like crap in it.

My only issue with DSL is that it is not really intended to be used this way. DSL was designed primarily to be a quick and easy live distro and you feel it at each step. Every time you reboot it, forgets certain things. For example to re-set my wifi with iwconfig every time. Other settings mysteriously reset themselves as well. Perhaps some tweaking will resolve this but it definitely does not work 100% directly “out of the box”. But hey, I guess I can’t complain. It work - and much faster than windows at that. It will probably be hard to beat the memory footprint and installed size of this distro - but the video performance could be better.

K. Mandla highly recommended Arch so I decided to try it next. Unfortunately, that did not work. Halfway through the initial boot off the CD I got a kernel panic, and a (un)helpful message that my CPU is to old. I guess Arch and Presario were never meant to be.

So my next steps are: Slax or Slax Popcorn (shipping with XFce instead of KDE). If that fails, I’ll get the smallest debian install I can find and then just build it from the ground up till I have a working system. Either that, or maybe I’ll finally try Gentoo for that “optimized for this particular machine” touch.

Use the 40 Most Common Unix Utilities under Windows

Monday, August 6th, 2007

Do you often find yourself missing common Unix utilities such as grep, awk, sed or touch when working under windows? Do you keep typing ls when you really meant dir? If you do, you are in the same boat as me.

There are many solutions for getting Unix like functionality under windows. The most popular and robust one is installing Cygwin which is a self contained, fully compliant POSIX environment that can be run on top of windows. If you are planning to do anything interesting you should definitely get it. But it might be overkill just for the basic functionality.

If you just wand to call few crucial missing from the default cmd shell, you need the Berkeley Utilities Package.

Berkeley Package

The linked page offers you a zip containing the 40 most commonly used Unix tools such ported to DOS. Yes, you didn’t misread that. These are all DOS ports - which means that running them is a little bit like running the good old blue EDIT text editor. Your cmd console might blink, and resize for a second, and then go back to normal, and your prompt will reset to use the DOS 8 character filenames in your path.

Odd DOS lieke behavior

But I think this is a smart price to pay for the power you gain by having tools such as grep, sed, awk, paste and etc at your fingertips. All these utilities work natively without the need to install some sort of runtime environment such as Cygwin. Just drop them somewhere in your Path and you are done.

Remapping the Caps Lock Key

Thursday, August 2nd, 2007

Here is a question: when was the last time you used the Caps Lock key on purpose? When was the last time you went “hey, I wish I could type in ALL CAPS ALL THE TIME”! Chances are that unless you are a Nigerian prince in a dire need of a money transfer, your use of Caps Lock is very limited.

In fact, I’m willing to bet that you hit that key by accident much more often, than on purpose. So why not get rid of it. And I don’t mean physically remove it - that’s just stupid. I mean, remap it to something more useful.

No More Caps Lock

Since I’m a vim user, I decided to remap my Caps Lock to the Escape key. This way I can have an easily accessible Esc under my pinky. This way switching back to normal mode is just single movement. I don’t have to lift my left hand from the home row to hit the actual Esc, and I don’t have to do a two hand motion to use the Ctrl+[ equivalent. Here is how you do it.

First open up your ~/.Xmodmap file and add the following lines:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape

Then activate your changes by doing:

xmodmap ~/.Xmodmap

If you want to map Caps Lock to a different key, simply replace Ecape in the sample above with something else like:

  • Control_L or Control_R for either left or right Ctrl key
  • Alt_L or Alt_R for Alt or AltGr keys
  • Super_L or Super_R for the respective “Windows Logo” keys
  • Shift_L or Shift_R for respective shift keys

Assigning it to AltGr is also an attractive idea for me, since the Caps Lock position is a bit more accessible than the usual AltGr which I use for typing Polish accented letters. I don’t really see what would be the point of mapping it to Shift, but hey - maybe someone will find that useful.

You can find the rest of key mappings by using the xev utility. Just run it, and start pressing letters and you will see key codes and names for your keys. You can use the same technique to remap almost every single key on the keyboard to whatever you like. mrgreen