Registers and the Clipboard

Recently I somehow ended up with set clipboard=unnamed in my .vimrc. I don't remember adding it, but I searched and removed it when I realised changes were overwriting my system clipboard contents. Here's what :help unnamed says, in case you're not familiar with this feature:

When included, Vim will use the clipboard register '*' for all yank, delete, change and put operations which would normally go to the unnamed register. When a register is explicitly specified, it will always be used regardless of whether "unnamed" is in 'clipboard' or not. The clipboard register can always be explicitly accessed using the "* notation.

Some people like set clipboard=unnamed. I find the "* register is way too frequently updated -- I prefer to think of the system clipboard as something that exists outside Vim, and keep Vim's registers as a separate bank of snippets.

Read More →

A Vim Clojure Toolchain

For those of us who like Lisp, Emacs is naturally compelling. But I always gravitate back to Vim. Therefore, whenever I see a Clojure/Vim article I find it very interesting -- how do people get that immediate REPL feel? How do you get parens balancing properly?!

Today I found My Clojure Toolchain: Vim by @venantius, and noticed the author also use Fireplace, which is the best Clojure REPL I know. The post goes into lots of detail about other Vim scripts that can help with Clojure development, from code traversal to linting.

Paredit.vim is another absolutely critical plugin to have for the Vim Clojure developer's toolbox ... Its true value lies in its support for what Emacs users refer to as "Slurpage" and "Barfage" - the ability to, with a keystroke or two, move existing arguments into or out of a given form.

Read More →

Indentation Motions

Here's a cool idea: indentation with motions. IndentWise by Jeet Sukumaran (GitHub: jeetsukumaran/vim-indentwise) provides motions based on indent depths or levels in normal, visual, and operator-pending modes.

For example, pressing [- moves to the previous line of lesser indent than the current line. The opposite is ]-, which moves to next line of lesser indent than the current line. It supports lots of other motions which are all documented, and configurable.

You can also move based on the absolute indentation level by providing a count argument, and block-scope boundary navigation is supported as well.

Read More →

Five Time-Saving Vim Tricks

Benjamin Klein, who wrote The VimL Primer, recently published a blog post entitled Save time in Vim NOW with these five weird tricks. I was looking for one weird trick, so I was very happy to receive five weird tricks. Anyway, one of them is about switching windows with CTRL and then a direction (hjkl), which saves a key press.

This is what the mapping looks like:

noremap <c-j> <c-w>j
noremap <c-k> <c-w>k
noremap <c-l> <c-w>l
noremap <c-h> <c-w>h

I've accidentally enabled this before when I've tried out .vimrc files from GitHub, or those Vim plugin packs aimed at beginners. For some reason I've never got used to it, I don't know if pressing CTRL and then a key with the right-hand just feels weird to me or what, but I find it tricky.

Read More →

Preview Images with image.vim

Sometimes I accidentally open a png or jpg with Vim, and it shows me a face-melting representation of the binary data. If you do this enough it gets annoying, so instead I've been using Ashish Anand's image.vim to render ASCII previews of images.


Converting images to ASCII is an old trick that you can do pretty easily on the command-line (at least it's easy in Linux), but the thing I liked about this plugin is it uses an autocmd to run whenever an image is opened:

Read More →

Cmd2: Fuzzy Search for cmdline

Command-line mode, where you enter commands with : and /, can be enhanced with gelguy's new plugin Cmd2.vim. It gives you fuzzy completion for search, which can be really useful if you've been searching for long strings in overly wordy projects. It also lets you easily get the line number and current word, which I always find fiddly.

Cmd2.vim works by behaving like a new submode -- it's more accurate to think of it as a framework for creating extensions that require command-line input. The fuzzy completion module which most people will want to try is actually an extension built on Cmd2.vim that is bundled with the plugin:

At a higher level, Cmd2 provides a framework to create extensions which require cmdline input. One such extension is Cmd2Complete which provides fuzzy completion for search in wildmenu style. The rendering of the UI and handling of the input is handled by the framework, but can be further customised - for example to create a CtrlP/Unite style menu instead.

Read More →

The Vim Regular Expression Dialect

If you look at the "pattern" help page, then you'll see this: "Vim's regexes are most similar to Perl's" which sounds great, until you continue reading to find "in terms of what you can do." One thing to keep in mind about Vim's regular expressions is they behave like other regular expression syntaxes that you might be familiar with, until they don't, in which case you start randomly escaping characters until your pattern works.

Sarcasm aside, you should think of regular expressions in Vim as a unique dialect. If something doesn't work, refer to :help pattern-searches. Here's an example: today I was converting a large Objective-C switch statement into a JSON file. I used a few regular expressions to slice and dice the entire file, and ended up with something close to what I wanted. I even managed to join every other line with :%normal J. I went from lots of these:

case ExampleTypeName:
  return @"TypeName";  

To JSON properties and values:

Read More →

Vim or Emacs?

Chris Penner is just starting out with Vim, and he's written an interesting article about his experiences: Vim Vs. Emacs?. I remember being in the same position, and the reason I stuck with Vim was the reason I could have stuck with Emacs: I didn't want to waste time learning a platform-specific editor. Also, Emacs and Vim work well in the command-line, which is great if you're an old school server-loving IRC nerd like me.

One of the beautiful things about creating vim commands or mappings is that it uses the same interface as normal editing. The mapping you need is exactly what you'd type inside the editor. This means that the more you learn in the main editor, the more customization options you unlock.

Emacs is also great (and far ahead of Vim) when it comes to doing more than one thing at once, and for being able to run and check code as you work on it.

Read More →

Script Roundup: vim-prosession, vim-dotoo

Dhruva Sagar sent me two plugins that he's written: VIM ProSession and VIM Do Too.

Vim ProSession (or vim-prosession) is an extension to Tim Pope's vim-obsession. The idea is to make it easier to manage multiple Vim sessions when Vim is started from a specific directory. It adds a command called :Prosession that completes session file names from the directory set by g:prosession_dir.

Vim Do Too

Read More →

File Navigation with the QuickFix List

If you need to deal with lists of files, then the QuickFix window is your friend. You can easily jump between files, then record and replay macros (complex repeat) across the contents of each file. Imagine that you're refactoring files by removing references to legacy code, but in some cases you can't just delete the code because you have to insert new and equivalent functionality. You can't quite search and replace because you need to make manual changes.

This is where the QuickFix list really helps. Even for simple edits I actually prefer it manipulating files in the shell because seeing a list of files is easier for me to work with. It copes with filenames with spaces, and I can press / to search within the list.

I've written about QuickFix before: Vim 101: QuickFix and Grep. In that article I said QuickFix is designed to display compiler errors, and this is true but I don't think many of us think about it like that anymore. We use it as a temporary list of files to flip through. Here's how you do that:

Read More →