Vim For Macos



In this blog post I'm going to show how to set up Vim for easier YAML editing.

Vim macos copy to clipboard

You can scroll down to the end for a summary of all installed plugins and config file changes.

Syntax Highlighting

There's not much to do here. VIM has YAML syntax highlighting built-in and it's great.

If you wonder what are Vim’s options and how to display their values, I wrote about it in the first article of this series. There are also read-only marks in the range 0-9. They are only available when using a viminfo file (for Vim) or a shada file (for Neovim). Installing MacVim on Mac OS X is just like installing GVim on Linux, at least in principle: you get both a regular GUI application and a CLI executable. MacVim's CLI executable is 'hidden' in the.app bundle, though, but you can use it directly via an alias or use the bundled mvim script that you are supposed to put in your $PATH. For Ubuntu, this can be installed from the Ubuntu repo. For macOS, gdb will have to be code signed, and I’ve found that gdb versions earlier than 8.3 are problematic (tested on macOS Mojave). I haven’t tested debugging on Windows. Before trying to debug with vim, I suggest verifying that gdb works as expected on your system. A command line version of Vim for macOS is builtin into the OS distribution. For example git on macOS uses Vim by default to prompt for writing a commit message. Vim has a long history since 1990-s and a well-established fan base. It is known for stability, high backwards compatibility and ubiquity. Vim is highly customizable and extensible.

VV is a Neovim client for macOS. A pure, fast, minimalistic Vim experience with good macOS integration. Optimized for speed and nice font rendering. Fast text render via WebGL. OS integration: copy/paste, mouse, scroll. Fullscreen support for native and simple (fast) mode. All app settings configurable via vimscript. Command line launcher.

There's one thing I want to mention though. A few years back YAML highlighting in Vim was very slow, and there was often a noticeable lag when opening large files. The workaround was to use the vim-yaml plugin for fast syntax highlighting.

Not sure if it's still worth installing I decided to make a performance benchmark. I loaded up a large YAML file (6100 lines) and compared the time:

As we can see the default syntax highlighting is just as fast as the plugin and there's no need to install a separate plugin to fix the slow syntax highlighting anymore.

Indentation

Indentation probably the most annoying part about editing YAML files. Large documents with deeply nested blocks are often hard to track and errors are easily made.

Vim macos gui

YAML documents are required to have a 2 space indentation. However, Vim does not set this by default but it's an easy fix by putting the following line in the vim config:

We can also setup Indentation guides. Indentation guides are thin vertical lines at each indentation level and useful to help line up nested YAML blocks.

Vim Macos Copy To Clipboard

We can display those lines by using the indentLine plugin. I've modified the indentation character to display a thinner line (default is '¦'):

The result should look like this:

Folding

With folding we can hide parts of the file that are not relevant to our current task.

Vim has built-in support for folding based on the indentation level but the default folding rules make it hard to tell what is folded. This is because the folding starts on the line following the start of a block. To change this we can install the vim-yaml-folds plugin.

Here's a side-by-side comparison of the default folding (left) compared to vim-yaml-folds (right):

To work with folding we need to remember a few keyboard commands. Vimcasts has a great episode on this here. Most of the time I use the following commands:

  • za: Toggle current fold
  • zR: Expand all folds

After the plugin is installed and folding is enabled the default settings will fold all blocks by default. To start with unfolded content we can set:

There's also a plugin called restore_view which will save the folds for each file. But be aware that this plugin will create an extra file with folding information for each opened document. Minitool partition wizard gpt.

Linting

Linting will analyze the code and show any potential errors while we're writing it which helps us catch formatting or syntax errors early on.

To do this in Vim we can use ALE, an asynchronous linting framework that has support for many languages and tools including YAML. To enable YAML linting in ALE we have to install yamllint, a Python-based YAML linter.

Installation instructions are here. On macOS we can install it with Homebrew:

The default configuration is fairly strict and shows errors in document style such as line length, trailing spaces or comment indentation.

We can modify the configuration to be less strict. Yamllint already comes with a relaxed version of the default config that is a good starting point. The only additional thing I've decided to disable is line length checking.

To do this we open up ~/.config/yamllint/config and paste the following:

I've modified the ALE configuration to change the message format, error symbols and only lint when the file is saved: Is jettison for mac safe.

We can see the errors and warnings on the left side:

Summary

Here's a summary of the plugins, applications and config modifications:

Vim Plugins

Vim Macos Gui

Applicatins

Config

In ~/.vimrc or ~/.config/nvim/init.vim

In ~/.config/yamllint/config

The new Dark Mode in macOS Mojave is a nice addition and is – especially inthe night hours — more pleasing to your eyes than the light mode.

However, enabling Dark Mode will not change the Terminal profile, which isa little bit annoying – especially if your color theme has a light and a darkvariant (like the infamous Solarized, Snow, One, or my own Rasta theme).

If you change your Terminal profile to something dark, Vim still doesn’t lookright because it uses its own mechanism for light/dark backgrounds (see :help'background' for details) and doesn’t know about the changes you made to theTerminal profile.

If you execute :set background=dark in Vim (and if you color schemesupports it), Vim looks nice and dark now, too.

However, on the next day, the fun begins again when you want to switcheverything back to light mode …

Wouldn’t it be nice if this could all be accomplished with a single command?

Commands

There are tools, that help you with switching to/from macOS Dark Mode (e.g.,NightOwl or Shifty), but they can’t change your Terminal profile or notify Vim.

As it turns out, it’s not too hard to implement a little program that doesexactly this:

  • You can uses the defaults command to get the current macOS Dark Theme mode:

  • You can use AppleScript (oh, how I love this language …) to set Dark Mode andupdate the Terminal profile:

  • You can wrap both things with a Python script:

  • Apowersoft screen capture pro license code. You can use the timer_start() function introduced in Vim 8 and neovim toregularly check for the current Dark Mode settings. Put this into your Vim config:

  • You can create an Automator action that runs the Python script and thatcan be activated with a global shortcut. I use ⌥⌘D (you need todeactivate this shortcut for showing/hiding the Dock first). This is theAppleScript I used:

Best Vim For Macos

The drawback of this method is that the current application (at the time youpress ⌥⌘D) is used as “source” of the action you get two dialogs asking youto give that app permissions to remote control the System Settings and Terminal.

Mac Vim Gui

A better solution would be if the authors of NightOwl and Shifty wouldintegrated this into their tools. I’m gonna contact them and see what happens. :-)

Update:

Gvim For Mac

MacVimgot anOSAppearanceChanged event that is emitted every time MacVim changes its appearance.

Vim Macos Insert

Thanks to Frank for the heads up!