Vim settings for working with YAML

Having managed to avoid YAML until quite recently, my vim installation wasn’t well set up at all for working with it. It needs more config settings than plugins, so I thought I’d write down what I found helpful. I’m using it quite a lot now I’m working with OAS (used to be Swagger) API definitions. Continue reading

Vimdiff and Vim to Compare Files

At the moment I’m working on a tricky project where two similar projects diverged. Very similar things happened to them both, but not quite the same things – and now we’re merging the codebases to give us as much common code as possible. All this simply serves to set the scene of exactly what I was doing spending a whole day with large code diffs – I had to look up a few things so I thought I’d capture them while I can remember. Continue reading

Vim and HTML Tags with the Surround Plugin

Marking up documents is always tedious, and usually there are shortcuts available. My favourite document format tool is Pandoc, literally a box of document-conversion tricks! Today though I was not so lucky so I marked up a plain text file as HTML by hand … or rather, using my favourite vim tricks so I thought I’d share them.
Continue reading

Copy/Pasting and Vim

I’m a vim user and I somehow completely missed this excellent feature until much more recently than I care to admit! Usually vim has its own clipboard, but it doesn’t share with the operating system. You will need a vim-gtk install, this isn’t available in really basic vim (I’m a little unclear exactly on the dependencies).

To paste between vim and something else, use the + (plus) buffer in vim. It contains the contents of your system clipboard, and you can also write to it. If you’re not already using buffers in vim, then you should probably read the excellent documentation but for a very quick start:

  • To copy something into the buffer, select it in visual mode and type "+y
  • To paste from the buffer, type "+P

I had no idea how I’d missed this really fundamental trick, so I thought I’d share!

Working with R and Vim

I’m a long-time vim user, starting to use R for some of the data tasks I do and in a Coursera course I’m taking at the moment. RStudio is the tool for working with R, it runs on Linux and it’s really cool … but it’s awkward to work with a modeless editor when you’re used to vim, so I was looking for alternatives.

It’s possible to run R just from a prompt, which works well for individual commands but isn’t great for editing those commands or keeping track of what you did. Looking around, I found that there is (of course!) a Vim-R plugin available, so I gave it a try – and really liked it! It is enabled for files ending in .R or .Rmd and allows you to launch an R prompt and run one or many lines in that prompt directly from vim.

The .Rmd format is actually for R Markdown, which is a markdown format that lets you embed R. I’ve been using it as a sort of lab book to keep track of what I did and why. You can then generate a document with all the R code shown and evaluated – very neat!

Using Modelines in Vim

I’m working on a book at the moment, and there are strict guidelines on formatting. One thing which is tripping me up is that all code samples must use two spaces for indentation. I normally have my editor set to 4 spaces, so I was looking for a good way to keep just these files consistent in their layout. Enter vim’s modelines feature!

You may have seen this, a comment line at the start or end of a document which gives the settings for vim to use, something like this:

/* vim: set sw=2: */

This is a modeline in vim. I tried adding my own at the top of my sourcefiles, but vim seemed to ignore them. It turns out that modelines are disabled in vim by default (for security reasons) but you can easily turn them on in your .vimrc with:

:set modeline

The only change I want to make is the tab sizes which is why I use the line above. The vim modeline documentation is the best place to go if you want to add options of your own to this line

Creating Presentations with LaTeX

This spring/summer, I’m giving quite a few talks at conferences, and I have a number of my own clients that I’m writing new training materials for. That’s a lot of content in total and so, inspired by Dave’s article about LaTeX and powerdot (and with some help from Dave himself!), I’ve started to write my own presentations this way too.

Getting started was a struggle, I’ve never really used anything like it before and if there’s one thing LaTeX doesn’t do well, it’s error messages! The blog post I linked above has a sample presentation in it and I used that as my starting point. The source code goes in a file with a “.tex” suffix, e.g. presentation.tex. I then installed the texlive-latex-extra, latexmk, vim-latexsuite, latex-fonts-recommended and texlive-fonts-extra packages from aptitude, and generated a PDF by running:

latexmk -f -pdfps presentation.tex

Continue reading

256 Colours in Vim

Recently I’ve been looking at different vim colorschemes, after using vimdiff and finding I couldn’t see half of the code. I’ve been using relaxedgreen for ages, and it is excellent but I find the blue really hard to see on my black background so I was in the market for an alternative.

Touring Colours in Vim

Please excuse the all-over-the-place spelling in this post, I’m British so “colour” is a word and “color” is a vim command. Confused? Me too

I grabbed two fantastic scripts – the Vim Color Sampler Pack and the rather awesome ScrollColors plugin which allows you to cycle through all your colorschemes and try them out.

Continue reading

Set Vim Shift Width

I have had a problem with vim for a while. Although I have my tabstop set to 4, when I use >> or << to indent or undent (technical term) my code, it moves the code by 8 characters. As I'm now working with phpcodesniffer, I’m pretty motivated to get my editor set correctly so I don’t waste time on formatting.

The setting I want is the shift width. I added this in my .vimrc file:

set sw=4

Hopefully this will help someone else with the same issues. I’ve been grappling with the tab/space/indentation settings for vim as long as I’ve been using it and I don’t think I’m done yet. Maybe one day I’ll solve it and write a big overview but for now, you can read the previous installment and pass on any tips you may have via the comments!

As an aside, I now completely understand why projects have vim settings in their files – I’ve got different coding standards going on inside different projects so I’m spending a lot of time fiddling with .vimrc these days!

Vim Macro: cleaning up line endings

When development teams have people working on a variety of platforms, its pretty common to end up with wrong line endings. In vim these will look like ^M at the end of each line. To get rid of these line endings you can use the following command (in command mode)

:% s/^M$//

To type the correct ^M character, you’ll need to press Ctrl + V followed by Ctrl + M (the first combination means “take the next combination literally).

To turn this into a macro you should do the following. In command mode, pressq, followed by any letter. This will be the shortcut to access the macro. Then type the command as above. Finally, press q again to stop recording and its done. You can use your macro by pressing @ and then the letter you chose.