Episode #3

Vim's Versatile CLI

Vim is a command-line tool and therefore comes with all the modular conveniences this entails - such as the ability to respond to signals, to be opened with special flags, or receive input from a pipe.

April 26, 2020

Show Notes

Software

  • Neovim - The vim fork I happen to use. I have no opinion on whether it's better or worse than regular Vim. This video used version 0.4.3
  • fzf - A versatile command-line fuzzy-finder
  • Universal-ctags - A maintained version of ctags. Used to index ("tag") language objects that can be used in navigation and auto-complete.

Clarifications

  • In case anyone else was confused by the **<Tab> file listing, that's an FZF trigger. Read more here

Vim Plugins

  • Vim-fetch - Open up vim at exact line number when fed a stacktrace entry.

Dotfiles

Screencast.txt

Transcribed by Rugo Obi

Before I continue showing more editing within Vim, I’m going to highlight how well Vim is integrated with the command line.

So first off, let me send a SIG-STOP to the Vim instance - that's with Ctrl Z - and this will suspend Vim and put it in the background. Now I can run a command-line command in the very same tab. This is handy if you're SSHed somewhere. And you can see that's executed, and now I type fg to return to Vim exactly as it was.

I'm going to exit now normally with ZQ - exit without saving.

Now, I'm going to demonstrate another workflow I find pretty handy.

Let's say I want to inspect part of a log but the log is quite large. So I have this development.log thing here, which according to word count wc has 20,000 lines. You can imagine this is even bigger.

So now I'm going to filter this log to the bits I want. For the sake of simplicity, let's say it's just the last 150 lines. In real life, it would probably be a grep pattern of some sort. Now that's just 150 lines, we can go for a word count wc again. Now, here's the key bit. I can open up these 150 lines alone with Vim if I tell Vim to accept input from stdin- which is symbolized with a hyphen there ... | nvim -. And now it's opened this up in an unnamed buffer, and you can see it's got 150 lines. I press capital G to go to the end of the file.

So let's exit them again.

Now, another cool thing is you can integrate Vim with... well, to be honest, you can integrate any command-line command with... is fzf. So I type ** and then tab, and this gives me this kind of autocomplete for any file within the directory. So I can fuzzy search for analytics, and that will fill out the argument, and I press Enter and I can go back into the analytics file that way.

Another way to open up Vim at exactly the right place, is to rely on them universal Ctags. So, if I pass -t to the command line interface for Vim, I can start typing out, attach for attachScript, now I press tab, and this is Vim populating the autocomplete. I can see attachScript, the function we looked at earlier, and then when I open up Vim, it'll bring me right there. Super cool.

I'm going to exit again, and demonstrate something else.

So, imagine we have a stack trace, and I want to inspect each line. Or maybe have a script to open up a Vim instance for every line. What I can do is... just going to populate this from my history. I can type the file name and then, as with all stack traces pretty much, colon, and the line number. So I've got :87. If I press enter, now thanks to the Vim Fetch plugin, I'm at line 87 of the file in question.

bash nvim app/webpacker/javascript/analytics.js:87

This is great for debugging, especially debugging library code which you can also open up handily with Vim.

Lastly, let's imagine you want to open up a Vim instance, at a particular string in a file, but you don't know at what line that string is. And also it doesn't have a handy tag that you can open up with a -t flag. So, let's say within the package. json I'm looking for the devDependencies key. So I can fill that out there. So it's + to say I run a command in Vim, and then / for the search as we've seen before, and then devDependencies the string in question.

bash nvim package.json +/devDependencies

And there you go, we are on line 20 where the devDependencies key is highlighted.