Security

Hacking Vim covers the basics and reveals tips for power users

Vincent Danen reviews a comprehensive new book on the vim text editor that will help turn you into a vim power user in no time.

I recently obtained a copy of the book Hacking Vim by Kim Schulz and published by Packt Publishing. For the record, I've read a few vim books; I have O'Reilly's vi Editor Pocket Reference on my desk, as well as Learning the vi Editor.

Neither book compares to Hacking Vim. This book was an absolute treat to read if you are a vim power user or want to be. If you're just using vim occasionally on remote systems or your usage is sporadic, then this book might not be for you. However, for someone who wants to take their vim usage from just casual text editing to doing some very powerful and productive things with it, you'll learn a lot from this book. I've been using vim for a few years now, and I've learned a lot of new things and seriously tweaked out my vim configuration as a result.

The book recaps the history of vim and other vi-like editors, but unlike other books I've read, the introductory content is brief. The book proceeds logically from configuration basics to the more advanced things you can do with vim. It also covers the GUI version of vim (gvim) for those who want to use a GUI text editor.

To start with, it discusses the various configuration files and how to do basic things like change fonts, colors, the status line, and so forth. This first part is all about customizing vim's appearance so that it works and looks the way you want it to. It also discusses topics, such as setting up a spell checker and changing the default key bindings.

Next, it covers navigation in vim: movement keys, dealing with buffers, searching in files, and markers. This is largely standard fodder for any vim book or tutorial, but unlike other books I've read, this one provides some great examples and illustrations.

In another chapter Schulz provides tips on boosting productivity. There are some real gems in this chapter, such as using template files to speed up document creation and using and creating auto-completion commands to make typing the same thing over and over again less tedious and time-consuming. Here are some of the other subjects covered in this section:

  • How to use and record macros in order to make vim do specific things at the touch of a key (or button)
  • Using sessions with vim (something that was entirely new to me)
  • Using registers and undo branching with some great examples that really make it clear
  • Text file folding (such as folding code functions)
  • Using vim as a simple outliner
  • Opening remote files easily

The Advanced Formatting chapter covers topics a lot of other books do as well: how to format text, using various indenting methods, and also discusses external formatting tools.

The final chapter of the book weighs in on scripting vim, which is a powerful means of further customizing this editor. It discusses topics like writing the actual scripts, writing syntax and color scheme files, and using vim with external interpreters such as perl and python.

All in all, I highly recommend this book if you are at all interested in making vim a highly productive text-editing environment. I found it to be well written with great explanations and insight, and it covered a lot of topics that other books on vim never touched.

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

20 comments
mdhealy
mdhealy

I used vi for many years, and today my primary text editor is vim. I like it a lot, but probably I would like emacs had I learned it first. As it happened, when I was a student I learned vi rather than emacs, and since then have not been motivated to learn another editor, simply because learning another editor wasn't on the critical path for anything I needed to accomplish. About half my colleagues use vim and about half use emacs, but all of us seem to get our jobs done. Both vim and emacs are cross-platform so you can probably find versions for your environment. I will admit that because I also use Perl frequently, I run a lot of Perl one-liners from vim. So for stuff like rearranging fields in a delimited file which I know COULD be done with native vim commands I tend to feed the buffer through Perl simply because Perl is what I know. On any new computer among the first tools I install will be vim, Perl, and GNU bc. Also, for folks who started in Unix and Perl but spend a lot of time on other platforms today, the Perl Power Tools are a cool set of Unix-like utilities written in Perl so they are available wherever Perl is.

Jaqui
Jaqui

vim. yuck it's no better than emacs. both suffer from bad ui and poor documentation.

mikifinaz1
mikifinaz1

Well, all the Linux/UNIX users are still caught in the last century. Vim, Vi and Emacs are so old school. If Linux wants to really get out of the geek only space it will have to do more graphical editors in a better way. Gnome and KDE are on the right path. It is what average people see and use that makes the difference. There could be a bunch of monkeys in a cage making it for all they care. Yes, these old editors are powerful, so was the command shell in Windblows systems. Users hated it and the minute Microsoft went graphical Windows systems took over. Don't throw away the tools, just make better visual ones that are easy to use. Ubuntu has the right idea. (I won't fight over implementation of that idea by them) Make it simple on the surface to lure in new people. Mac is the ideal in this way. Easy for new people with a powerful system underneath for the geeks.

CharlieSpencer
CharlieSpencer

While I can't dispute your position on the UI, the article is about a book the author feels is a solution to the poor documentation problem.

