Practical: Registers

Continuing with the trite university themed Vim 101 posts, I thought it would be useful to drop in the occasional practical lesson. Something for you to practice to develop good habits when using Vim.

One thing that tripped me up early on was registers. I managed to get used to yanking and putting text, but I kept overwriting the default register when I actually wanted to keep that content hanging around.

A few weeks ago I wrote about the black hole register, and I included a note about copying the default register. I believe that it's better to get used to thinking "should I yank this into a register?" before applying an operator command.

Read More →

Collaborative Vim

There are a few emerging solutions appearing for collaborating with Vim. The most obvious solution, which people are actually using for serious work, is to set up a server with your code, tmux, and Vim. In How We Use tmux for Remote Pair Programming, Joe Moore of Pivotal Labs talks about how to set this up and why he prefers this approach. Although it would be equally trivial to connect using GUI desktop sharing, the text-based approach has its own advantages:

Due to network latency GUI-based desktop screen sharing was intolerably slow for coding. tmux made network latency a non issue. My personal experience was that tmux + Vim was so fast when working remotely that it was usually indistinguishable from coding locally.

If you're remote pair programming, you could get a cheap virtual machine somewhere, set up tmux and Vim, then hand out ssh keys so people can connect to the same tmux instance safely.

Read More →

Script Roundup: vim-signify, vim-redl

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

vim-signify

Sy

vim-signify (GitHub: mhinz / vim-signify, License: Vim), or Sy, by Marco Hinz, uses signs to show linewise changes in a file. It works with lots of version control systems: git, mercurial, darcs, bazaar, subversion, cvs, and rcs (phew!). It's configurable, and has some clever navigation features -- g:signify_mapping_next_hunk and g:signify_mapping_prev_hunk can be set to jump between changed lines.

Other options allow the sign colours to be changed, and the plugin can be toggled per-buffer.

Read More →

Vim 101: Decoding Tab Titles

If you've managed to fit tabs into your workflow, then you've probably noticed curious numbers and signs that appear alongside the truncated filename. It's very easy to read this information once you know how: the number refers to the number of split windows in a tab, and the + means there is an unsaved file in the tab.

Decoding tabs

The tab bar itself is known as the 'tabline' (:help 'tabline'). If you executed :set tabline=hello then 'hello' would be displayed instead of the standard content. You can switch back to the default by typing :set tabline=.

Read More →

Polycephalous Plugins

Working in a modern command-line environment can offer several ways to manage multiple windows: Vim itself supports split windows and tabs, and many Vim users also use terminal multiplexers like tmux and GNU screen. However, there are those of us who also use a terminal that supports split windows and tabs, like iTerm2.

A new breed of Vim plugins can take advantage of all of these features to better support asynchronous tasks. For example, dispatch.vim by Tim Pope is a new one that can talk to tmux, screen, and iTerm to run time-consuming tasks in the background using Vim's standard compiler plugins. The intention is that you can asynchronously run test suites while continuing to work.

There's also Vitality by Steve Losh which enables Vim to communicate with iTerm2 and tmux.

Read More →

Script Roundup: eighties.vim, thwins

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

eighties.vim

eighties.vim (GitHub: justincampbell / vim-eighties, License: MIT) by Justin Campbell will automatically resize windows when switching between them. It calculates the size based on the longest line and takes into account signs -- the default width is 80 characters. It ignores file browsers, so it won't resize NERDTree.

The configuration options are as follows:

let g:eighties_enabled = 1
let g:eighties_minimum_width = 80 
let g:eighties_extra_width = 0 " Increase this if you want some extra room
let g:eighties_compute = 1 " Disable this if you just want the minimum + extra

thwins

thwins (GitHub: mitnk / thwins) "three windows" by Hugo Wang makes Vim display three windows -- one main window with two vertically split windows in the right column, and was inspired by dwm.vim.

Read More →

Terminology: Macro

In general terms, a macro is a reusable set of instructions. Intermediate and advanced Vim users use "complex repeats" to record typed characters into registers, then play back the register's contents later on. Most of us refer to complex repeats as macros, but Vim's documentation calls key mappings "macros":

1. Key mapping                *key-mapping* *mapping* *macro*

Key mapping is used to change the meaning of typed keys.  The most common use
is to define a sequence commands for a function key.

    :map  a=strftime("%c")

Contrast this to complex repeats, which are initialised by pressing q in Normal mode. I previously wrote about this in Vim 101: A Gentle Introduction to Macros.

Is it correct to call complex repeats macros, given that Vim's documentation distinguishes between them? Well, almost the entire community calls complex repeats macros, as they are technically macros in the broadest sense. However, it's useful to be aware that recording a sequence of characters to a register is referred to as a "complex repeat" by Vim's documentation, should any confusion arise when using both terms together.

Read More →

Medium and Hard Mode

In Habit breaking, habit making, Drew Neil mentions avoiding using motions that only move the cursor one row or column at a time. Hard Mode by Matt Parrott and Xeross takes this idea further by adding HardMode() to Vim which removes all character-wise navigation:

The philosophy behind Hard Mode is that you'll never master Vim's advanced motion and search functionality if you can fall back on the anti-pattern of fumbling around your code with the arrow keys.

That makes it an interesting way to force yourself to master a larger repertoire of motions, like fFtT and ,.

Read More →

Script Roundup: vim-expand-region, Git Support

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

vim-expand-region

vim-expand-region (GitHub: terryma / vim-expand-region, License: MIT) by Terry Ma allows visual selections to be expanded. Pressing + will expand the selection, and _ will shrink it.

The expansion is performed using text objects, and the text object list used by the plugin can be customised by passing a dictionary to expand_region#custom_text_objects -- the documentation includes an example of how to do this.

Git Support

Git Support by Wolfgang Mehner allows Git to be called from within Vim, and it also provides a menu for the commands supported by the plugin. It has some nice features like automatic folds for diffs and logs.

Read More →

The Black Hole Register

Giant Ring of Black Holes

Once you've got used to using the y command to yank text, it's easy to use the unnamed register when perhaps a named register would be more useful.

For example, typing yw will yank the word under the cursor into the unnamed register. Subsequent yanks or deletes will change this register, so it's easy to lose the text that was in there.

Read More →