Book Image

Hacking Vim 7.2

By : Kim Schulz
Book Image

Hacking Vim 7.2

By: Kim Schulz

Overview of this book

<p>Vim is one of the most powerful open-source editors used by programmers and system administrators around the world. While Vim itself is inherently a powerful editor, personalizing it to suit your needs can be a daunting task. However, it is possible to do so with the help of this book.<br /><br />This book contains examples that cover everything from personalizing Vim according to the your work cycle to optimizations that will boost the your productivity. The main focus of this book is to make your life, as a Vim user, easier.<br /><br />Each chapter deals with a different aspect, and provides recipes for easy-to-use hacks to customize and simplify your Vim experience. After an introduction covering the derivation of Vim and its relatives from the vi editor, the author explains basic changes that you can make to the appearance of the Vim editor. Further chapters cover improved navigation through files and buffers in Vim; speeding up your work with templates, auto-completion, folding, sessions, and registers; and formatting text and code, including using external formatting scripts. The final comprehensive chapter covers everything about using Vim scripts and scripting to extend functionality.<br /><br />This book is written for Vim 7.2, the latest stable version. This latest version of Vim includes many new features like spell-checking, code completion, document tabs, current line and column highlighting, undo branches, and much more.</p>
Table of Contents (15 chapters)
Hacking Vim 7.2
Credits
About the Author
About the Reviewers
Preface
Index

Vim games


Even though Vim is a text editor, people have spent a lot of time on creating scripts that make it do other things (other than being an editor). Among these are small games that you can play directly inside the Vim editor. Notice that these are not just simple games like ''20 questions'' where everything can be done in text. These are actual graphical games! The graphics are not the best because they are created as so-called ASCII-art. They are, however, enough to give reasonably good game-play, and hours of fun.

So let's take a quick rundown on some of the games that Vim users have created for Vim in Vim script.

Game of Life

The first game is not really a game, but still a script worth mentioning. The Game of Life is what's normally called a zero-player-game because you don't play the game, but rather just watch how the game plays itself. The game has a simple artificial intelligence that emulates the evolution of cells. The cells follow some very basic rules:

  1. Any living cell with fewer than two living neighbors dies, as if by loneliness.

  2. Any living cell with more than three living neighbors dies, as if by overcrowding.

  3. Any living cell with two or three living neighbors lives, and moves on as being the next generation.

  4. Any dead cell with exactly three living neighbors comes to life.

In 1996, a guy who calls himself Eli the Bearded created a Vim script that implements these rules and prints a Game of Life in the current buffer. It was not particularly fast, but was meant as a proof-of-concept implementation. For most people this game will be quite boring, but for Game of Life enthusiasts, this implementation could be very interesting.

You can find the Game of Life at the following site: http://www.vanhemert.co.uk/vim/vimacros/life1.vim.

Nibbles

When I got my very first PC back in 1986, I only had one game to play. This was a game called Nibbles. I have spent a lot of time playing this game, where I had to control a small worm moving around, in different levels. In each level, it had to eat some things in order to grow. For each thing it ate, another part was added to the end of the worm, and after a while the worm was really long. You could not cross the boundaries or wall of the level and you were not allowed to cross your own tail as well.

In 2004, Hari Krishna Dara recreated this fine game as a Vim script. He only implemented a few levels, but made it possible to easily add more levels, if needed. The game-play was nice and it ran quite smoothly, if you keep in mind that it reprints parts of the text over and over again.

You can find the latest version of the game at the following address: http://www.vim.org/scripts/script.php?script_id=91.

Rubik's cube

In 1974, a Hungarian sculptor and professor of architecture, Ernő Rubik created a complex mechanical cube consisting of 27 smaller cubes. The sides had different colors such that each face of the cube had the same color. However, if you turned the layers that the smaller cubes created, you could scramble the colors, and the puzzle of the game was to get it back into an unscrambled setup again.

In 2005, more than 30 years after the first Rubik's cube, Olivier Vermersch recreated the game of Rubik's cube in Vim script. The Vim version of the cube can still give the player hours of fun and mind puzzle.

The script that makes it possible to play the game is available via the online Vim community. Instructions on how to download and play this game can be found at this address: http://www.vim.org/scripts/script.php?script_id=1271.

Tic-Tac-Toe

Most of the children of the modern world know the simple game of Tic-Tac-Toe and have played it at some point of their life. So, why not implement this game in Vim, such that you can play against your favorite editor to see which of you is the smarter one?

In 1996, Kevin Earls decided to do so, and this ended up as a list of Vim macros that combined to give a nice little Tic-Tac-Toe game inside a Vim buffer.

Even though the artificial intelligence for the opponent (Vim) is not that advanced, it can still be quite hard to win against.

You can find the script file you need to use, in order to get this game, at Mr. Earls' home page. Installation and usage instructions are inside the script: http://www.vanhemert.co.uk/vim/vimacros/ttt.vim.

Mines

Back in 1995, when Microsoft released Windows 95, one of the games that came with it was a little game called Mines or Minesweeper. The game was a mixture of a simple numeric puzzle, and a gamble game. The player had to get an entire area cleared of mines, by either clicking on areas without mines (guessing) or calculating where the mines were, and marking them with a flag. To calculate the placement of the mines, you had to get an area cleared first. This revealed some numbers that said how many mines there were in the squares next to this particular area. When you calculate where a mine is placed, you mark it with a flag.

In 2004, Charles E. Campbell recreated this game in Vim script, such that you can get up a minefield in an empty Vim buffer. You can play the game in different difficulty levels. Although you can win the easy mode quite easily, you can get a headache while trying to get the mines marked on time in the higher levels.

You can find the script that makes this possible on the Vim online community by following this link: http://vim.sourceforge.net/scripts/script.php?script_id=551.

On this site, you will also find information about how to install and use this game.

Sokoban

I have to admit that I just love to play puzzle games, so I obviously also like the game Sokoban. The game play is simple and so is the user interface—however, the puzzles are mind-bending and quite hard. The task is to be a small man who has to move some boxes around in mazes / corridors. Sounds easy right? The man can however, only push and not pull the boxes. So, whenever it gets into a corner or blind alley, the box is stuck and you cannot get through that level without restarting.

In 2002, Mike Sharpe recreated this game in Vim using the level definitions from the old Linux game XSokoban. He kept the user interface very simple but the game play is still great.

Note

You can find the game XSokoban at the following address: http://www.cs.cornell.edu/andru/xsokoban.html.

If you want to play this fine game, then you can find the script and installation instructions on the Vim online community site at this address: http://www.vim.org/scripts/script.php?script_id=211.

Tetris

The final game for Vim that I am going to mention in this appendix is a real classic—Tetris, where blocks of different sizes and shapes fall down and need to be placed properly to produce complete rows. This game can be dated back to 1985; the Russian Alexey Pajitnov designed and created it. Since then, the game has been implemented for almost any platform, and in hundreds of different variations over the same theme.

In 2002, Gergely Kontra decided to implement this game in Vim script and this turned into yet another fine implementation of this classic game. The game even has different modes and keeps a high-score list so that you can play against your own previous records.

You can find the game at the Vim online community site at the following address: http://www.vim.org/scripts/script.php?script_id=172.

Here, you will also find instructions about installation and how to play.