Emacs — ispell — No word lists (Ubuntu)

November 16, 2008 by Rares Vernica

In Ubuntu, if you get:

Error: No word lists can be found for the language “en_US”.

when trying to start ispell (e.g., M-x ispell-buffer) from Emacs, then you need to install the aspell-en package:

sudo apt-get install aspell-en

Emacs — Using rectangles

November 12, 2008 by Rares Vernica

In Emacs, you can not only select by lines, but also by columns. If you select by columns, the selection is called a rectangle. You can use rectangles with the following shortcuts:

  • C-@ or C-Space — start selection (as usual)
  • M-x delete-rectangle or C-x r d — delete the rectangle delimited by the start selection and the current cursor position
  • M-x kill-rectangle or C-x r k — then you can use — M-x yank-rectangle or C-x r y
  • M-x clear-rectangle or C-x r c — replace the contents of the rectangle with spaces
  • M-x open-rectangle or C-x r o — insert spaces in the area and push the original contents to the right

C/C++ — Insert sequential values into container

November 7, 2008 by Rares Vernica

To insert sequential values into a container, you could use the iota function available in ext/numeric header. For example:

#include <ext/numeric>
#include <vector>

std::vector<int> v(5);
__gnu_cxx::iota(v.begin(), v.end(), 0);

v will contain {0, 1, 2, 3, 4}.

For more information see sgi documentation

Python — Convert string to HTML/XML entities

October 18, 2008 by Rares Vernica

In order to convert a string to HTML/XML entities you can use:

”.join(['&#%d;' % ord(ch) for ch in text])

For example if text=’mailto’ the output would be:

‘& #109;& #97;& #105;& #108;& #116;& #111;’

This might be useful in hiding email addresses in web pages.

Unix/Linux — Swap Ctrl and Caps Lock in X11

April 26, 2008 by Rares Vernica

In order to swap the Ctrl and Caps Lock keys in X11, you can add the following line in Section “InputDevice” of file /etc/X11/xorg.conf:

Option “XkbOptions” “ctrl:swapcaps”

If you woud like to keep the Ctrl where it is and make Caps Lock act as a Ctrl also, you can add:

Option “XkbOptions” “ctrl:nocaps”

Unix/Linux — generate large file

April 11, 2008 by Rares Vernica

In order to generate a large file of a set size you can use:

dd if=/dev/zero of=test100M.bin bs=100000000 count=1

where of is the output file and bs is its size in bytes.

C/C++ — Friends and Templates

November 3, 2007 by Rares Vernica

In C++, if you declare a template class and you would like to have a friend function that has an argument of the current class type, you need to be careful when specifying the name of the template types. For example:

template <class T>
class A {

template <class T>
friend void b(A<T> a);
} ;

Will confuse the compiler, as you use the same template name, T, for the class template and the function template type. What you need to do, it to use a different name for the function template type:

template <class T>
class A {

template <class V>
friend void b(A<V> a);
} ;

Notice that I used T for the class template type and V for the function template type.

Unix/Linux — Redirect output with sudo

September 26, 2007 by Rares Vernica

Sometimes you might need to redirect the output with sudo privileges. For example:

echo myhost.com > /etc/hostname

will give you the following error:

-bash: /etc/hostname: Permission denied

Unfortunately, using:

sudo echo myhost.com > /etc/hostname

will give the same error.

The solution is to start bash as sudo and then give the entire command to bash as input:

sudo bash -c “echo myhost.com > /etc/hostname”

C++ — Convert string to upper/lower case

August 4, 2007 by Rares Vernica

To convert an std::string to upper case you can use the following:

#include <algorithm>
#include <string>

std::string data = “Abc”;
std::transform(data.begin(), data.end(),
data.begin(), ::toupper);

For converting to lower case, you need to replace ::toupper with ::tolower.

JavaScript — Trim white space

July 23, 2007 by Rares Vernica

Here is a way to trim the white space from a string in Javascript:

replace(/^\s+|\s+$/g, ”)

By removing |\s+$ it will do a left trim only, and by removing ^\s+| it will do a right trim only (g is no longer necessary).