Unix Tip: Make ‘less’ more friendly

February 11, 2011

You probably know about less: it is a standard tool that allows scrolling up and down in documents that do not fit on a single screen. Less has a very handy feature, which can be turned on by invoking it with the -i flag. This causes less to ignore case when searching. For example, ‘udf’ will find ‘udf’, ‘UDF’, ‘UdF’, and any other combination of upper-case and lower-case. If you’re used to searching in a web browser, this is probably what you want. But less is even more clever than that. If your search pattern contains upper-case letters, the ignore-case feature will be disabled. So if you’re looking for ‘QXml’, you will not be bothered by matches for the lower-case ‘qxml’. (This is equivalent to ignorecase + smartcase in vim.)

So how do we take this useful feature and make it permanent, so that we don’t have to remember to type less -i every time? We could create an alias less='less -i'. But there are tools (such as git-log) that invoke less on their own, and they will not know about the ignore-case option. It would be better if we could tell less that we always want that feature on, regardless of startup flags. This article will teach you how to do that.

Read the rest of this entry »


Today’s Random Firefox Trick…

May 22, 2009

… you can select non-contiguous regions of text by holding Ctrl while selecting with the mouse! (If you have a table, it will select a rectangle instead.)


Two OpenOffice.org Writer Tricks

February 16, 2009

Trick #1: Stop AutoText from switching to Times New Roman

I often edit files in fonts other than Times New Roman, just because that font is so ubiquitous. And I sometimes use AutoText to insert frequently used phrases, or Romanian words in an English text (so that I don’t have to switch languages constantly). One thing that has always annoyed me is that after invoking an AutoText completion (F3), the next characters will always be in Times New Roman (even if the AutoText itself is in another font).

As it turns out, this happens because Times New Roman is the font of the Default paragraph style. So hit F11 and modify your style to use the desired font. In fact (and this is a topic for another day), styles should be used in most cases and formatting a selection of text (bold, italic, font, font size, paragraph indentation) should rarely be done by hand. This is very unintuitive to people who have been using text editors before the GUI era.

Trick #2: Insert Formula without switching into formula-edit mode

Read the rest of this entry »


pv: Progress Bar for md5sum et al.

February 4, 2009

Tools that know they will take a long time often come with a built-in progress indicator, but there are other utilities on Linux that often leave the user frustratedly tapping their fingers, wondering how much longer they will have to wait.

Luckily, there is a nifty little tool called pv that will donate a progress bar to any program that can read from standard input or a pipe. pv probably stands for pipe viewer.

1. Simple example: figure out how long an md5sum will take:

pv eternal.avi |md5sum

will display something like

96.5MB 0:00:05 [25.3MB/s] [=======>                                    ]  9% ETA 0:00:48

Read the rest of this entry »


Rescaling a PDF File

February 17, 2008

I had a PDF file which I wanted to print with multiple pages per sheet. But the margins were so big that a lot of paper would have gone to waste.

I couldn’t just select the text, copy it and format it to my liking. The file is not scanned, so it’s still a mystery to me why the extracted text turns out garbled.

The solution I found was the pdfrescale script (via here). It uses some kind of latex magic to scale the entire PDF by whatever factor you want. All the needed dependencies should by installed by

sudo aptitude install pdftk pdfjam

If you know of an easier solution, I’d be interested to hear about it!

pdfrescaled.png


Die, DOS, die!

November 21, 2007

I hatedislike programming on the school computers using Windows, the DJGPP compiler and no decent terminal. One of the most annoying problems is that you can only see the last ~25 lines of your program’s output. gdb doesn’t catch assertion failures for some reason, so you’re left dealing with your bugs with bare hands… talk about dirty work!

This reminded me of the old “DOS” way of waiting for a key press every now and then. Here’s one of my latest quick-and-dirty-and-not-properly-tested tricks:

#define assert(a) if(!(a)) { printf(“MyAssert(%s) failed\n”, #a); \
freopen(“con“, “r”, stdin); \
getchar(); \
abort(); }

If stdin is freopen‘d to some file, the getchar() will not accomplish its job of waiting for a keyboard stroke. That’s why the freopen is there. (Remember how you could crash Win9x by running ‘c:\con\con‘?)

So now, instead of crashing with a useless stack trace and no clue to which assert failed in only 25 lines, it gracefully shows:

MyAssert(the::world.isFlat()) failed

waits for you to read the message and press Enter, and then aborts. Problem solved 8-)


The Magic SysRq Key

October 24, 2007

[ignore]

One day I’ll stop apologizing for ignoring this blog for so long…

I’ve upgraded to Gutsy and it flies! I like Dolphin, the new file manager, and I’m sure it will get even better for KDE4.

There is one annoying glitch however. After waking up from STD (hibernate), all USB devices are dead. This has been fixed already in the latest kernel, and I’m just waiting for it to finally reach *Ubuntu repositories. Reading about the bug, I came across an interesting peculiarity of the Linux kernel. Sometimes I’m surprised at how much I don’t know ;)

[/ignore]

OK, here’s the deal. You can use the SysRq key to send requests directly to the Linux kernel. Why would you want to do that? Well, for starters, Linux may crash. WHAAAAT? Well, it may. Seldom, but it may. Especially if you mess around where you’re not supposed to :P

What can you ask the kernel to do using the SysRq key? Many things. One I find particularly useful is the Alt+SysRq+f. It kills the process which is using the most memory on your system. So when your system thrashes to death because of a runaway process, you don’t need to switch to the console (which may take ages) and use kill anymore. Just use the shortcut.

A few notes:

  • It’s Alt+SysRq and not just SysRq because the SysRq key shares a button with Print Screen, and without the Alt it would be interpreted as such.
  • IT DOESN’T WORK!! It may not work because you have a certain kernel option disabled. There’s not much you can do in that case, short of recompiling the kernel…
  • Be careful what you do. This is a gate directly into the kernel, and it allows you to do things like rebooting immediately without syncing disks, which could seriously screw up your file system.