Developing Shaders in Vim

I’ve been recently writing shaders in Vim and found some fun shortcuts to help in development.

Frequently I’ll want both vertex and fragment shaders open in the same terminal window, but I’ll want Vim to have multiple windows. In the man pages, I found a simple command line switch to open both files, one on top of another. This is known as staked windows.

vim -o vert.glsl frag.glsl

But I like seeing the shaders side-by-side, especially if there are a bunch of varying variables, so use an uppercase “O” switch instead.

vim -O vert.glsl frag.glsl

Making it Pretty

I finally decided to add some color to vim, so I looked around for a syntax file for GLSL and found one at vim.org. Download and install it to get syntax highlighting.

So after starting vim, I like running these lastline commands to set the line numbers and get the nice GLSL syntax highlighting:

:set nu
:syntax on
:set syntax=glsl

Make sure there isn’t a space between “=” and “glsl”, otherwise you’ll get an “Unknown option” error.

Navigation

Now that the windows are all set up, you can start navigating between them and editing your shaders. To manipulate the windows you use CTRL-W which enters the window command mode, then add an extra character for what you want. For example, to toggle the cursor between the windows you can use CTRL-W w. This involves holding down CTRL, pressing ‘w’ letting go of both keys and pressing ‘w’ again. To close the current window, you can use CTRL-W q in the same way. You can of course still use the lastline mode commands :wq, or :q!

If you ever need to split up the windows further you have a few options. You can use CTRL-W s to split a window horizontally or CTRL-W v to split vertically. You can also go to lastline mode and type:

:split

Or if you want to split the current window vertically,

:vsplit

I found some of the more useful window command mode commands are these:
CTRL-W w – Go to the next window
CTRL-W q – Quit the current window
CTRL-W s – Split the current window horizontally
CTRL-W v – Split the current window vertically
CTRL-W n – Create a new empty window
CTRL-W = – Make all windows equal size

But better yet, start Vim and type :help windows which will give you more information than you’d ever want about windows!