Neon Samurai
Neon Samurai

First, it's a text editor or word processor. No reason to run a full graphic back end just to support display of ascii. If you really can't live without it though, vim and Emacs both have GUI layers with pointy-clicky menus. Your also not limited to those editors so you can choose your kwrite or similar. Mouse control has only ever been faster than keyboard control for drawing like raster or vector graphic work. Other than things explicitly requiring pen or mouse fluid control, the keyboard is the way to go. Word, Excel.. you can eat those programs alive and cut your work time in half or more once you get off the mouse. In many cases, imposing a graphic desktop is just not a rational decision either. Regardless of what Windows Server would have one believe, a server with graphic desktop is not rational. There is no justification for having that much extra resource use and potential for security vulnerabilities. For *nix, Webmin will provide a safe graphic interface with much less software and resource overhead but it's a *nix so SSH is all you probably need. For Windows, get the graphic crap off the server and put a management consol app on the admin's workstation under a user level account. Give it a proper encrypted protocol. Don't invite the admin to log into the server directly or over insecure protocols then use the server's web browser to invite attack. If you learn vi, you can also be pretty sure than any *nix or similarly based network appliance is going to have it. On a Linux desktop, it's probably there when needed. On a Windows desktop, notepad doesn't even manage ascii text correctly let alone the rest of the GUI editors or larger word processors. Thanks goodness Edit is still part of the standard install though; open "cmd", "edit filename", "save".. ascii characters and formatting are no longer eaten by Windows different handling of hard and soft line wraps or similar. You also can't have multiple account open on an AD connected Windows workstation. Thank goodness on can "runas" an admin cmd.com.. it's not quite "su" or "sudo" but it's as close as windows gets without logging the user level account out. In terms of Windows only becoming popular after dropping the all powerful command line; it's only recently come close to what a real text shell like Bash can do. Dos, Win95, Win98, winNTs up to winXP have a crippled command line in comparison. PowerShell is magnitudes better than any command shell provided in the past and it's an MS offered extra utility rather than standard install component. By all means, stick with what editors and work environment you prefer. Don't blindly decry text editors and non-GUI though simply because they are not pretty though. Once you learn more keyboard control within GUI and text shell in general, you'll wonder how you managed without it; less carpel tunnel strain also.

vdanen
vdanen

If you need GUI, and power, try gvim. Oh, and FWIW... Ubuntu doesn't have the "right idea". Open a console and vim will work. Just like any other distro. Install the vim-x11 package, and you have gvim... just like any other distro. Ubuntu has innovated nothing in regards to text editors. Tell me what Ubuntu has that no other distro has and then maybe I'll consider the rest of your anti-Linux rant. On another note, you mention the Mac being the ideal way. I agree. But, then again, it comes with vim also. What does that tell you? I think about the only readily-available OS that *doesn't* come with vim is Windows.

wratholix
wratholix

I think your approach is a little off track and even though you are right, there are more visual appealing editors for the GUI. you are forgetting that most linux/bsd boxes are accessed via remote shell such as ssh. Personally i wouldnt want to dump gnome or especially kde on my server and setup tunnels for my X just to edit my configs. Perhaps you're one of those linux users that never bothered with learning some of the core functionalities that make unix based systems so popular amongst us geeks. vi is a complex editor for that can handle complex job and most of all never fails to do the job efficiently & fast. To come back to shells... on wind0wse systems really wasnt anything 'powerful' .. it just passes the border of being useful for a few scripts, one of the problems being that the the sysconfig is stored in the registry these days but most of all never came with a decent editor.. nor solid usability.. but what does that have to do with editing? Since the wind0wse gui is up and running anyway i'd use a free editor like Crimson Editor (continued as Emerald Editor) in the GUI instead of wasting my time in a windows shell. http://sourceforge.net/projects/emeraldeditor/ Sure we can pull out some other big names but i'm not talking about installing a couple of gigs of software to do editing :P Since i found this lightweight power editor for windows cannot go without see the light of day without it :) It helps me editing my cisco device configs to scripts to anything in neat colors since it can read the syntax as well as edit with regexpr... I looked at the WinVi posted above and i didnt catch on.. can you use Vi commands? i think it is a little plain and lacking. Give Crimson a try :P

ME
ME

