There is no obvious way to specify that a program should run for no longer than x seconds. The following stackoverflow question solves it in a very perlish-way. Define a helper function doalarm:
$ doalarm () { perl -e 'alarm shift; exec @ARGV' "$@"; } # define a helper function
And you are done.
$ doalarm 300 ./my_prog
will run for at most 300 seconds.
aspell is a command-line spell checker. It is a replacement for the older ispell. It can be used to manage dictionaries, to check a complete file, or words typed in your terminal, among other uses.
We can invoke aspell this way:
$ aspell -a --lang=en
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.3)
This leaves us with a prompt in which we can type a word. For instance, if we type:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.3)
hello
*
The asterisk indicates that aspell found the word in the dictionary. On the contrary, if we type:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.3)
rescoe
& rescoe 15 0: Roscoe, rescue, rescuer, Rosco, resow, resole, rescale, rescued, rescues, fresco, recce, Reece, rescan, Roscoe's, Rosco's
We are presented with a list of options.
One of the nice features of aspell is that it can check a complete file. This way, it interactively asks the user to take an action for every misspelled word. For example:
$ cat test.txt
Simple text to check
the use of aspell from inside Vim
The following werd was misspelled
If we ran:
$ aspell --lang=en -c test.txt
we are presented with the following screen:
Simple text to check
the use of aspell from inside Vim
The following werd was misspelled
1) Aspell 6) Aspell's
2) asp ell 7) Ascella
3) asp-ell 8) spill
4) Ispell 9) Ispell's
5) spell
i) Ignore I) Ignore all
r) Replace R) Replace all
a) Add l) Add Lower
b) Abort x) Exit
?
This is very interesting as we can edit a file and check the spelling, but it would be nicer if we could spell check our text directly from our favorite editor…
You saw this coming, didn’t you? :)
Spell checking from inside Vim
While editing the same test.txt file with Vim, we can type the following:
:!aspell --lang=en -c %
This opens the aspell prompt which enables us to correct the misspelled words. Once we are done, Vim notifies us of the modification of the file:
W11: Warning: File "test.txt" has changed since editing started
See ":help W11" for more info.
[O]K, (L)oad File:
Typing L returns to Vim with the corrected file.
If we want to check a single word, we can type the following:
:!echo <cword> \| aspell pipe --lang=en<CR>
<cword> expands to the word under the cursor. Then we pipe this word to aspell for spell checking. This is somehow long to write, so it could be a good idea to define a macro:
:map <F4> <Esc>:!echo <cword> \| aspell pipe --lang=en<CR>
Enjoy!
I found myself needing to write a clean: rule in a makefile which would wipe out all the auxiliary files generated by latex (among other things). Moreover, the project has some subdirectories in which there might be more of those files.
Apart from finding files by date, size, modification time… the find utility can use regular expressions. I am not delving a lot into this (what kind of expressions, etc.), I just wanted to point out two things:
- That it comes in quite handy.
- That the regex must match the complete file name as reported by
find.
The second item is worth noticing. If you need to find, say all the files in this directory structure having an x, you cannot use
$ find . -regex 'x'
which, if it were a perl regex would match anything containing an x (assuming the quotation marks stand for the regex delimiter). What you need is to match the whole string, taking into account that any find in . starts with './'. Hence, find the files in this directory structure ‘having an x in its name’ is written
$ find . -regex './.*x.*'
which means exactly: find all the files matching ‘start with dot-slash, then anything, an x and anything’, where anything can be of length 0.
The clean: in my makefile reads now:
clean:
find -E . -regex "\./.*.(aux|log|blg|bbl|~)" -exec rm -f '{}' \;
The -E option means ‘use modern regular expressions’, which are the ones everybody uses nowadays. I prefer using ‘\.‘ for “real dots,” it looks cleaner to me.
It has been quite a while, has it not?
I have seen people many times doing something like this:
$cat binary_file
The problem with the above is that usually you end up with a prompt that looks like this:
�g`�g���i`�i��k`�kX@l`(l �
Whatever you type, you get garbage in your terminal. In this case, what most people do is to despair and close the terminal, losing the information contained in it.
reset is a command that gets the terminal back to its normal state. Sometimes everything is so mangled that there is no echo from the terminal. Don’t panic, just type reset and press ENTER.
If ENTER does not work, try to replace it by Ctrl-J
If none of the above works… you can fallback to your initial idea and close the terminal.
Enjoy!
Yes, it looks like a true spam page. But it is the only way to describe my problem. I was trying to set up mutt (yes, I am a bit fed up with Mail.app and its inability to be controlled with the keyboard alone in any sensible way). Terminal.app seems to work OK with utf-8. However, the included vim does not. Any time you type an accented char and delete it, the text gets mangled (you know, typical off-by-one cursor position). The solution for vim is incredibly stupid: open up vim and write :set encoding=utf-8 (or do that at any time while in command mode ESC).
For mutt, things are more or less according to the manual, but you must know where to look for. I ended up with the following two lines in my .muttrc:
set &charset ?charset
set charset="utf-8"
but I am uncertain whether the first line of the two is necessary.
YMMV, but that works for me.
I found a nice script in stackoverflow about how to color the output of make in bash. It works by filtering the output and looking for certain expressions to highlight.
Placing this function definition in your .bashrc file defines a bash make function which gets executed instead of the command. This way we can shadow the real make command and create our own custom version.
make()
{
ccred=$(echo -e "33[1;31m")
ccyellow=$(echo -e "33[1;33m")
ccend=$(echo -e "33[0m")
/usr/bin/make "$@" 2>&1 | sed -e "s/[Ee]rror:/$ccred&$ccend/g" -e "s/[Ww]arning:/$ccyellow&$ccend/g"
return ${PIPESTATUS[0]}
}
Of course, you can use this technique to wrap other commands as well.
Enjoy!
Sometimes I need to know all the alive hosts’ IP addresses in the LAN I am connected. The following command will show that information:
$ nmap -n -sP 192.168.10.0/24
Starting Nmap 5.21 ( http://nmap.org ) at 2012-08-05 11:24 BST
Nmap scan report for 192.168.10.1
Host is up (0.00086s latency).
Nmap scan report for 192.168.10.40
Host is up (0.00057s latency).
Nmap scan report for 192.168.10.50
Host is up (0.00056s latency).
Nmap scan report for 192.168.10.100
Host is up (0.00051s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 13.44 seconds
The option -sP tells nmap to do a ping scan, that is, to go no further than determining if host is online.
The option -n will never do DNS resolution. If the hostname is needed then do not use the -n option or use instead the option -R (always resolve).
Imagine we have a simple program like the one below:
#include <stdio.h>
int main(int argc, char **argv)
{
char *str = "Hello guys, this is just a bit large string to test a nice Vim macro";
printf("%s\n", str);
return (0);
}
This program is pretty simple, but it will serve its purpose. Imagine we have to inspect the content of str or for the same matter, any other memory address. We want to do it while we are inside a gdb session or while we are analyzing a core file. The simplest case is to do something like the following:
(gdb) p str
$1 = 0x400642 "Hello guys, this is just a bit large string to test a nice Vim macro"
But sometimes, we have to inspect a raw memory address, so we have to tell gdb to print just bytes. We can do this with the following command (the actual output may differ depending on the version of gdb you use):
x/100c str
0x400642 <.rodata>: 72 'H' 101 'e' 108 'l' 108 'l' 111 'o' 32 ' ' 103 'g' 117 'u'
0x40064a <.rodata+8>: 121 'y' 115 's' 44 ',' 32 ' ' 116 't' 104 'h' 105 'i' 115 's'
0x400652 <.rodata+16>: 32 ' ' 105 'i' 115 's' 32 ' ' 106 'j' 117 'u' 115 's' 116 't'
0x40065a <.rodata+24>: 32 ' ' 97 'a' 32 ' ' 98 'b' 105 'i' 116 't' 32 ' ' 108 'l'
0x400662 <.rodata+32>: 97 'a' 114 'r' 103 'g' 101 'e' 32 ' ' 115 's' 116 't' 114 'r'
0x40066a <.rodata+40>: 105 'i' 110 'n' 103 'g' 32 ' ' 116 't' 111 'o' 32 ' ' 116 't'
0x400672 <.rodata+48>: 101 'e' 115 's' 116 't' 32 ' ' 97 'a' 32 ' ' 110 'n' 105 'i'
0x40067a <.rodata+56>: 99 'c' 101 'e' 32 ' ' 86 'V' 105 'i' 109 'm' 32 ' ' 109 'm'
0x400682 <.rodata+64>: 97 'a' 99 'c' 114 'r' 111 'o' 0 '' 37 '%' 115 's' 10 '\n'
0x40068a <.rodata+72>: 0 '' 0 '' 1 '01' 27 '33' 3 '03' 59 ';' 24 '30' 0 ''
0x400692 <.eh_frame_hdr+6>: 0 '' 0 '' 2 '02' 0 '' 0 '' 0 '' 20 '24' -2 'th'
0x40069a <.eh_frame_hdr+14>: -1 '"y' -1 '"y' 52 '4' 0 '' 0 '' 0 '' 36 '$' -1 '"y'
0x4006a2 <.eh_frame_hdr+22>: -1 '"y' -1 '"y' 116 't' 0 ''
Above we just told gdb to print the first hundred bytes starting at str. However, the output of the command is not easy to read or deal with. We can copy that text into Vim and set the following map:
:map <F7> /'.'<CR><ESC>lvy<ESC>maG$p`a
Now, place the cursor at the beginning of the file (<ESC>gg) and press F7 until you get the whole string written at the end.
Explanation:
This macro does the following:
- /’.'<CR><ESC> Places the cursor at the first occurrence of ‘<any character>’. This is, the “letters” we are looking for
- lvy Copies the character under the cursor
- <ESC>ma Sets a mark at the position of the cursor so we can go back to it later.
- G$p Goes to the end of the file (last character of the last line) and pastes the character
- `a Goes back to the mark
This way, every time we press F7, we print a new character at the end of the file. I have to inspect memory addresses at work very often looking for very long strings and this map saves me a lot of time.
Enjoy!
There are some situations when someone messes up with his text editor, file browser or shell and ends up creating a file called something like “-test”.
Selecting the file on the file browser and pressing “Delete” should eliminate it. However, as you will probably have noticed, this blog is called “commandliners” so I am going to explain how to delete this files from a shell.
A naïve try would be something like the following:
$ rm -fichero
which leads to the following error:
rm: illegal option -- l
usage: rm [-f | -i] [-dIPRrvW] file ...
unlink file
Here, -file is interpreted as a flag and not as a file name. You could try surrounding the name with quotes or brackets, escape the leading dash with “\”, but none of these will not work.
There is however, a way to say “the following one is not a command flag although it looks like it”:
rm -- -fichero
The -- (double dash) exists for this reason. You need to specify it only once, e.g:
rm -- -file1 -file2 normal_file
Enjoy!
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!