Vim 101: A Gentle Introduction to Macros

You're working hard, and it's been a long day. You glance at the clock: 13:59. Is it still that early? Rushing to finish the latest bug fix, you accidentally hit some keys, then Vim displays an ominous message:

recording

Recording? Recording what?! Can I keep working or do I need to restart Vim? How do I make this thing go away!

I can understand some trepidation towards Vim's macro system, and a lot of people on IRC have asked me about it over the years. I stumbled with it a bit at first as well, back in the early days when I was struggling to install Red Hat 5.2 on my PC at university.

Read More →

Sharpening the Saw, Go and Vim

Sharpening the Saw

On sharpening the saw by Drew Neil is an article about the path to Vim mastery:

When you know Vim's capabilities, you’ll start to find yourself reaching for things that aren't there. You'll notice common patterns of usage, and realize that they could be streamlined if only Vim had an operator or motion for the job.

Vim has a tough learning curve, so it's sometimes hard to understand what to learn next. Drew's post includes some concrete examples documenting his path to Vim mastery, so it works well as a motivational piece to encourage beginners to start learning more.

Read More →

Script Roundup: Forms, Rooter

Send in your Vim scripts for review through our contact form or @vimnews.

Forms

Forms

Forms (GitHub: megaannum / forms) by Richard Emberson is a user interface library that works in both text mode and GVim. Once Forms and self.vim have been loaded, then all kinds of user interface elements can be created and managed.

Forms support labels, buttons, menus, and various layout styles. They can be used with the mouse or keyboard. The author has included lots of examples in the autoload/forms/example directory. This actually seems like a convenient way of managing plugins with complex options, so it'll be interesting to see if it catches on.

Read More →

Vim 101: Indentation

Some programmers are extremely sensitive about formatting. Perfectionists, sticklers, pedants -- call them what you will, they'll always be there to reformat entire source files to confuse an otherwise comprehensible Git history. Admittedly, I do like source files to be perfectly formatted, even down to whitespace. Even if you're completely relaxed about source formatting, you'll still need to master the basics of Vim's built-in indentation capabilities.

Configuring Indentation

The expandtab option causes Vim to insert with spaces instead of tabs. The exact number of spaces can be set using softtabstop. The shiftwidth should also be set because this controls how many spaces will be used with autoindent.

The average .vimrc might look like this:

Read More →

The Battle Against Emoji

Emoji

Mac OS X: Mountain Lion has been released, which comes complete with lots of new features and security improvements. One minor annoyance, particularly relevant to Vim users, is the handling of emoji glyphs. Emoji are pictographs often available on mobile devices that have started to bleed into Apple's operating systems.

Where the problems start is Apple's glyph substitution algorithm. Some fonts are incorrectly displaying glyphs from Apple Color Emoji.ttf, and this has been found to occur in text-based command-line applications, and affects both iTerm2 and the built-in Terminal app. Rather than getting the fancy characters Powerline uses, or modern zsh Unicode shell prompts, people are seeing weird and wonderful emoji glyphs instead.

Read More →

Script Roundup: seeks.vim, Gocode

seeks.vim

seeks.vim (GitHub: sanpii / seeks.vim) by Sanpi is an interface to the Seeks.fr search engine that can display results using a window inside Vim. It's built using the webapi-vim project, which supports lots of interesting web-related technologies.

Gocode

Gocode (License: MIT) by "nsf" is a daemon written with Go that provides context-sensitive autocompletion for various editors including Vim. It's designed to be fast, providing omni completion matching that would potentially perform quite slowly inside a typical editor:

Typical autocompletion time with warm cache is 30ms, which is barely noticeable.

Read More →

Vim 101: Undo

You'll notice experienced Vim users switch back into Normal mode as often as is practical. One reason for this is due to the way Vim tracks changes in a buffer. A change is a command entered in the Normal or Command-line modes, or edits made while Insert mode is active. Pressing u or typing :undo will revert the change. This command accepts an argument, so typing 3u will undo three changes. Typing CTRL-R or :redo will make the change again, and a [count] argument can be supplied to CTRL-R as well.

Branches

Undo branches (:help undo-branches) provide another way of managing sets of changes. Typing :undol will display them. Why is this useful? Well, imagine what happens when you undo something and then start editing again. In a linear undo system, this would effectively orphan the changes that occurred after edit resumed. With Vim's branch-based system, it's possible to get back to any given state more easily.

After making a number of changes and typing :undol, you'll see a list that looks like this:

Read More →

Memorising Shortcuts

I was playing around with ShortcutFoo, because it's free to try out without a registration, and I started to wonder: how do people memorise keyboard shortcuts? When using Vim it feels like there's always a more efficient way to do something, so it's natural to want to master as many shortcuts and commands as possible.

One good way to learn commonly used shortcuts is to occasionally run through vimtutor. Learning how to use the help system is a good idea as well, because unlike a lot of software's documentation Vim's help is impressively complete.

The VimGolf community is a great source of ideas about how to improve editing efficiency. A problem is suggested, then whoever has the solution with the lowest number of key presses is the winner. I like to read through these, but often the lowest number of key presses is a regular expression, and I probably wouldn't solve something like this using a regular expression in my daily work.

Read More →

Script Roundup: textobj-word-column.vim, refactor-rails.vim

Send in your Vim scripts for review through our contact form or @vimnews.

textobj-word-column.vim

textobj-word-column.vim by Jim Garvin adds extra text-objects:

  • ac: A column
  • ic: Inner column
  • aC: A COLUMN, based on aW
  • iC: Inner COLUMN, based on iW

These text-objects can be combined with other commands. For example, vic will visually select a column. The author has made an animated gif that demonstrates the plugin with some Ruby code.

refactor-rails.vim

refactor-rails.vim (GitHub: sandeepravi / refactor-rails.vim, License: Vim) by Sandeep Ravichandran is designed to help refactor Rails projects, and includes commands like these:

Read More →

Vim 101: What is the Leader Key?

You're happily using Vim, slowly mastering file navigation, split windows and tabs, search and replace, and then you discover a hot new .vimrc on Hacker News that includes lots of commands like this:

noremap <Leader>W :w !sudo tee % > /dev/null

What the devil is <Leader>? Well, in this example it simply means typing \W (backslash then w) in Normal mode will save the current file using the sudo command. Does that mean <Leader> means backslash? Well, not quite. The <Leader> key is a reference to a specific key defined by the mapleader variable. A lot of people change to comma because they find it easier to type:

let mapleader=","

The mapleader variable is easy to change, and if you always remember to map keys with <Leader> then you'll avoid confusing your own customisation with Vim's default keyboard shortcuts.

Read More →