I use a chain saw -- a downright dangerous device and unnecessary for people who think that wood comes from furniture stores. But if you need to cut up trees in a hurry, the expense and learning curve are well worth it. My guess is that you haven't experienced it yet, but someday you will no doubt find yourself asking, "How can I automate this repetitive task?" Vim, or some other command-line tool will be the answer. It is exactly this capability, via OS X, that transformed the Mac from an artist's and child's plaything into a seriously productive development platform. There are many things in computing that you don't want to have to click a mouse button every time you do it. Hence command lines and powerful tools. You'll understand it when you need it.

CharlieSpencer
CharlieSpencer

As I understand it, vi / vim are almost guaranteed to be included with every readily available distribution. Because they run at the command line, it doesn't matter what GUI you're using. While these features may not matter to an individual user, they're positive attributes to support techs who work with multiple distros, and administrators who don't want to use CPU overhead for a GUI. If you don't like a tool, no one is requiring you to use it; but don't expect everyone who does use it to abandon it because you don't.

chartleyucf
chartleyucf

The point of the article was to show case a good guide to using vi/vim, which has a lot of features that most people don't take advantage of. There is no reason to get rid of power tools for super users in order to make an OS work for those who don't use them. This seems more like a rant about why people dislike Linux rather than constructive comment about the article.

sparkie
sparkie

If you want an easy and proper texteditor without the superfluous code try WinVI. The best of both worlds.

Jaqui
Jaqui

I bet the book kept to the same documentation stupidity in the vi(m) guide C-c when they mean [Ctrl]c M-x when they mean [Alt]x [ if Mac the Command key ] then the app is MODED, enter commands mode and edit text mode. that should have been killed off by 1990. Odd that Wordperfect had more keystroke options and never had MODED operations, yet vi(m) has to have moded operations.

Neon Samurai
Neon Samurai

It was foriegn for me with touchpad and a few minutes here or there on friend's machines. A machine with mouse attached clarified the GUI interface. It became wide open and native for me the moment I found the command shell under utilities. With cli and ssh, it's as natural as any other OS on my network; you can see the directory trees normally hidden by the application icon also.

Jaqui
Jaqui

vi/vim or emacs will be the default text editor on the cli. both are bad choices for the default text editor BECAUSE OF their extensive feature sets. They are not new user friendly at all. nano, the gnu clone of pine, is a basic text editor, with the commands displayed on the bottom of the screen. what is displayed is the entire command set. I think it is a far better option for the default text editor because it is so limited. edit to add: screenshot of nano http://jaqui-greenlees.net/images/snapshot1.jpg screenshot of vi http://jaqui-greenlees.net/images/snapshot2.jpg both at same size in konsole, and scaled to 800x600 for dissplay online.

Neon Samurai
Neon Samurai

Nice to see the article though as it's reminded me to get my Vim cheat sheet out again and get back to more learning.

Jaqui
Jaqui

for the typical end user? not likely. vi(m) and emacs both fail to have documentation that is understandable for anyone who is NOT a university computer science student / graduate. with most new users for these apps not having a computer science degree, the documentation needs to be written according to Publishing Industry Standard nomenclature, so [Ctrl] and [Alt] are a 1000000% improvement over C and M when it comes to legibility.

vdanen
vdanen

You can customize the keybindings all you like. The book shows you how to do that also. So (simple) things you normally need to do in a particular mode you can remap to other things (i.e. making CTRL-A work like going to command mode, $, then going back to insert mode). I'm not sure how you'd use a keystroke to do something like ":%s/this text/that text/g" though, so command mode is extremely useful. Also, you're harping on type conventions? So what if it uses C-x vs CTRL-x and so forth? Besides, M-x or META-x is a more apt representation than ALT-x. (Oh, and macs have an ALT key too). Finally, before you "bet" some other inaccuracies, they refer to C-x when doing the actual config snippets since that's how it's written in the configs. but in actual text they use Ctrl-c... they even highlight it for you just for kicks.

CharlieSpencer
CharlieSpencer

And I'm not saying they should be the defaults. My point was only that the odds are excellent that they're on whatever system you approach, regardless of distro. Sure, you could carry your preferred editor around on a thumbdrive or install it off the web. Internet Explorer isn't the best default browser option for a Window system, but I KNOW it's going to be on any Windows system I work on. A Windows support tech should be familiar with it; he can't count on the browser of his preference being installed.

Neon Samurai
Neon Samurai

Debian includes it by default though it was needing a text based editor on Maemo that introduced me to it. Out of habit, I still like Joe (Joe's Own Editor) for simplicity of display and command use being habit for me now. Nano is a happy choice also though and usually my apt/source.list and ~/.bashrc editor before adding in Joe.