Instantly share code, notes, and snippets.

Embed
What would you like to do?
18F Tech Talk Lite! 2017: How to Vim Everywhere. Or, how I abstracted away my workflow and can't use regular computers anymore

Preface

Hey there folks, I'm Roger and I use the terminal for just about everything text related. I really like using a keyboard over a mouse.

Let's begin our journey into my workflow with two things in mind. First off, there is very little research here in terms of actual productivity gains. I did this mostly for fun.

This is just how one person decided to hit the dopamine part of their brain almost two years ago when I was bored out of my skull and felt like I needed a challenge.

And lastly, whenever talking about editor preference – and Vim – it's irresponsible to not have a touch of humility and levity about the whole thing.

While I have never personally used Emacs, I have heard a lot of good and interesting things about it. It’s a really powerful text editor nearing an environment. With all the cruft I'm about to show you in my Vim setup, I think I could have had a similar workflow in Emacs with minimal effort.

How to Vim Everywhere

.               .
|-. ,-. . , ,   |- ,-.   .  , . ,-,-.
| | | | |/|/    |  | |   | /  | | | |
' ' `-' ' '     `' `-'   `'   ' ' ' '

                           .
,-. .  , ,-. ,-. . . . , , |-. ,-. ,-. ,-.
|-' | /  |-' |   | | |/|/  | | |-' |   |-'
`-' `'   `-' '   `-| ' '   ' ' `-' '   `-'
                  /|
                 `-'

Or, how I abstracted away my workflow and now can't use regular computers anymore. This talk focuses on my journey into working with Vim and what that means to me. Your mileage may vary on hot button subjects such as this so I encourage you to take everything I say here with tongue firmly planted in cheek.

I can still use most computers, but if you let my five minute shell script run on your Linux-flavored computer, and I can bring my own keyboard, I'll be happier while using it.

It hadn't always been like this. My absolute favorite text editor used to be BBEdit. Using it back in the 90s on my Indigo, later Ruby, iMac to write AppleScript files and CSS for MySpace and LiveJournal themes. It wasn't until I began writing software for a living that I found it harder and harder to justify using a text editor without plugins.

When the world's your oyster, use Vim to open it up

 ,ggg,         ,gg                                  ,ggg,
dP""Y8a       ,8P                                  dP""8I
Yb, `88       d8'                                 dP   88
 `"  88       88  gg                             dP    88
     88       88  ""                            ,8'    88
     I8       8I  gg    ,ggg,,ggg,,ggg,         d88888888   gg    gg    gg    ,ggg,     ,g,       ,ggggg,     ,ggg,,ggg,,ggg,    ,ggg,
     `8,     ,8'  88   ,8" "8P" "8P" "8,  __   ,8"     88   I8    I8    88bg i8" "8i   ,8'8,     dP"  "Y8ggg ,8" "8P" "8P" "8,  i8" "8i
      Y8,   ,8P   88   I8   8I   8I   8I dP"  ,8P      Y8   I8    I8    8I   I8, ,8I  ,8'  Yb   i8'    ,8I   I8   8I   8I   8I  I8, ,8I
       Yb,_,dP  _,88,_,dP   8I   8I   Yb,Yb,_,dP       `8b,,d8,  ,d8,  ,8I   `YbadP' ,8'_   8) ,d8,   ,d8'  ,dP   8I   8I   Yb, `YbadP'
        "Y8P"   8P""Y88P'   8I   8I   `Y8 "Y8P"         `Y8P""Y88P""Y88P"   888P"Y888P' "YY8P8PP"Y8888P"    8P'   8I   8I   `Y8888P"Y888

Vim has a great community of people using it all over the world. There's also a huge amount of plugins that solve the same problems I was trying to solve when I switched over to Sublime Text. While it's not an integrated development environment (IDE), it can very well handle any file you want to throw at it. For example here's a list of some plugins I have installed at one point or another with a popular package manager for Vim called Vundle

    Plugin 'mattn/emmet-vim'
    Plugin 'applescript.vim'
    Plugin 'markcornick/vim-terraform'
    Plugin 'rust-lang/rust.vim'
    Plugin 'ledger/vim-ledger'
    Plugin 'scrooloose/nerdtree'
    Plugin 'vim-airline/vim-airline'
    Bundle 'edkolev/tmuxline.vim'
    Plugin 'ctrlpvim/ctrlp.vim'
    Plugin 'ryanoasis/vim-devicons'
    Plugin 'valloric/YouCompleteMe'

