Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Look up a unicode character by name

Terminal - Look up a unicode character by name
egrep -i "^[0-9a-f]{4,} .*$*" $(locate CharName.pm) | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done
2010-12-31 16:47:59
User: hackerb9
Functions: egrep locate read
12
Look up a unicode character by name

[Update! Thanks to a tip from ioggstream, I've fixed both of the bugs mentioned below.]

You, yes, 𝙔𝙊𝙐, can be the terror of the Internet! Why use normal, boring bullet points in your text, when you could use a ROTATED HEAVY BLACK HEART BULLET (❥)!? (Which would also be an awesome band name, by the way).



This script makes it easy to find unusual characters from the command line. You can then cut and paste them or, if you're using a GTK application, type Control+Shift+U followed by the code point number (e.g., 2765) and then a SPACE.



USAGE: Put this script in a file (I called mine "ugrep") and make it executable. Run it from the command line like so,



ugrep heart



The output will look like this,



☙ U+2619 REVERSED ROTATED FLORAL HEART BULLET

♡ U+2661 WHITE HEART SUIT

♥ U+2665 BLACK HEART SUIT

❣ U+2763 HEAVY HEART EXCLAMATION MARK ORNAMENT

❤ U+2764 HEAVY BLACK HEART

❥ U+2765 ROTATED HEAVY BLACK HEART BULLET

❦ U+2766 FLORAL HEART

❧ U+2767 ROTATED FLORAL HEART BULLET

⺖ U+2E96 CJK RADICAL HEART ONE

⺗ U+2E97 CJK RADICAL HEART TWO

⼼ U+2F3C KANGXI RADICAL HEART



You can, of course, use regular expressions. For example, if you are looking for the "pi" symbol, you could do this:



ugrep '\bpi\b'



REQUIREMENTS: Although this is written in Bash, it assumes you have Perl installed because it greps through the Perl Unicode character name module (/usr/lib/perl5/Unicode/CharName.pm). Note that it would not have made more sense to write this in Perl, since the CharName.pm module doesn't actually include a subroutine for looking up a character based on the description. (Weird.)



BUGS: In order to fit this script in the commandlinefu limits, a couple bugs were added. ① Astral characters beyond the BMP (basic multilingual plane) are not displayed correctly, but see below. ② Perl code from the perl module being grepped is sometimes extraneously matched.



MISFEATURES: Bash's printf cannot, given a Unicode codepoint, print the resulting character to the terminal. GNU's coreutils printf (usually "/usr/bin/printf") can do so, but it is brokenly pedantic about how many hexadecimal digits follow the escape sequence and will actually die with an error if you give the wrong number. This is especially annoying since Unicode code points are usually variable length with implied leading zeros. The CharNames.pm file represents BMP characters as 4 hexits, but astral characters as 5. In the actual version of this script that I use, I've kludged around this misfeature by zero-padding to 8 hexits like so,



/usr/bin/printf "\U$(printf "%08x" 0x$hex)"



TIP 1: The author recommends "xsel" for command line cut-and-paste. For example,



ugrep biohazard | xsel



TIP 2: In Emacs, instead of running this command in a subshell, you can type Unicode code points directly by pressing Control-Q first, but you'll likely want to change the default input from octal to hexadecimal. (setq read-quoted-char-radix 16).



TIP 3: Of course, if you're using X, and you want to type one of the more common unusual characters, it's easiest of all to do it with your Compose (aka Multi) key. For example, hitting [Compose] <3 types ♥.

Alternatives

There are 4 alternatives - vote for the best!

Terminal - Alternatives
grep -i "$*" /usr/lib/perl5/Unicode/CharName.pm | while read a b; do /usr/bin/printf "\u$a\tU+%s\t%s\n" "$b"; done
2011-01-04 11:30:16
User: ioggstream
Functions: grep read
1

No need for further filedes or substitution for splitting. Simply use read a b

Know a better way?

If you can do better, submit your command here.

What others think

For some reason this module wasn't included with Ubuntu's perl, so I had to install it using

sudo apt-get install libunicode-string-perl

Then I had to run updatedb to let locate know where CharName.pm lives (well, actually it was getting late and I was lazy, so I let the updatedb cron job take care of this for me).

I will suggest making a shell function of this:

ugrep() { exec 5< <(grep -i "$*" $(locate CharName.pm));while read <&5;do h=${REPLY%% *};/usr/bin/printf "\u$h\tU+%s\t%s\n" "$h" "${REPLY##$h }";done; }

The other thing that got me is that in unicode land, an umlaut is called a diaeresis.

Anyway, nicely done.

Comment by bartonski 206 weeks and 6 days ago

Thanks for the compliment, Bartonski, and the suggestion. I don't use shell functions because I don't like all my programs munged together in a single file. It's cleaner and easier to just toss shell fragments like this into a ~/bin directory and chmod 755 them. Also, although infinitesimal, each shell function in a .profile slows down the start up time of every login shell. If this was a command I use all the time and it was important that it not fork, for example "ls", I would use a shell function. But, for the other 99.9% of commands, I use script files so it will only be loaded when needed.

Comment by hackerb9 206 weeks and 4 days ago

P.S. If you find yourself needing umlauts, you'll definitely be happier once you enable a Compose key. Both Gnome and KDE have an option to do so in their keyboard control panels. If you don't use either of those, you can change it the "old fashioned" way,

xmodmap -e "keysym Menu = Multi_key"

That will let you hit (one at a time) the Menu key, a letter key, and then a double quote to get an umlaut. For example, Multi_key o " → ?. (And to get the right pointing arrow I just typed, I used Multi_key, minus, greater than).

Comment by hackerb9 206 weeks and 4 days ago

Somehow I have multiple copies of CharName.pm on my system, so I needed to limit the results to one:

locate -l 1 CharName.pm

Otherwise, great command.

Comment by billmakesbooks 203 weeks and 2 days ago

Awesome command! By the way, if you want to have even more Unicode characters available via XCompose, check out this project to add more key bindings:

https://github.com/kragen/xcompose

Comment by hfs 153 weeks and 6 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts