shell

ASCII codes in Vim

Today, a work mate asked if anyone had an ASCII code table at hand. He was editing a file in Vim and wanted to know the ASCII code for a certain character. Here is the answer:

While having the cursor on the character, type:

<ESC>:ascii<ENTER>

Enjoy!

A quick link to IBM's introduction to text utilities on UNIX. They know the stuff.
03.17.12 | pfortuny | # | (0)

shell

Colored diff

As a programmer I am used to dealing with diff files. If you do not know what a diff file is, here you can find a nice description.
They are simply text files so they can be viewed with less, more, cat and other common utilities. The main problem is that those utilities do not recognize these files’ special syntax.

cdiff (/usr/ports/textproc/cdiff) is a small utility that shows diff files in a colored fashion. It can be used this way:

$ cdiff file.diff

Of course, Vim can be useful here too ;)

svn diff | vim -R -

Enjoy!

shell

Greping from inside Vim

Hi there,
First things first: Happy New Year! :)
One task I usually perform when I am writing code is grepping the code for a certain string. In order to do that, I used to exit Vim (either with :q or suspending the process), grep the files and then go back to my Vim session. This approximation has one main drawback: I usually don’t remember all the matches reported by grep.
Vim provides a mechanism for invoking grep from a Vim session. The command is:

:grep string files

This way, one can invoke his local grep command and the results will be integrated into the error list, the one we wrote about in compilation errors. This way, one can move through the matching list using both :cp and :cn
We can instruct Vim to use another grep program by setting the grepprg variable.

shell

Unicode sorting in perl

Just Normalize. Assume file.txt is a list of unicode words, then

cat file.txt | perl -e 'use Unicode::Normalize; my @w ;\
while (<>) {chomp; push @w, $_;} ; @w = sort {NFD($a) cmp NFD($b) } @w ;\
print(join("\n", @w))'

will output the sorted list (well, sorted according to the NFD normalization, which for Spanish is enough).

shell

Creating shell archives

shar(1) is a FreeBSD base system utility for creating shell archives that is, a kind of self-extracting archive of a hierarchy of files and directories. The archive will recreate the file hierarchy specified at creation time. It is fast and the result is easy to handle as the shell archive is a text file (good to email a file hierarchy).

To create a shell archive:

$ shar `find my_directory` > my_hierarchy.shar

To recreate the hierarchy:

$ sh my_hierarchy.shar

That is, shar is a simpler version of ar and tar.

Enjoy!

shell

How to know if a CPU is 32 or 64-bit

Nowadays almost all the computers have a 64-bit CPU, but sometimes we are logged in on a remote server and do not know what kind of CPU it has and we need to know it to install a package or… out of mere curiosity.

In those cases we can run the following command:

$ grep --color lm /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts
acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up ida nonstop_tsc arat pni
ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm 

If the lm flag is present (i.e. if there is any output to the grep command), then the CPU is 64-bit. lm means long mode (64 bit extension).

network

Resume rsync file transfers

I did not know that rsync had the resume capability till last week when I had to transfer almost 200GB between two servers with no good connection. I think some context is needed here: My company has two servers in two different cities where the backups are stored. There is a daily syncronization between the backup tree between the servers. That is, a cron task that calls a script that basically runs rsync.

Due to the bad connection, I have had to add the -P option to the rsync command so that the command run by the cron task is:

$ rsync -rPtz -e ssh /local_backup_path/ remote_host:/remote_backup_path/

The -e ssh tells rsync to use a ssh client instead of rsh. The -z option compresses the file. -t preserves time attributes and the -P option resumes incomplete file transfers.

If you run this command from the command line you can use the --progress option, that will show you a progress bar during transfer. Very useful with large files.

I hope this saves you some time ;)

shell

Audio settings on FreeBSD

mixer(8) is a FreeBSD base system utility for setting and displaying sound card mixer levels. It resembles the Linux’s command line utility alsamixer. Invoked with no parameters it shows the current values of the mixer devices:

$ mixer
Mixer vol      is currently set to  85:85
Mixer pcm      is currently set to  75:75
Mixer speaker  is currently set to  75:75
Mixer mic      is currently set to   0:0
Mixer mix      is currently set to   0:0
Mixer rec      is currently set to  75:75
Mixer igain    is currently set to  75:75
Mixer monitor  is currently set to  75:75
Recording source: mic

Setting specific values is easy enough:

$ mixer rec 80:80
Setting the mixer rec from 75:75 to 80:80.

Enjoy!

$ chmod -R u+rX
Changes recursively the write permissions only in the directories. It has been very useful to me lately and I have to admit I did not know this till some weeks ago.
09.08.11 | rafacas | # | (1)