Find more plugins on the awesome VimAwesome site.

Tidy Vim resource files

Organizing your Vim resource file, ~/.vimrc, is just as important as picking the right plugins. The thing about Vim is that it's really powerful, but certain features are either off by default or need to customized for your personal preference. For instance, it won't highlight search results or show what line your on. Syntax highlighting needs to be turned on by default as well. The stock version of Vim you get when you boot it up is very Spartan, but that didn't stop me from layering bundle after plugin.

When I started using Vim, I spent about a week tweaking my resource file myself until I found The Ultimate Vim Distribution which I borrowed tons of things from and put into my own configuration file.

Since then, I've been tweaking it regularly and organizing it using manual folds which allow you to collapse sections of a file to focus on just the section you want to work on. You can check out my resource files in my ~/.files repository.

Terminal multiplexers

One of the things I knew I would really miss from using Vim is creating projects for various things I work on at any given time. I like to keep projects and files strewn about like dandelion seeds waiting for a light gust of wind. Since the version of Vim I wanted to use was Terminal-based rather than GUI-based, I also didn't want to leave a bunch of Terminal windows open on my machine. My previous workflow with Sublime Text allowed me to save project workspaces that included multiple directories all within a single window.

Enter tmux, the terminal multiplexer. It's a virtual terminal inside your terminal. It's like inception for your terminal-based workflows. It also allows you to use vim-like bindings so you can use it to help you solidify the learning of Vim commands. Also since it saves state when you detach from sessions, you can easily context switch across entire projects or tasks at work without losing all your files or terminal settings for that particular setup.

  ~/Developer/eighteenf/tech-talks
    tmux list-sessions -F "#S"
18f-cloud-gov
18f-facilitator
18f-general

Above is a list of tmux sessions I keep to track all the various things I do at work.

Learn by doing and forcing yourself to do

You're not going to get anywhere fast until you let Vim swallow everything around you.

Make sure your computer knows that your default editor is Vim.

export EDITOR=$(which vim)

Use your terminal a lot? Make it act like vi.

set -o vi # Use vim keybindings in copy mode

Using tmux? Command it like vi while managing sessions.

setw -g mode-keys vi # Use vim keybindings in copy mode

Use GitHub in a new way with vi-like shortcuts.

Better, Farter, Smarter

.-,--'        .      ,.         .  .-,--.         .                ,--,--'      .
 \|__ . ,-. ,-|     / |   ,-. ,-|   `|__/ ,-. ,-. |  ,-. ,-. ,-.   `- | ,-. . , |-
  |   | | | | |    /~~|-. | | | |   )| \  |-' | | |  ,-| |   |-'    , | |-'  X  |
 `'   ' ' ' `-^  ,'   `-' ' ' `-^   `'  ` `-' |-' `' `-^ `-' `-'    `-' `-' ' ` `'
                                              |
                                              '

Finding and replacing text (FART) is one of the best ways to work. Sublime Text — and Atom too — is really great for this. One of my initial fears of switching to Vim is that I'd have to do all these stuff manually and it would be time consuming. But I quickly realized that I had the same workflow just with a bit more methodical friction.

:%s/findThis/andReplaceItWithThis/g

:'<,'>s/findInTheSeclection/andReplaceItWithThis/g

And when I want to find across an entire directory, I can leverage lvim to find across multiple files by directory path.

:lvim /patternToMatchAcrossAllFiles/ **/*

And if things get too slow, prefix the command with noautocmd in order to disable all your Vim plugins when searching through files.

:noautocmd lvim /patternToMatchAcrossAllFiles/ **/*

Moving around in a Vim world

 ,ggg, ,ggg,_,ggg,
