1. Improve vim responsiveness with better timeout settings

    Vim is typically fast, but it can be painfully slow at times, too.

    A great example of this is when you make a block selection, insert/append text, and then hit ESC to apply your action to the rest of your selection. It seems to take forever before you’re back in normal mode and can see your action applied to the selection.

    After digging into the issue, I learned that it doesn’t have to be this way.

    Vim has two settings that you let you control timeouts when working with map key sequences and terminal keycodes/escape sequences:

    1. timeoutlen defaults to 1000 and is the delay in milliseconds that vim uses when checking for map key sequences. It’s also used when checking keycodes if ttimeoutlen is disabled.

    2. ttimeoutlen defaults to -1 (disabled) and is the delay in milliseconds that vim uses when checking for keycodes.

    Out of the box, my vim was taking a full second to switch between insert and normal mode because ttimeoutlen was disabled; every hit of the ESC key caused vim to wait the full timeout in anticipation of a possible keycode.

    To get around this I added the following to my vimrc:

    " Adjust keycode timeout length
    set ttimeoutlen=100
    

    Since doing so, I’ve noticed that vim has been much snappier and even more of a pleasure to work with than usual.

    If you’re like me and use vim in tmux, it’s worth noting that I also had to adjust my tmux.conf to get the timeout settings above working as expected. tmux has its own built-in delay for escape sequences that needs to be tweaked.

  2. Alternate screen mode and annoying scrolling issues in iTerm2

    I recently switched to using vim inside of iTerm instead of running MacVim, and I was immediately annoyed by the strange scrolling behavior that happened while I was using vim.

    Accidental mouse scrolls into the scrollback buffer would reveal strange lines and pieces of the files I was working on. Clearing the scrollback buffer would help for a short period of time, but iTerm was determined to keep logging bits and pieces of my files and status line as I went about using vim.

    After googling around, I learned that the curses-based version of vim runs in alternate screen mode. Alternate screen mode lets an application draw to a different screen so that the state of your shell/prompt is preserved and can be restored when you quit that application. 

    This is how vim (and other curses-based applications) takes over your screen and then returns you back to your shell (just as you left it) when you exit the application.

    For whatever reason, iTerm saves lines from alternate screen mode and puts them into the scrollback buffer by default. Luckily, this can be disabled by a quick visit to the terminal tab within your profile settings:

    Disabling saving of lines to the scrollback buffer when in alternate screen mode

    Now, after I enter vim I can immediately clear the scrollback buffer (Menu item Edit > Clear Scrollback Buffer or Cmd-K), issue the vim redraw command in ex mode (:redraw!), and I’m all set for hours of happy hacking without any side effects from accidental mouse scrolling.

  3. Vim Clutch

    Earlier today a link was making rounds of how Aleksandr Levchuk modified a USB pedal to act as a toggle for Vim’s insert mode. Press the pedal to enter insert mode, and depress it to return to normal mode.

    When I think about all of the different types of inputs that we use to operate other technology (for example, driving a car), it surprises me that today was the first time I’ve read about anyone attempting to use alternative inputs beyond a keyboard and mouse to improve their coding efficiency.

    I’m not sure how useful the Vim Clutch really is in practice, but it certainly sounds like a fantastic idea.