Trix & Graphix

Two tips to create comments in programs edited thorough VIM

Here are a couple of simple tips to help you in the hard task of programming in VIM (the hard task is to write the code, not to do it in VIM, of course :) )

Inserting 50 symbols to create a header
Normaly it is a good idea to remark something within a program using comments. It's just something like


In general, writing a large numbers of symbols is not a hard task, but it can be even easier using VIM. You only have to type a character, and repeat the order many times. To do this, use the command
and press ESC, and you will get a hundred symbols "#" all together. Later just push "." to repeat the last command. You will have exactly 2 lines of 100 symbols having pressed only 7 keys. Oh yeah.

Commenting several lines of code with few commands
This tip is less silly than the former one, and it may save you a lot of time. Unfortunately I don't know illustrate it, so I'll try to explain in my own words. Imagine you want to comment 20 lines, inserting some character (for example "#", but it may be other depending on the language of your choice) in the first column of these lines. To do it in VIM, use the block selection tool by pressing "ctrl+v" in command mode (remember that "v" is to use the standard selection, whereas "V" is the line selection). Then, select the first character, and only the first one!, in every line you want to comment. In this moment, press "I" (capital "i", the lowercase does not work) and type the character that you want to insert in every selected line. At the moment you will only see the change in the first line, but after pressing ESC, all selected lines will be commented automatically.

I don't even know how I could live without this! :)

Converting eps to pdf

In most Unix-like systems there is a tool called epstopdf. It is very simple, but incredibly useful for preparing figures for a LaTeX document. Nevertheless there are some cases in which this tool is not present in your system by default. For example Snow Leopard, the SO of Mac, does not provide it. If I remember correctly, this tool is also missing in Ubuntu, and I guess that this can be the case in many other Linux distributions. There are other cases in which although the tool is present, it does not work properly, and you get unexpected results with the warning message:

==> Warning: BoundingBox not found!
In this case, the problem is because you are using a too old version of this program. 

I have had problems in the past looking for a newer version, since you get a lot of garbage when searching epstopdf in google. Hence, this post is a simple hint to help you getting the program. Hopefully someone in the future will reach this post through google and will save a lot of time :)

The solution is simple: just go to the CTAN page of this software. Actually it is just a perl script. Place it somewhere and add the location of the script to your PATH. That's all folks.

Improving the search in VIM

Here are a couple of tips to improve the searching engine in VIM.

First of all, searching patterns in VIM is quite simple. In command mode, use the

command. You can use regular expressions too. For example
searches for the text "foo" in the beginning of a line. You can use very complex regular expresions, whose explanation if far away from the scope of this blog.

The searching commands in VIM takes the focus of the cursor to the next match going downward within the text. If you want to go to the next match, use the command
takes you to the former match. The command
 makes the same than "/" but in the opposite direction, this is from the end of the file to the beginning.

Besides these commands, you can configure some of the behaviour of VIM when searchig patterns by adding some lines to the .vimrc file. For example
set incsearch
search in the text meanwhile you are typing, without waiting you to press enter. Another interesting feature is
set hlsearch
which highlight the found text. This is very usefull when debuggingg scripts, since you have a very convenient way to taking a look at the patters you want in a given moment.

Changing the colours in the output of "ls" command

During the last weeks I'm working quite often with Mac OSX. However I normally use a lot the terminal, but the default terminal in this OS is quite lame. Thus, I recommend to use the program iTerm. This terminal has some interesting features, which I don't want to discuss in detail. Take a look and decide yourself!

However there is an interesting feature which is not activated by default in this OS (and has nothing to do with the terminal program you use). It is the use of fancy colours according to the type of file when listing the content of a directory through the command ls. This is a very important feature, very useful to easily identify the kind of files you have into a directory. In fact, it has few to do with the fancy aspect, but with how comfortable you feel yourself working in a terminal.

To allow the use of colours you have to use the -G option. Nevertheless having to write this every time is a pain (you would type it a thousand times every day!). It's much easier to use an alias in the .bass_profile in your home

alias ls="ls -G"
Finally, in order to customize these coulours, you have to use the environment variable LSCOLOR. To do so, you only have to add also this line to the .bash_profile in your home:
export LSCOLORS="exfxcxdxcxegedabagacad"
But what in the hell means that line? Well, I could explain it in detail but... finally I would only sumarize the content of the man page of ls. So here I copy the explanations you may need, directly taken from this man page:

LSCOLORS The value of this variable describes what color to use for which attribute
when colors are enabled with CLICOLOR. This string is a concatenation of
pairs of the format fb, where f is the foreground color and b is the back-
ground color.

The color designators are as follows:

a black
b red
c green
d brown
e blue
f magenta
g cyan
h light grey
A bold black, usually shows up as dark grey
B bold red
C bold green
D bold brown, usually shows up as yellow
E bold blue
F bold magenta
G bold cyan
H bold light grey; looks like bright white
x default foreground or background

Note that the above are standard ANSI colors. The actual display may differ
depending on the color capabilities of the terminal in use.

The order of the attributes are as follows:

1. directory
2. symbolic link
3. socket
4. pipe
5. executable
6. block special
7. character special
8. executable with setuid bit set
9. executable with setgid bit set
10. directory writable to others, with sticky bit
11. directory writable to others, without sticky bit

The default is "exfxcxdxbxegedabagacad", i.e. blue foreground and default
background for regular directories, black foreground and red background for
setuid executables, etc.

Forcing vim to remember the cursor position

In some VIM installations this awesome editor does not remember the last cursor position in the former edition of a file. Of course, this can be a pain. Here is a small tip to solve this problem. You only need to copy this code into your .vimrc file

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")

\| exe "normal g'\"" | endif

Customizing the Unix prompt

This is a very fast post about the prompt (basically this is an auto-remainder for myself xD).

The appearance of the prompt in an Unix terminal is determined by the environment variable PS1. You can (you have to) modify it if you want to change the prompt. It is easy to take a look at the current value of variable: just type in an open terminal
echo $PS1
and here is it. The prompt that I'm currently using is this one:
PS1='$(tput setaf 1)\u@\h $(tput setaf 2)\w> $(tput setaf 0)'
There are a number of commands very useful to depict important information. In my example \h shows the name of the host, whereas \u is the user and \w is the current working path. The commands tput setaf change the color. Take a look to the man pages of tput and terminfo to learn more.

Of course there are many more options. Here there is a more complete reference in this respect. By the way, it is a good idea to modify this variable in the .bash_profile file in your home directory :)

Reading a file until it ends

It is a commont task that you have to read some information from a file to perform some calculations. This uses to be done inside a loop. The problem is that many times you don't know how many lines the file has, so you can't use a counter as the condition to stop reading. Or maybe you know, but you don't want to pass that information to the program every time you run it. In this post I'm showing how to "teach" a Fortran and Bash program to read until it reach the end of the file.

Fortran version
In fortran, you have to use the option iostat when you use the read function. By doing so, there is an integer variable (ierr in the example below) that contains the output code of the last read action. If it is different to 0, it means that the end of the files has been reached (or something even worse). I think the example is self explanatory

program readtotheend
implicit none
integer :: i, ierr, number
do while(ierr.eq.0)
print*, 'Element', i, number
print*, 'Reading finished', i-1, 'lines'
end program

Bash version
The Bash version of this idea is to use a while loop and feed it with the input file

set -ex
while read number; do
# Here you can do whatever you want to do with the variable, which is stored in the variable number
done < filein.asc

This is a really elegant solution to perform some operation to all the lines of a file.