dP""Y8dP""Y88P""Y8b                      I8        ,dPYb,                     ,dPYb,         ,dPYb,
Yb, `88'  `88'  `88                      I8        IP'`Yb                     IP'`Yb         IP'`Yb
 `"  88    88    88                   88888888     I8  8I             gg      I8  8I         I8  8I
     88    88    88                      I8        I8  8'             ""      I8  8bgg,      I8  8'
     88    88    88   ,ggg,    ,ggg,     I8        I8 dPgg,           gg      I8 dP" "8      I8 dP
     88    88    88  i8" "8i  i8" "8i    I8        I8dP" "8I          8I      I8d8bggP"      I8dP
     88    88    88  I8, ,8I  I8, ,8I   ,I8,       I8P    I8         ,8I      I8P' "Yb,      I8P
     88    88    Y8, `YbadP'  `YbadP'  ,d88b,     ,d8     I8,      _,d8I     ,d8    `Yb,    ,d8b,_
     88    88    `Y8888P"Y888888P"Y88888P""Y88    88P     `Y8    888P"888    88P      Y8    8P'"Y88
                                                                    ,d8I'
                                                                  ,dP'8I
                                                                 ,8"  8I
                                                                 I8   8I
                                                                 `8, ,8I
                                                                  `Y8P"

One thing that people have a hard time with Vim is the modes. And the default Normal mode which uses h, j, k, and l as the arrow keys everyone sees on their keyboards. At first it might seem dated that these keys are used to move around, but with enough practice you realize that it's all part of the idea that your typing fingers shouldn't stray too far away from the main row.

 ____            ____            ____            ____
||h ||          ||j ||          ||k ||          ||l ||
||__||          ||__||          ||__||          ||__||
|/__\|          |/__\|          |/__\|          |/__\|

h allows you to move left, j allows you to move down, k allows you to move up, and l allows you to move right. It took me a little bit of time to get used to it, but now I actually expect this behavior from anything with a blinking cursor. Which means I end up typing a lot of jjjjklhjlllllll sometimes without realizing it.

These aren't the only ways to move around in Vim though. There are much more powerful ways to move up and down for instance.

 ____  ____            ____  ____
||^ ||||e ||          ||^ ||||y ||
||__||||__||          ||__||||__||
|/__\||/__\|          |/__\||/__\|

control + e will move the page up but keep your cursor in the same place. It's useful when you want to read a few more lines below. control + y will move the page down but keep your cursor in the same place. It's useful too but for the opposite direction.

 ____  ____            ____  ____
||^ ||||d ||          ||^ ||||u ||
||__||||__||          ||__||||__||
|/__\||/__\|          |/__\||/__\|

control + d will move the page and your cursor a half page down. control + u will move the page and your cursor a half page up.

All these commands are the most useful when you're ready source code rather than editing it. And the great part of it all is that if you do have to edit something, you're hands are right there on the home-key row ready to start punching up, or punching down, your text file.

Leaving your mark and making sure you're able to too!

                  .-'''-.
                 '   _    \
               /   /` '.   \              .                                .--.           .
       _     _.   |     \  '            .'|                         _     _|__|         .'|
 /\    \\   //|   '      |  '.-,.--.  .'  |                   /\    \\   //.--.     .| <  |
 `\\  //\\ // \    \     / / |  .-. |<    |                   `\\  //\\ // |  |   .' |_ | |
   \`//  \'/   `.   ` ..' /  | |  | | |   | ____                \`//  \'/  |  | .'     || | .'''-.
    \|   |/       '-...-'`   | |  | | |   | \ .'                 \|   |/   |  |'--.  .-'| |/.'''. \
     '                       | |  '-  |   |/  .                   '        |  |   |  |  |  /    | |
                             | |      |    /\  \                           |__|   |  |  | |     | |
                             | |      |   |  \  \                                 |  '.'| |     | |
                             |_|      '    \  \  \                                |   / | '.    | '.
                                     '------'  '---'                       .---.  `'-'  '---'   '---'
   .                                 __.....__                         .--.|   |      __.....__
 .'|                     .--./)  .-''         '.                  _.._ |__||   |  .-''         '.
<  |                    /.''\\  /     .-''"'-.  `.              .' .._|.--.|   | /     .-''"'-.  `.
 | |                   | |  | |/     /________\   \             | '    |  ||   |/     /________\   \
 | | .'''-.     _    _  \`-' / |                  |           __| |__  |  ||   ||                  |    _
 | |/.'''. \   | '  / | /("'`  \    .-------------'          |__   __| |  ||   |\    .-------------'  .' |
 |  /    | |  .' | .' | \ '---. \    '-.____...---.             | |    |  ||   | \    '-.____...---. .   | /
 | |     | |  /  | /  |  /'""'.\ `.             .'              | |    |__||   |  `.             .'.'.'| |//
 | |     | | |   `'.  | ||     ||  `''-...... -'                | |        '---'    `''-...... -'.'.'.-'  /
 | '.    | '.'   .'|  '/\'. __//                                | |                              .'   \_.'
 '---'   '---'`-'  `--'  `'---'                                 |_|

