CuteMachine

Normal And Global Commands With Vim For VSCode

The global command is a useful tool when working with Vim. Unfortunately, you cannot use the global command out of the box with the Vim integration for VSCode. You have to set it up first.

To enable the two commands, we need to install Neovim and then configure Vim For VSCode to use it.

You can install Neovim with the Homebrew package manager for macOS.

First, install the brew command on your Mac and then use the new command to install Neovim with brew install neovim. To install Homebrew, follow the instructions found on the Homebrew website.

After the installation process has finished, you can run nvim --version to ensure that Neovim has been installed successfully.

$ nvim --version
NVIM v0.4.3
Build type: Release

To configure Vim for VSCode, you first need to find out where the nvim command has been installed. In your Terminal, you can run type nvim to get that information.

$ type nvim
nvim is /usr/local/bin/nvim

The last step you need to complete is to add this information to the VSCodeVim configuration in settings.json.


    "vim.enableNeovim": true,
    "vim.neovimPath": "/usr/local/bin/nvim",

Example of using the global command

Imagine you have a huge Markdown file, and you want to delete everything except the headings to compile a table of contents.

# Heading One

A random amount of text.

## Subheading One

More random amount of text.

## Subheading Two

Even more random amount of text.

## Subheading Three

End of text.

So the following would be the result we wish to get:

# Heading One
## Subheading One
## Subheading Two
## Subheading Three

With the global command, you would type :global!/^#/d.

You can abbreviate the global command to just g: :g!/^#/d.

The : enters the command line mode in vi. The command line mode is used to enter Ex commands. The global command is an Ex command.

The ! after global means that we want to invert the pattern. You enter the pattern where the command should match between the slashes. And the d is the standard delete command you know from using vim.

Posted on CuteMachine in vscode and vim.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer