2008-12-22

Beyond basic Vim usage

I have used Vim for about 8 years now, but for quite a while I didn't use the "vee-eye" key mapping the way it should be used. All I did was press i to go to Insert mode, type the text and then Esc:wq to save and quit. That's completely the wrong way to use this wonderful text editor. If you are in that situation I hope this Vim introduction can get you past it. After an overview of some settings for the configuration file I will list the commands everyone should know (and some you probably don't really need to know).

Basic configuration

To be able to use Vim efficiently you have to have some basic configuration set up. In Linux or Unix you should edit
~/.vimrc (Windows specifics last in the post) to include at least the following lines:
set nocompatible      " This is vim, not vi (Put this line first!)
colorscheme koehler " Optional, but I like a black background
set smartcase " Only do case sensitive match on Upper Case
syntax on " Syntax highlight on
set showcmd " Show the command you have typed in
set showmatch " Show matching brackets or parentheses
set wildmenu " Show possible command tab completions
set ruler " Show useful information on the command line
set incsearch " Incremental searching
set hlsearch " Highlight search results. Clear with :nohl
Besides these settings I have some more things, mostly related to tabs and indentation:
set expandtab         " Expand tabs to spaces
set tabstop=4 " 4 spaces is one tab
set shiftwidth=4 " Should be the same as tabstop
set softtabstop=4 " Makes the spaces feel like real tabs
set smarttab " Backspace over expandtab
set autoindent " Use current indentation on next line
set nocindent " Don't use cindent
set nosmartindent " Don't break filetype indent scripts
filetype on " Try to figure out the right filetype
filetype plugin on " Filetype specific plugins
filetype indent on " Filetype specific intenting
There's a lot more you can configure in the rc-file and there are plenty of good examples on the net, just search.

File handling

The file handling commands you need to know are:
:w         " Write changes to disk
:w newfile " Write changes to newfile
:w! " Force write
:q " Quit
:q! " Really quit!
:wq " Save and quit
:wqa! " Really save all files and quit.
Also, opening a file with vim + filename will position you at the end of the file.

Movements


A great way to getting started with vim is the built in
vimtutor, a text file tutorial you can follow to learn all basic commands in Vim. But to get you up to speed I'll list the basic moves here. Just one comment first. Even though the arrow keys probably work, don't use them! They are slow and you have to move your fingers from the keyboard.
h      " Left - Moves the cursor one step
j " Down
k " Up
l " Right

gg " Go to the start of the file
G " Go to the end, just like in less

w " Move one word forward
W " Move to next blank delimited word
b " Move one word backwards
B " Move to previous blank delimited word

0 " Go to the beginning of the line
$ " Go to the end of the line

H " Move to the top of the visible screen (Head)
M " Move the cursor to the middle of the screen
L " Move to the end of the screen (Last)

fc " Forward onto the character c on this line
Fc " Backwards onto the character c on this line
tc " Forward to the character c on this line but not on it
Tc " Back to the character c but not on it
nfc " Forward onto the nth c character on this line
All these moves can be used together with the commands I will describe below and will be referred to with a m. Most moves and actions can be performed with a number in front. 5w means move five words forward. I'll note that number n and a single char with c.

Normal mode and Insert mode


Vim has six basic modes, the most important two being Normal och Insert. Normal mode is the default where you do all the commands and movements and Insert is when you actually type in text
. There are many commands related to changing between these two modes:
i      " Go to insert mode before the cursor position
I " Go to insert mode at the beginning of the line
a " Go to insert mode after the cursor position
A " Go to insert mode at the end of the line
o " Insert a new line below and go to insert mode
O " Insert a new line above and go to insert mode
c
m " Delete from the cursor to m and go to insert mode
cc " Delete the whole row and go to insert mode (Change)
C " Change from current position to the end of the line
rc " Replace the character under the cursor with c
Esc " Go back to Normal mode
Ctrl-c " Go back to Normal mode
Ctrl-o " Do one command in normal mode, i.e Ctrl-o$
Search and Replace

Usually the fastest way to navigate the cursor is to search for the text you want to go to:

/regex " Search for a word or a regular expression
* " Search forward for the word under the cursor
# " Search backwards for the word under the cursor
n " Next search match
N " Previous search match
'' " Two back ticks; Move cursor to where you were before the search

:n " Go to line number n
:%s/oldtext/newtext/g " Replace oldtext with newtext in the whole file
:%s/oldtext/newtext/gc " The same, but confirm each replace
When doing a replace the %s means search every line in the file and the /g means replace all occurrences on the line. Omitting the g means you will only change the first occurrence on the line and. Without the % you only search the current line.

Cut, Copy and Paste


As expected there are many ways to copy and paste text in Vim. The "must know" ones are:
x   " Delete the character under the cursor
dd " Delete the current line and put it in the clip board
dw " Delete the word under the cursor
ndj " Delete n rows down and put them in the clip board
D " Delete from here to the end of the line
yy " Yank a line, meaning to copy it
yw " Yank a word
ym " Yank from the cursor to m
p " Paste below or after the cursor
P " Paste before or above the cursor
np " Paste
n copies of what you have in the clip board
Undo and Redo

Another great feature with vim is that you can do multiple undo or redo. The commands are:
u            " Undo once
uu " Undo two times
Ctr-r " Redo

:earlier n " Go to older text state n times
:earlier ns " Go to about n seconds before
:earlier nm " Go to about n minutes before
:earlier nh " Go to about n hours before

:later n " Go to newer text state n times
:later ns " Go to about n seconds later
:later nm " Go to about n minutes later
:later nh " Go to about n hours later
If all these normal mode commands is too much to remember at once there's a great printable cheat sheet at viemu.com that can help you practice them. There's a good quick reference available here.

Fun tricks

A fun thing about vim is that there's always more to learn and a quicker way to do the kind of edit you are trying to do. Some of my favorite tricks are:
.            " The simple dot replays your last command. Very powerful!

r[enter] " Insert linebreak without going into insert mode
J " Append the line below to the current line

qq[do stuff]q " Record the stuff you do in command register q
@q " Replay the action saved in q
@@ " Replay last used action

ci> " Delete between previous <> and go to insert mode
da> " Delete from previous <>, including the tags

^r=(3+4)*5 " Use when in insert mode. Will insert 35 at the cursor
Ctrl-A " Increase the number closest to the cursor with one
Ctrl-X " Decrease the number closest to the cursor with one

~ " Toggle case of the character under the cursor
gu
m " Lowercase from the cursor to m
gUi> " Uppercase between the previous <>

:TOhtml " Generate html that looks exactly like what you see in vim

g?
m " Do rot13 on the movement

z[Ret] " Scroll the window so that the cursor is at the top
zz
" Scroll the window so that the cursor is in the middle
z- " Scroll the window so that the cursor is at the bottom

gf " Go to the file under the cursor
K " Look up man page for the word under the cursor

!ls " Run ls in the current working directory
!!date " Replace the current line with the date. Use date /T on Windows
:set number " Show line numbers

:reg " Show what you have in the registers (The clip boards)

Ctrl-g " Show filename and position in file

:Ctrl-r Ctrl-w " Copy the word under the cursor to the : line
Split windows

In vim you can have more than one file visible in the editor at the same time, either by opening a file in split view or by using
vimdiff.
:split       " Split the windows horizontally
:sp myfile " Open myfile in a split view
:vsplit " Split the window vertically (:vs works as well)
Ctrl-ww " Move the cursor to the other window
Ctrl-w= " Resize the windows as equally as possible
Ctrl-wc " Close the current window
Custom mappings

Vim has a lot of key mappings available and it is easy to add your own to your vimrc-file. An example to make pasting text into Vim easier when you have autoindent/smartindent on:
map <C-s> :w<CR>
nnoremap <F2> :set invpaste paste?<CR>
imap <F2> <C-O><F2>
set pastetoggle=<F2>
The first line sets a mapping so that pressing in normal mode will invert the 'paste' option, and will then show the value of that option. The second line does the same in insert mode (but insert mode mappings only apply when 'paste' is off). The third line allows you to press when in insert mode, to turn 'paste' off. This tip and lots of others can be found at the great Vim Wikia.

Vim on Windows

On windows the configuration file is called _vimrc and can be found in your Vim install directory. By default it has some settings already, but you can still add the basic settings mentioned in the beginning of this post. In addition to that you should set nobackup and for fun, remap Ctrl-A and Ctrl-X key bindings to be Ctrl-+ and Ctrl-- on the numpad so that they work. They are used to increase and decrease numbers:
set nobackup
noremap <C-kPlus> <C-A>
noremap <C-kMinus> <C-X>
Thanks for reading this far. These settings have been found during a long time and from all kinds of sources and there are almost infinitely many to be found.

After this post I understand why the Daily Vim blog has a black background everywhere. It is a pain to get black block quotes in blogger to show your nice vim highlights. (Hint; edit the html to use <blockquote style="font-family: courier new; background-color: black; color: white;"><pre></pre></blockquote> around the html generated by TOhtml. Also, remember to remove the superflous tags, :s%/
<br>//g.)

(The post has been updated after graywh's comment.)

2008-11-04

Zeitgeist: Addendum

Zeitgeist: The Movie was by far my favorite “provocative documentary” of 2007, and a month ago a new movie by the same team called Zeitgeist: Addendum was released.

I'd say that this new movie is less provocative and more informative and inspiring than the first one. The topics are still the same, that the banks control the world more than you think, the war on terror is a scam and that religion is just one of many ways to keep the general public from asking too many questions.

Chapter I (0:06:45)

The first fifteen minutes gives a good introduction to the monetary system that controls the world we live in today. The movie goes through how loans and debt is involved in the creation of money and how much power the banks get because of this and makes it clear how the fractional reserve system of monetary expansion gives rise to more inflation. According to the director Peter Joseph, The Federal Reserve is the main culprit behind all of this and the whole purpose is to transfer true wealth from individuals to the banks. It's a system of modern slavery because with the current system, society will never be debt free since the money needed to repay interest on loans can only be created by even more debt.

Chapter II (0:25:10)

This part starts with a very interesting interview with John Perkins, author of Confessions of an Economic Hit Man. The quote shown in the intro of this chapter describes it very well:
There are two ways to conquer and enslave a nation.
One is by the sword. The other is by debt.
- John Adams (1735-1826)
The movie shows how often economic threats, corruption and sabotage is used instead of a flat out physical war, mostly because it is way cheaper and much less visible. The later part of the chapter discuss another US favorite topic, terrorism, and what it is really all about.

Chapter III (0:54:25)

Chapter three of the movie is a long interview with Jacque Fresco of the The Venus Project, extended with commented short video clips. This 92 year old man is looks much younger than he is, and he clearly describes what the Venus Project is all about.

Basically they want to totally redesign our whole culture to be more ethical and humane. The only way to do this, according to Jacque Fresco, is through technology and social awareness. Money, politics and religion are described as false institutions since they can not change the current system. The profit based society we currently have will never be sustainable or efficient because scarcity is the only way to keep the profits high.

Chapter IV (1:32:42)

The last chapter starts with a description of emergence and begs you to challenge your current belief system and become more aware. They ask for intelligent management of the earths resources in a way that can't be done in a monetary system. The later parts of the chapter gives some suggestions of how to combat this monetary system using technology available to us right now. The points listed for moving towards this goal is:
  1. Expose the banking fraud by boycotting the banks behind it.
  2. Boycott the TV news networks and use the internet instead.
  3. Don't ever join the military. Ever.
  4. Boycott the energy companies and use renewable energy instead.
  5. Reject the political system and focus on technology.
  6. Join the Zeitgeist Movement for social change.
The movie ends with a scene where people stand in a blurry Manhattan intersection and think of what they are doing with their life, and realizing that they have to do something about it.

Watch it!

The two hour long Zeitgeist: Addendum movie is packed full of information that will make you think. Even if you don't agree with everything in the movie, you should be provoked enough to question the current structure of our society. The movie is somewhat US-centric, but the implications are still global. Please don't stay ignorant, visit http://www.zeitgeistmovie.com right now and let everyone you know hear about it!

I, for one, await March 15th 2009...

2008-10-30

An open source license primer

Licensing is a tricky issue, especially when it comes to software, and even experienced developers have a hard time knowing what you can and can't do with open source software. I am not a lawyer, but I've been interested in free and open source for a long time and a while ago a colleague and I held a presentation on licenses (Slides in Swedish here) at the company we work for.

To explain what this license thing is all about you need to start with Copyright, which is as old as the printing press. The whole point of copyright is to give the authors of “original works of authorship” the rights for their works so that they have control over how it will be distributed. If someone else want to distribute that piece of work, they need to obtain a license from the copyright holder, usually in exchange for a one time payment or a royalty agreement.

But what if the copyright owner doesn't want the distribution of his or her work to be limited in any way. The first thought might be to put the work into the public domain, meaning that you abandon any claim of ownership of the work. But is this really making it as “free” as possible? What if the piece of work is source code that someone will download, modify in some way and then compile to a binary program to be sold. Is the original piece of code still free?

This leads us to the use of Copyleft licenses where the idea is to use copyright law to prevent any restrictions that would limit the distribution of a piece of work, or modified versions of it.


Open source licenses are usually grouped into permissive, weak copyleft and strong copyleft. In the picture above the BSD license is a permissive one, LGPL a weak copyleft and GPL and AGPL are strong copyleft, meaning they include the “Liberty or Death” clause that was introduced in version 2 of the GNU GPL. Of course there are other common open source licenses, and OSI has a nice list of them here, but for simplicity I'll only discuss the ones mentioned above.

There are several versions of the licenses mentioned above and some are not compatible with earlier versions. Since GNU GPL is probably the most common open source license FSF have a chart showing compatibility with the latest version compared to the previous version.



Permissive licenses

It's important to note the word Modified before BSD in the image above. The original BSD license, sometimes called "BSD-old" or "4-clause BSD", had an advertising clause that required authors of derivatives of a BSD-licensed work to include an acknowledgment of the original source. This made it incompatible with GPL, and also unmanageable because so many acknowledgments were required. Code licensed under BSD can be used in closed source projects, as long as a copy of the license is included in the distribution. Other permissive licenses have similar demands.

If you release code for a new protocol or media codec, it might be a good choice to put it under a permissive license because it makes it more likely that companies that are not yet open source friendly will still implement it.

Weak copyleft

If you are using code covered by a weak copyleft license like LGPL, it gets a bit more complex. The GNU Lesser General Public License allows users to link to the LGPL program without having to put their own program under the same license. There are still restrictions, like that you have to make it possible to upgrade the LGPL program to a more recent version and if you modify any LGPL licensed code you have to release the changes.

This type of license is often used in toolkits like GTK+ and media libraries like gstreamer so that they can also be used to build proprietary products and get wider adoption.

Strong copyleft

Strong copyleft licenses like GPL or AGPL are a lot more restrictive when it comes to what you can do to the code. They are called strong because they include the “Liberty or Death” clause, meaning that all the code have to be open. This includes linking into libraries and running the code within the same process. It is precisely because of this strictness that LGPL was created and there is also several exceptions to the GPL covering for example GCC, fonts or the GNU Classpath.

There are two major versions of the GPL, 2.1 and 3. The big differences have been explained succinctly by the FSF and basically concerns these points:
  • Tivoization - Using crypto hardware to control what code is allowed to run on the device.
  • DRM - Restrictions on what code you write and distribute.
  • Patents - Using GPL code gives you right to use the patents in it.
  • Digital distribution - Physical distribution of the source code is no longer an explicit demand.
In addition to these changes, the text have become a bit easier to read and clarifies certain details concerning for example out-sourcing. It is also compatible with more licenses and gives you a chance to rectify your mistakes in case you violate the license unintentionally.

The difference between GPL and AGPL is that AGPL has an extension that covers online services like forums, blogs or online stores. This covers the “legal hole” in the GPL where you are not distributing a program but still provide a service for users over the net using GPL licensed code.

Remember

The important thing to remember is that these copyleft licenses are all distribution licenses and not user licenses. This means you are allowed to modify and use them internally within your company or home without releasing the changes to the code, as long as you do not distribute it in any form. Even if the company doesn't want to release all their code, hopefully they will still contribute to the upstream development of the copyleft code they use.

But if you do distribute code, be sure that it does not violate any of the licenses. It has been proven in court several times by GPL-Violations that a copyleft license has the same legal status as a proprietary license. If you think no one will find out that you are using copyleft licensed code because it's built into the firmware of your product you are probably wrong. There is even a manual written by Armijn Hemel describing how to detect license violations in consumer devices.

To be clear, building an application that runs on top of Linux or an open source application server like JBoss does not mean you have to open the source of your application.

If you plan to use GPL code in any way, do it right and follow the Software Freedom Law Center's excellent Guide to GPL Compliance!

2008-07-26

Minix 3.0

Was surprised to see this ad in my Gmail today:


I usually get a lot of Linux and embedded hardware related ads because of the OpenMoko mailinglists, but Minix 3 was really a surprise. I tried running it in VMWare about two years ago after reading a nice introduction to it on OSNews. Can't say I was impressed. It sure didn't look like much back then (and still doesn't, it's just X), but microkernels are very interesting.

There are a lot of L4 kernel based operating systems, and a few of them even run on the Neo1973(Video from here). Hopefully this one will too some day.

There will always be some overhead when using a microkernel compared to a monolithic kernel because of the extra IPC, but the modern implementations are not that far behind. It will be interesting to see how useful the different versions will become, although there's little hope some of them will ever be completed...

2008-05-24

Roguelike revival

I played my first roguelike JAMoria on a Mac Classic II somewhere around 1993. It's a classic black and white ASCII game where you run around with a @ using the numpad on the keyboard. In JAMoria you start in a small city, just like the classic Moria. But instead of just having one dungeon, it has multiple dungeons to descend into, both in the town and in the wilderness that surrounds it.

Like all roguelikes, if you died you had to start over again and create a completely new character to play with. Already being familiar with regular table top role playing games, rolling new stats and trying out new race and class combinations was a major part of the fun.

In early 1996, using a blazingly fast 28.8 baud modem I managed to download ADOM 8.1 and install it on my first non Macintosh pc, a Pentium 75Mhz. At first I found the game really hard, and all new characters died after just a few minutes, but I kept trying. After some newsgroup searching I found help in rec.games.roguelike.misc, and later rec.games.roguelike.adom. And I've been playing ADOM ever since. It's a very very difficult game to "win", but just trying to is fun enough. Not much has happened to ADOM since version 1.1.1 in 2002, and by that time the development of all the other rougelikes I knew had gone into maintenance mode or ceased completely.

Adom: Terinyo

One reason might be that graphical games were making it impossible to compete. One of my favourites, Dungeon Hack, was early and most roguelike of them all. The much more popular Diablo came later and was not turn based, even if the game mechanics was kind of the same. Of course people tried making graphical front ends to the classic roguelikes with varying success. Falcon's Eye was probably the most known. A fork of that project called Vulture's is still under development and it's a game I have yet to try. (The demo video looks good though!)

To get back to the title of this post; It feel there is a huge revival going on in the roguelike scene. There's new blogs, columns, news pages and games showing up everywhere and older newsgroups and pages like RogueBasin seem to have a surge in activity and popularity. Even the ADOM homepage has gotten a face lift with a new blog about Thomas Biskups new roguelike JADE, and a brand new forum.

I guess that the main reason people enjoy making roguelikes is that the games are full of tricky algorithms like dungeon generation, field of vision, monster AI and more, but still small enough for a single developer to make by themselves. The main reason is of course that there's no graphics and no sound (well, DoomRL has sound), so all the programming effort and processing power can be spent on actual game play instead of bells and whistles. If you know what to do, creating a new game from scratch can be pretty quick. 7 Day Roguelike competitions seems to be more and more popular.

Some are more ambitious than others. The crazy guys behind Dwarf Fortress have quit their jobs to work full time on their ASCII dwarf game simulator. It's not really a classic roguelike, but the features are impressive. Julian Mensch has been working on his Incursion since 1999 and the planned release is in 2011!

All in all, it's a genre of games worth trying, and I guess it's a nice way to learn a new language trying to implement a 7DRL in it. (My attempt in J2ME was never completed though. Take a look at Dweller instead.)

Also, I wonder what's the best way to control a roguelike when you only have a touchscreen. Would be nice to have one for the OpenMoko phone.

2008-05-05

Einstein quote

The fairest thing we can experience is the mysterious. It is the fundamental emotion which stands at the cradle of true art and true science. He who knows it not and can no longer wonder, no longer feel amazement, is as good as dead, a snuffed-out candle. It was the experience of mystery - even if mixed with fear - that engendered religion. A knowledge of the existence of something we cannot penetrate, of the manifestations of the profoundest reason and the most radiant beauty, which are only accessible to our reason in their most elementary forms-it is this knowledge and this emotion that constitute the truly religious attitude; in this sense, and in this alone, I am a deeply religious man. I cannot conceive of a God who rewards and punishes his creatures, or has a will of the type of which we are conscious in ourselves. An individual who should survive his physical death is also beyond my comprehension, nor do I wish it otherwise; such notions are for the fears or absurd egoism of feeble souls. Enough for me the mystery of the eternity of life, and the inkling of the marvellous structure of reality, together with the single-hearted endeavour to comprehend a portion, be it never so tiny, of the reason that manifests itself in nature.
-Albert Einstein, The world as I see it

2008-04-17

History in Zsh

Since I use Zsh with shared history, the normal history command doesn't work properly. Instead I came up with this ugly hack using cat and cut:
cm@tyst> cat ~/.zshhistory | cut -c16-40 |awk '{a[$1]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
550 ls
496 cd
322 make
224 vim
168 su
151 screen
105 ssh
91 bitbake
76 rm
58 mtn
and for root:
root@tyst> cat ~/.zshhistory | cut -c16-40 |awk '{a[$1]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
713 emerge
312 vim
247 ls
165 cd
120 screen
108 iptables
83 etc-update
60 top
59 dmesg
53 cp
Guess it's quite obvious what distro I use on my computer at home and what I spend time doing...

2008-03-09

Lies

For some strange reason studies of liars seem to pop up all over the net right now. NYmag has a long and very interesting article about why kids lie and how they learn it from their parents. Kids start as early as age 2 or 3, and the smarter the kid the earlier they start lying. Telling the truth is easy, but making a believable lie takes a lot more effort.

The always excellent Radiolab had a show last week about lies where they presented the results from several studies on the subject. They interviewed the famous psychologist Paul Ekman about facial expressions and how to tell if people are lying. The whole concept of microexpressions is fascinating. And Ekman's vow he took when his daughter was born to never lie again seems incredibly difficult to keep.

Another part of the program discusses a study Yaling Yang did on pathological liars. Brain scans of them showed that they had a lot more white brain matter in their prefrontal cortex compared to the control group, but less gray matter. The researchers believed that the huge increase in connections in the brain made lying practically effortless for these individuals and they could reply instantly with a plausible lie.

The radio show continues with a segment about a con-woman called Hope and how she used unsuspicious people by constantly lying.

People who are good at lying are generally more happy and have greater success in life. People with serious depressions are "too realistic" and seem unable to twist the truth into a brighter version. Being able to lie to yourself as well as others have many advantages even when you don't abuse it to the fullest.

I spend a lot of time thinking about how to interact with people and even more observing what people do and how they react. If I had no moral or empathy there's no limit to how much I could take advantage of peoples good will. Most people want to help and trust other peoples "good nature", but I'm afraid there's too many who don't have that in them. I'll be sure to keep my eyes open for the slightest "leakage", be it a microexpression or an inconsistency in the story. Just never let people know you caught them lying, that just makes them more careful next time and harder to catch.

This cynical post is written while listening to Billy Talent - Lies

2008-01-28

Schwartz and gllin

For some reason Andrzej "balrog-kun" Zaborowski is still missing from PlanetOpenMoko, so I'll just link to some of his recent posts instead.

Andrzej have written an ELF-loader called Schwartz that can translate OABI to EABI and his latest post shows how to use it with gllin instead of a chroot.

Very impressive! I haven't tried it myself yet, but I definitely will soon.