Bryan Grohman

All Writing

Vim Workspaces


If you've used an IDE for coding, you'll notice that you can maintain multiple workspaces to organize different projects. This lets you set up your workspace once per project and then open it where you left off in the future without adjusting your IDE's configuration.

Vim doesn't have a built-in workspace feature, but it's easy to add it with a custom plugin.

Here's what we want our workspace plugin to do:

  1. Set the current working directory to the root of our project.
  2. Set up paths for searching based on the project root.
  3. Configure any other project-specific Vim settings.

You can add this directly to your vimrc file, but I prefer to keep my workspace configuration in a separate ~/.vim/plugins/workspaces.vim file instead.

Here's an example for my Vim workspace:

function! workspaces#Vim()
    let repo_base_path = "~/.vim"
    execute "cd " . repo_base_path
    execute "set path=.," . repo_base_path
    execute ":e " . repo_base_path . "/vimrc"

command! WorkspaceVim call workspaces#Vim()

When I run the :WorkspaceVim command, Vim will switch directories to my ~/.vim directory, set the path accordingly, and open my vimrc file for editing.

For some projects, setting the path variable is particularly useful so that the built-in Vim search commands like :find only operate on the relevant directories within the project and exclude paths that contain your project dependencies (e.g. node_modules when using Node), build artifacts, or other miscellaneous directories.

When combined with Vim's session management features, your own custom workspace plugin will make it easier to save your project configuration.