I sometimes work with huge files. This can lead to a lot of frustrating context-switching even though the task is the same. I find myself jumping around files sometimes and forget what the name of something is that's defined way at the top. I set markers to any key on my keyboard with the power of Marks, :h mark. This is a super convenient way to jump back and forth between ephemeral sections I create when I'm working with 2000 line YAML files.

Another convenience is that when working with huge files and having plugins enabled can really slow down your editing or even viewing of a file. That's where :noautocmd comes in handy. If you have a file that's really slowing down a plugin, and you're not sure which plugin it is or how to disable it, you can just reload the current file with the noautocmd prefix like so :noautocmd e.

Understanding the command-mode

Vim's command-mode is a little tricky to understand. It took me a while to realize two things about it.

  1. How to activate it and deactivate it.
  2. How to actually use it instead of simply getting frustrated at it.

You activate command-mode by typing q:. Notice that the colon is on the right and not the left side of the q. This pops up the Command Line buffer which is really similar to a backtrace when you press control + r in your terminal to search through your previous commands. The major difference here is that instead of just searching through it, this is yet another buffer so you can manipulate it with all the power of Vim modes.

I can combine my previous two commands using | and edit a command that didn't work the way I thought it would before running them again. I can even run commands in the Terminal instead of just within Vim which helps me stay in the flow even though I may be context switching.

Once a command is ready to be executed, I hit return on the line I want to run and Vim will run it as if you typed a : in front of it.

To quit out of this buffer, it's the usual suspect – :q.

oh no! CAPS LOCK WAS ON

 ______   ______   ______  ______   __       ______   ______   __  __
/\  ___\ /\  __ \ /\  == \/\  ___\ /\ \     /\  __ \ /\  ___\ /\ \/ /
\ \ \____\ \  __ \\ \  _-/\ \___  \\ \ \____\ \ \/\ \\ \ \____\ \  _"-.
 \ \_____\\ \_\ \_\\ \_\   \/\_____\\ \_____\\ \_____\\ \_____\\ \_\ \_\
  \/_____/ \/_/\/_/ \/_/    \/_____/ \/_____/ \/_____/ \/_____/ \/_/\/_/

 __    __   ______   __  __   ______       __  __   ______   __  __
/\ "-./  \ /\  __ \ /\ \/ /  /\  ___\     /\ \_\ \ /\  __ \ /\ \/\ \
\ \ \-./\ \\ \  __ \\ \  _"-.\ \  __\     \ \____ \\ \ \/\ \\ \ \_\ \
 \ \_\ \ \_\\ \_\ \_\\ \_\ \_\\ \_____\    \/\_____\\ \_____\\ \_____\
  \/_/  \/_/ \/_/\/_/ \/_/\/_/ \/_____/     \/_____/ \/_____/ \/_____/

 __       ______   ______   ______       ______   ______   __   __   ______  ______   ______   __
/\ \     /\  __ \ /\  ___\ /\  ___\     /\  ___\ /\  __ \ /\ "-.\ \ /\__  _\/\  == \ /\  __ \ /\ \
\ \ \____\ \ \/\ \\ \___  \\ \  __\     \ \ \____\ \ \/\ \\ \ \-.  \\/_/\ \/\ \  __< \ \ \/\ \\ \ \____
 \ \_____\\ \_____\\/\_____\\ \_____\    \ \_____\\ \_____\\ \_\\"\_\  \ \_\ \ \_\ \_\\ \_____\\ \_____\
  \/_____/ \/_____/ \/_____/ \/_____/     \/_____/ \/_____/ \/_/ \/_/   \/_/  \/_/ /_/ \/_____/ \/_____/

So at this point I started worrying about carpal tunnel then modified my keyboard muscle memory, and actual keyboard preferences, to leverage caps lock as control to give my left pinky a rest. This made me think that the original control button might still be useful as caps lock. Which hasn't worked out so well for me.

I found Vim to be acting really weird when you're trying to use commands, only to realize that caps lock was on. Because every key on your keyboard is a command to Vim, when caps lock is on it changes the context of all the commands. And some of the commands do similar but destructive things to your files!

 ____            ____            ____            ____
||J ||          ||K ||          ||j ||          ||k ||
||__||          ||__||          ||__||          ||__||
|/__\|          |/__\|          |/__\|          |/__\|

For example, j and k will move the cursor up and down a file one line at a time. But, J and K will move the link up and down a file one line at a time. It might not be what you expected.

 ____            ____
