Vim Scratch Plugin

The Vim Scratch plugin is a handy plugin that allows you to open “Scratch buffers”. These are buffers that Vim wont ask you to save, it’s just a temporary buffer that you can create and will be discarded when you quit Vim.

Once installed, you can open a scratch buffer with :Scratch. If you close the buffer and then run :Scratch again, the contents are remembered, but the contents will be lost when you close Vim.

I’ve been using this lots for taking quick notes when working on something, or to note something I need to revisit.

comments powered by Disqus… Click Here

Clearing the Search Highlight

I think it’s the biggest annoyance of any Vim-er. Highlighting something when I search for it is great, but a quick way to get rid of that highlight would be even better. I think today I finally found the perfect way to do it. This is one I picked up from Katrina Owen’s vimrc.

:nnoremap <CR:nohlsearch<cr" clear the search buffer when hitting return

Now once I’ve searched, the highlight is gone the next time I hit enter, which is brilliant.

comments powered by Disqus… Click Here

Repeating last command-line with `@:`

Once you’ve used Vim for a while you become accustomed to repeating the last change in normal mode with .. Being able to repeat actions easily lends itself to some efficient workflows and Vim also provides a way of repeating the last command-line action from normal mode using @:.

I find @: useful to repeat commands such as :cn / :cp to navigate the quickfix list, :bd to easily close a bunch of files and to repeat the last substitution.

See :help @: for more.

Guest post by Matt Walker (@_walkermatt).

comments powered by Disqus… Click Here

Searching again with //

If you’ve searched for something and want to repeat the search again, just use //.

For example, if you run:

/foo

To search forward for foo, and then later on you want to repeat this search, you can simply run:

//

This also works in substitutions. Say you’ve searched /foo and now want to replace it, there’s no need to type foo again:

:%s//bar/g

Will replace all instances of foo with bar (presuming /foo was your last search).

comments powered by Disqus… Click Here

Automatic Comment Prefixing Revisited

A while back, I wrote about removing Vim’s automatic comment prefixing. This behaviour is turned on by default, and is best shown with an example:

# some comment {cursor here}

If I was to hit o or O, to insert on the line above/below, or be in insert mode and hit enter, Vim would automatically add the comment prefix for me, so it would look like this:

# some comment
# {cursor here in insert mode}

Vim has automatically added the # for me.

I am not a fan of this, and in my original post I advised adding this line to your vimrc to fix it:

set formatoptions-=or

However, that stopped working for me (I’m not sure when – or if it’s related to a new Vim version). Today I managed to figure out the solution. This line does the trick:

autocmd FileType * setlocal formatoptions-=r formatoptions-=o

To be honest, I’m not sure why the original doesn’t work, and if anyone does, please leave a comment, as I’d be really interested to know why. If you’ve had the same problem, try the new line and see if that solves it.

comments powered by Disqus… Click Here

Stop Vim being slow when using rbenv

Note: I have now found a better solution to this problem, which I have documented. I suggest you use that solution over this one.

A lot of people, like me, use rbenv to manage Ruby versions on their machine. When I first set it up, I would see a large lag when opening a Ruby file in Vim. After a bit of Googling, I stumbled upon a solution.

Putting this into my .vimrc sped things up quite a bit:

let g:ruby_path = system('echo $HOME/.rbenv/shims')

If you’ve been struggling with Ruby slowness and are using rbenv, give that a go. If you use RVM, there’s a similar fix:

let g:ruby_path = system('rvm current')

comments powered by Disqus… Click Here

Sorting a selection

Today at work a colleague suggested ordering a hash of key / value pairs alphabetically to provide a bit of order. I agreed and then set about figuring out how to do it in Vim. This was the starting hash:

{
  "foo" =2,
  "bar" =3,
  "baz" =4,
}

Expecting some complex solution involving some form of regex searching, Vim surprised me with its built in sort functionality. All I did was highlight the keys within the object, and then hit :sort u:

{
  "bar" =3,
  "baz" =4,
  "foo" =2,
}

The u means duplicate lines will be removed. :help sorting has a great run down of all the options.

The Vim wiki has some useful other things it can do, too.

comments powered by Disqus… Click Here

Vim Startify

Today I found a handy little Vim plugin called Vim Startify.

It adds a small splash screen to the Vim intro screen that shows recently edited folders and also allows you to add bookmarks for files you commonly edit (your ~/.vimrc, perhaps).

I installed it this morning and so far it’s been really useful. It’s very basic and doesn’t add too much but the ability to hop to recently edited files (I’m forever loading Vim and finding the same file to edit) and also bookmarking files (in particular my Vim and ZSH configuration files).

comments powered by Disqus… Click Here

Live in-place Ruby execution

Today I was able to get Vim to execute Ruby files without ever having to leave Vim, and putting the results of execution in place next to the code that was executed. Here’s a quick demo on Youtube.

There’s three parts to this:

The rcodetools gem, which includes the CLI tool xmpfilter, which does the work.
The xmpfilter vim plugin which hooks up Vim to xmpfilter.
The relevant mappings in your vimrc to get it all working.
Installing the gem is simple:

gem install rcodetools

You then need to install the xmpfilter vim plugin. I recommend going down the Pathogen route for installing plugins, but do it whichever way you prefer.

There’s two parts to marking a line for execution. The first is to comment it with the # => syntax, which is what xmpfilter looks for. The second is then a command to run xmpfilter over the current file.

Here are the mappings I’ve gone for:

nmap <buffer<F4<Plug(xmpfilter-run)
xmap <buffer<F4<Plug(xmpfilter-run)
imap <buffer<F4<Plug(xmpfilter-run)
nmap <buffer<F3<Plug(xmpfilter-mark)
xmap <buffer<F3<Plug(xmpfilter-mark)
imap <buffer<F3<Plug(xmpfilter-mark)

Now I can hit F3 to mark a line to execute and then F4 to execute it.

Note: if you use rbenv to manage your Ruby versions like I do, you’ll need Tim Pope’s rbenv.vim plugin which tells Vim where your Ruby is located. Without this plugin I couldn’t get xmpfilter to work, presumably due to not being able to locate the rbenv installed version of Ruby.… Click Here

Vim’s `lcd` command

In Vim you can use :cd to change directory across your Vim session. But did you know you can use lcd to change directory for the current window?

I find this useful if I’m in one project and need to dig into some code from another. I will open a new tab and then lcd into place. Once I’m done I can then delete that tab and in my other window(s) I’m still in the directory I was earlier.

See :help lcd for more.

comments powered by Disqus… Click Here