Vim Macos

Posted on  by admin

Normally, on macOS, when you hold down a key, it will display a popup allowing you pick a special character if there are any assigned to that particular key. If a key doesn’t have any special characters behind it and you hold it down, nothing will happen. Introduction Vim which stands for vi improved is a text editor and an improved version of vi editor that is designed to run on CLI (Command Line Interface) as well as GUI (Graphical user interface). It was developed by Bram Moolenaar in 1991.

Vi is an one of two powerhouse text editors in the Unix world, the other being EMACS. While obtuse, vi is extremely powerful and efficient. There may be times when vi is the only text editor available, so it helps to at least know the basics.
On Mac OS X (and Linux), vi is symlinked to vim (vi improved), a more modern free software version. Vim It is the default editor when changing a crontab.
If you gave vi a whirl and don't see the beauty of it, give the nano editor a try. It also ships with Mac OS X.
Vim macos yank to clipboardnote: a chunk of this small guide came from a web page I found long ago, but I don't remember where so I can't give proper credit. I've added and changed things from the original text.

Vi has two modes, command and insert (really, three if you count replace mode). Command mode is used to navigate, search, and issue other commands. Insert mode is used to enter text.

Vi starts in command mode.

You can precede most commands with a number indicating how many times to perform a command. For example, entering 99 followed by the down arrow will move the cursor down 99 lines. '99x' will delete 99 characters.

While in command mode (case sensitive)
  • move the cursor with arrow keys; if there aren't any arrow keys, use j,k,h,l
  • i - change to insert mode (before cursor)
  • a - change to insert mode (after cursor)
  • A - change to insert mode (at end of line)
  • r - replace one character
  • R - overwrite text
  • x - delete one character
  • dd - delete one line
  • yy - yank line (copy)
  • p - paste deleted or yanked text after cursor
  • P - paste deleted or yanked text before cursor
  • G - go to end of the file
  • 1G - go to top of the file
  • J - merge next line with this one
  • / - search, follow / with text to find
  • :wq - write file and quit
  • :q! - quit without saving
  • %s/old/new/g - substitute; replace 'old' with 'new' on all lines
  • :g/pattern/d - delete all lines that match the pattern
While in insert mode
  • ESC - change to command mode
  • any text typed is entered at the cursor
Typical vi session
  1. Type 'vi file.txt' at command prompt
  2. Move cursor to where new text will be added
  3. Type 'i' to change to insert mode
  4. Type new text
  5. Type ESC to go back to command mode
  6. type ':wq' and ENTER to write the file and quit

Vim, Neovim, and MacVim can all exhibit slow scrolling in macOS. In some cases, the problem is OS-specific: key repeat settings can slow down scrolling with the j and k keys. Other times, the problem is really about Vim’s ability to render long lines with syntax highlighting.

Fear not! There are solutions to both problems.

Key Repeat Settings

There was always a marked difference between Vim on my Mac and Vim on Linux. Scrolling with j and k was blindingly fast on Linux, but plodded along on my Mac so slowly that I began using Control-F and Control-B most of the time.

It turns out there was a simple cause for the problem: key repeat settings. The default setting was too low for me.

Gvim

You can change Key Repeat settings in System Preferences -> Keyboard. Tweak the Key Repeat and Delay Until Repeat settings to find a good speed. I had to restart after changing these settings to see any effect.

Now, the vanilla options helped some, but in order to get really fast scrolling speed, I had to take two more steps:

  • Switch to iTerm 2 for my terminal. Terminal.app couldn’t cope with faster key repeat settings – Vim was still slow, though a bit faster.
  • Tweak the repeat values even more with KeyRemap4MacBook.

The magic KeyRemap4MacBook values for me were:

  • [Key Repeat] Initial Wait: 500
  • [Key Repeat] Wait: 35

Slow Rendering

Even with fast repeat settings, Vim can slow to a craw while scrolling through files that have long lines, when syntax highlighting is turned on. If you’re a programmer, that means a lot of files! And the problem affects more than just macOS.

There are three settings that helped this on my systems.

Vim Macos Insert

nocursorline

This setting had the most impact of the three. It turns off the bar that highlights the current line you’re on. That’s kind of a bummer, but if it means I can scroll through HTML and Ruby files, then I’ll take it.

Vim Macos Yank To Clipboard

lazyredraw

This does… something. You can read about it with help lazyredraw. Basically, it seems to limit the number of times Vim renders, which sped up scrolling for me.

synmaxcol

This is the nuclear option. It turns off syntax highlighting after a max column value, so lines longer than that abrubtly lose syntax coloring. I don’t use this setting anymore because nocursorline was more useful.

Wrap-up

There you have it. All the tricks I currently know for dealing with slow scrolling in Vim. Note that I did not mention using tmux over screen, or Neovim instead of Vim. Besides using iTerm instead of Terminal.app for the key repeat issue, no other changes in how I was running Vim helped.

I read that slow scrolling due to rendering long lines was less pronounced in GVim and MacVim than in terminal Vim, but that was not the case for me. Only adjusting the settings I mentioned helped!

Vim Macos Copy To Clipboard

Get my newsletter.

Mac Install Vim

You'll get an occasional email when I'm excited enough about a technology to write about it.