||Q ||          ||q ||
||__||          ||__||
|/__\|          |/__\|

Another caps lock offender is q. Regularly you can hit q without much of a problem. But, hitting Q will begin Ex mode which lets you type Vim commands without the : prefix. I generally don't use it but you can find out more information about it from the documentation, :h Ex-mode.

Funny enough, this actually helped me learn more about Vim because I would say to myself, "What the heck was that?" or "How do I do that again?"

What's stranger than friction? Productivity

          ▄▄▄▄▄      ▄▄▄▄▀ █▄▄▄▄ ██      ▄     ▄▀  ▄███▄   █▄▄▄▄
         █     ▀▄ ▀▀▀ █    █  ▄▀ █ █      █  ▄▀    █▀   ▀  █  ▄▀
       ▄  ▀▀▀▀▄       █    █▀▀▌  █▄▄█ ██   █ █ ▀▄  ██▄▄    █▀▀▌
        ▀▄▄▄▄▀       █     █  █  █  █ █ █  █ █   █ █▄   ▄▀ █  █
                    ▀        █      █ █  █ █  ███  ▀███▀     █
                            ▀      █  █   ██                ▀
                                  ▀
   ▄▄▄▄▀ ▄  █ ██      ▄      ▄████  █▄▄▄▄ ▄█ ▄█▄      ▄▄▄▄▀ ▄█ ████▄    ▄
▀▀▀ █   █   █ █ █      █     █▀   ▀ █  ▄▀ ██ █▀ ▀▄ ▀▀▀ █    ██ █   █     █
    █   ██▀▀█ █▄▄█ ██   █    █▀▀    █▀▀▌  ██ █   ▀     █    ██ █   █ ██   █
   █    █   █ █  █ █ █  █    █      █  █  ▐█ █▄  ▄▀   █     ▐█ ▀████ █ █  █
  ▀        █     █ █  █ █     █       █    ▐ ▀███▀   ▀       ▐       █  █ █
          ▀     █  █   ██      ▀     ▀                               █   ██
               ▀

I am my most productive in Vim because of a lot of it's shortcuts and home-row focused commands. It's a lot to keep in my head though, and I have realized that because of all the repetitive key strokes I have actually developed both muscle memory and meticulous workflows when working on text files. Some of these workflows sometimes include double-checking and triple-checking my work.

With more friction comes more focus on what you're trying to do. You find yourself actually reading source code, because moving around is so methodical you will become more methodical about simply moving around. You find yourself thinking about the actual steps in a problem because editing, saving, reviewing the file become parts of that process.

That's all folks

=====================================================================
████████╗██╗  ██╗ █████╗ ███╗   ██╗██╗  ██╗███████╗
╚══██╔══╝██║  ██║██╔══██╗████╗  ██║██║ ██╔╝██╔════╝
   ██║   ███████║███████║██╔██╗ ██║█████╔╝ ███████╗
   ██║   ██╔══██║██╔══██║██║╚██╗██║██╔═██╗ ╚════██║
   ██║   ██║  ██║██║  ██║██║ ╚████║██║  ██╗███████║
   ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝

███████╗ ██████╗ ██████╗
██╔════╝██╔═══██╗██╔══██╗
█████╗  ██║   ██║██████╔╝
██╔══╝  ██║   ██║██╔══██╗
██║     ╚██████╔╝██║  ██║
╚═╝      ╚═════╝ ╚═╝  ╚═╝

██╗     ██╗███████╗████████╗███████╗███╗   ██╗██╗███╗   ██╗ ██████╗
██║     ██║██╔════╝╚══██╔══╝██╔════╝████╗  ██║██║████╗  ██║██╔════╝
██║     ██║███████╗   ██║   █████╗  ██╔██╗ ██║██║██╔██╗ ██║██║  ███╗
██║     ██║╚════██║   ██║   ██╔══╝  ██║╚██╗██║██║██║╚██╗██║██║   ██║
███████╗██║███████║   ██║   ███████╗██║ ╚████║██║██║ ╚████║╚██████╔╝
╚══════╝╚═╝╚══════╝   ╚═╝   ╚══════╝╚═╝  ╚═══╝╚═╝╚═╝  ╚═══╝ ╚═════╝
=====================================================================
   Roger Steve Ruiz
   2017
   https://gist.github.com/rogeruiz/c6df37c47b14aaafd8614bb1d0c8ad56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment