Learning Vim: The Path to Mastery (Vim Conf 2020) (Matt Boehm)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
be here all right everyone hey i'm live great the uh lag is it takes a little getting used to but let me close that and we're good to go so let me switch over to here and a quick introduction prime did a great job going through things but i've been using vim for about 10 years uh i ran a local meetup in the dc area and so that gave me the chance to talk at a bunch of local meetup events meet a lot of really great people and i wrote two plugins a couple years back one of them lets you view stack traces in your splits and the other one is kind of like a window manager if you have a ton of splits like i used to always have where you have a sliding window over them so before i forget i wanted to give out a quick thanks to everyone that's made vimconf possible adam prime everyone who's helped arrange things backstage and all the other speakers have been really great to work with and i'm honored to uh be among them and i want to thank my company too they sponsored my local bim meetup for years they jumped at the opportunity to sponsor this and most importantly they let me pose with my favorite keyboard on their otherwise very professional website so i'm very happy that uh they let me get away with that uh so opening up the talk i want to open with a story from one of my um let me switch over to the stage comments down here so i can get distracted great so uh i was at one of these local vim meetups and i was talking this guy and i said hey what programming language do you use for vim and he said well i don't know yet i kind of took me back you know most people learned them kind of late in their career and he said well i'm a professional musician and i decided i want to learn programming so first i just decided to look into keyboards and i was like what's the best keyboard and how do i you know learn to type quickly and learn to touch type then i started looking at operating systems and learned a little bit of bash and c in the command line and now i'm trying to learn vim and maybe after that i'll pick a language you know maybe python or something but i'm still deciding on that and so this really stuck with me not only because it was unconventional but because it got me thinking about the fact how that people have been learning all these other trades like music or martial arts or crafts for centuries or millennia way longer than people have been learning to program or use them and so maybe there are lessons in how they learn things that we can apply to our uh learning of them switch over to the terminal real quick and pull up my notes great alright so i have a couple of things that i want to show you in them so when i started i want to walk you through from when i first started learning vim to where i'm at today how i would do a simple task so when i very first picked up them and if i wanted to change this true to false i was still using insert mode and arrow keys so i'd arrow over there you know backspace out change it to false and pat myself on the back and at the time that's all i knew so that was fine that's how i did things maybe when i was feeling super cheeky i would try to arrow before that e and like delete three letters and only type the rest but half the time i'd end up typing the whole word yeah whole word anyway and it would still screw me up so that didn't really help that much but the cool thing about them is it gives you so many other ways to do things so very early on you learn about hjkl and maybe ce to change the end of the word so now you can just do l to the beginning of the word and c e false and look you've saved some keystrokes you got to stay on the home row and everything's good and really when you learn this it is a big improvement over over using the arrow keys but you often forget looking back how hard it was for you to learn this right this only works in normal mode so now you have to remember to escape out of insert mode to move around and get in that habit of escaping out when you want to move and this does take some time to develop but once you do it becomes second nature you just automatically escape without thinking of it and you stop trying to track what mode you're in as you're editing so uh once you learn that and practice it enough it becomes this second nature thing where you don't have to think about it it just works and that's kind of the advantage of repeated practice maybe later on let me change that back to true you learn about w and b and now you can move by word and that's way quicker and this is still the way i edit things all the time just because it's so much quicker than using hjkl and it's fairly easy uh so that's great you can now get there in fewer keystrokes and this is where you start to learn that vim has all these different motions for you and you get to pick whichever one works best for you and this is just the start right next a lot of people learn about f and t so f and t uh let you get to the next occurrence of that character right before it and i want to walk you through when i first learned this what it was like trying to use f and t so i know i want to get to that t over there and i there's oh i can use f for t to get over there uh i'm moving forward i want to get on the t so i want the lowercase f1 so i'm going to type an f and a t and oh i didn't jump to the right place there was another t on the line so i need to repeat that that's semicolon and then i get there and when you're first learning this is actually slower than using the arrow keys and it's only with practice that you can find some efficiency in this and jump over real quick oh wrong window so this introduces this idea of your productivity over time as you're using them and i i picked up this concept from a book i just read called mastery i was giving a talk and had mastery in the title so i thought i need to read a book called mastery but one really cool idea they had is this idea of your productivity over time as you're learning something so when you first start you're very low you start getting very comfortable you go up and then you learn about a new feature like this wb or f and t and your productivity actually drops because you're learning something new but over time as you develop the muscle memory and practice that and learn to use that very efficiently it goes back up again so as you're learning them you're going to have these up and down bumps and the key is to keep striving for learning things as muscle memory and having these things become second nature and just take them on one problem at a time you don't need to learn f and t day one you can do you know just hjkl wb whatever you want until you're ready and you're comfortable with that and ready to learn about the next step so let me jump back over here so f and t actually worked out to be slower than the arrow keys and the good news is as you practice a lot of these steps will go away so you won't have to think about how you're going to use f and t you're just going to right away say i want to jump to that t and you're going to do these two things as a single motion and when you're not in the right place you're going to learn to anticipate that and when you type ft your finger is going to be hovering over that semicolon key just waiting to hit it so uh with practice this does speed up but f and t introduced this concept of misses and i like to think of it like a cache miss or something where you thought you were gonna jump to that t but you don't uh you end up jumping somewhere else because you didn't see the other t in the line and you can find this in many areas in vim you know if you're auto completing and it's not the first result when you expect it to be you're using a fuzzy file finder you'll repeatedly encounter this idea of a miss where you want it to do one thing but it's a little bit stochastic and kind of random in a way and it didn't do exactly what you expected so you need to learn to deal with these misses you can learn to anticipate them and have that finger hovering over the semicolon but you'll also find that you build an intuitive sense for how likely you are to miss so for instance when i'm using f and t i use them a lot with punctuation or capital letters which are rare in my programming however if i'm doing lowercase methods or lowercase letters i'm trying to jump to i'm more likely to just use wb because somewhere in my brain it's stuck that those f and t might fail more often for that and of course the last thing you can do is find other solutions that are less likely to miss so let's get back to our example over here i can also search for the true so if i start searching and type tr and hit enter i jump right there and now i can change that to false right and there's two ways to search you can either search and with ink search you can type the t and see if you're there and if you're not there yet you type the next letter and now you're there and you're good or you can search by just typing the first few letters and hoping you end up where you want i usually use that second method because i don't want to have to look at my screen and wait for it to tell me you know am i there yet am i there yet i just want to risk the chance that i'll do a miss and 99 percent of the time i don't and i end up being faster that way and then lastly just to mention it uh there's also a plug-in called sneak so instead of using f and t to go single characters sneak lets you jump two characters at a time so i can just str similar in my search but i don't need to slash and enter and i jump right there so sneak's pretty cool too and then lastly some of you may know that you don't need to see e false because of course there's been plugins to help you with that so in my case if i type control a i can swap between true and false over here so now combining all these things if i'm at the beginning of this line i know i can sneak over there and hit ctrl a to toggle it so the reason i wanted to go through that painful journey of all those ways of doing these commands is to talk about the idea that you want to learn a little bit at the time and then focus on mastering it and am i no i'm not cut off i'm just blocking my screen so you don't want to day one have to learn about f and t and sneak and switch because before that you need to learn about modal editing and getting to the right mode at the right time and only after you've internalized that and mastered that will you be comfortable with some of these more advanced techniques so don't worry about learning everything at once just try to pick up one little thing at a time and if something isn't working for you you don't need to use it if you don't like f and t just don't use it there's a million other ways to do it and it's all about what's intuitively easy and comfortable for you and then as soon as you're starting to feel feel comfortable that's when you want to look for improvements uh so once you're comfortable with w and b that's when you start seeking out f and t or search or some of these other methods all right so next i wanted to talk a little bit about improvements the first thing i tell a lot of people when they're picking up them is to make a vimarcy and first thing you put in there is a shortcut to edit your vmrc so in my case it's comma ve open in another window and the cool thing about this is not only that you can reference all of your plugins and settings and all of that stuff if you forget them but if you think of a new thing you want to try you can open this add it save it and source it and you're you're going and if you want to change it later open it remove it but making that loop of changing things and trying them out as short and tight as possible is really key in learning to use them better let's close that one thing that you'll learn is even the smallest changes make a huge difference so uh i'm in the jj to escape crowd so instead of reaching up for the escape key some people remap caps lock i learned that you can remap jj in insert mode to escape and it sounded weird at first but once you use it you realize oh this is really really fast and actually works fairly well and so this has a chance of giving you a miss just like that f and t if i'm trying to type a word that has two j's in it then it's going to think i'm trying to escape and escape out but luckily those words are pretty rare so these are the words with two j's in it and for instance uh if i wanted to look at words that end in j because if you're trying to type a j and then type escape it'll mess you up none of these are words i'm going to type so i like this example because it shows you a strategy for finding a mapping that's not likely to miss and you can then use that to improve your productivity and as you learn this it will be muscle memory when you first start you think about hitting the j key but after a while you're just double tapping your index finger when you go to escape in my case i'm using dvorak so i actually mapped hh to escape which misses a little more but i'm so used to that double tap i can't give it up go back up here in a really silly example that's a little more controversial i remap semicolon in normal mode to colon so normally to type a command like save you do colon w and i'm like i don't want to have to hit that shift key just to type a command so instead of hitting shift in the semicolon key i can just hit semicolon w to save and it seems kind of dumb right you're remapping semicolon which normally is for that fnt thing and it's such the smallest improvement you don't have to move your pinky over and hit that shift you're saving maybe like a fraction of a second and sure those seconds add up but what this is really about is easing the cognitive mode and just making them more comfortable and usable for you so uh even though this seems like such a silly thing and you've got to find out another way to repeat your f and t commands this has actually one of the bigger impacts and if i ever don't have it i miss it really hard and you can play around with many other ways to reduce keystrokes so of course you'll get into mappings as you learn about your mrc but there's also abbreviations and snippets to play with while you're programming and you can really make them yours and customize it however you want to so for instance if i look at my abbreviations i just have a few in here to play with this would be in my config and bim uh so a sample abbreviation instead of typing implementation i can just type impul and i i forget what i was working on at the time that i had to keep typing this word and just got sick of it and typed impul instead and as soon as you hit space or period or anything else it completes the work for you saves you a bunch of time and it doesn't do it if it's part of another word so i can still type simple and it's not going to expand that uh they're a bit dangerous to deal with but if you try them out and you like them great i have a bunch for python that i added more recently just to save a few keystrokes i'm not convinced that these are actually that beneficial yet but i thought i'd play with them and try them out see how they go uh to show you an example of that let me switch over this other guy so uh as i mentioned you want to really practice to make these things turn into muscle memory and one great way to practice is to repeatedly do the same programming problem in what's often called like a code coda so i started doing this with python to try out these new snippets i had written here's a small one where i'm running a prime number writing a prime number generator it takes about a minute to get through you'll see instead of import i typed i am in space to go over and then i'm trying to use snippets there's a bit of visual artifacts from the screen recording software but this was a chance for me to play with all of these and figure out maybe this snippet is good maybe this isn't you know the four in one it really doesn't save that many keystrokes i'm using autocomplete when a word i've already typed needs to be typed again and repeatedly trying your code out this way is a great way to practice and the other cool thing you'll see in a minute is i have a key key binding in python so if i am in normal mode and hit enter on this file it'll open up a terminal and run the file and if anything goes wrong it drops being a debugger that shows me uh all the variables lets me do whatever i want over there another snippet for import main uh and i just pre-recorded this because i'm too nervous to type this crazy thing live and you can see after this i run another snippet for that and i hit enter it runs the file there's an error because i actually didn't include the two the first prime number so now i jump down there using sneak and i use that yield 2 and now i'm done so that was probably take like 30. it's crazy when you record yourself using something like a cinema as i did or another tool how embarrassingly slow and error prone your code looks when you're looking over your own shoulder and that really helps give you kind of a coach in yourself and a critical eye on learning how to improve to program so it gives you a chance to practice the same thing again and again because again if you are an athlete or you're a musician or anything else you're playing the same scales you're running through the same exercises you're doing the same thing again and again and again just to try to build up that uh build up that muscle memory and really practice the techniques in a very concise way rather than just when i'm coding maybe i'll use a snippet once or twice and next thing i want to talk about is uh did i talk about improvements already let's see let me close this i did so next on the list is talking about magic real quick if i type the full file name so as you learn vim one cool thing you'll figure out is that a lot of the knowledge builds on top of what you already know so for instance if i have a print foo bar and i want to change the stuff in that print to something else you learn about ci but then if you get targets.bim all it does is add a cin that lets you do it if you're before the string so now i can cin and i can still change it and it even works multi-line as well uh you'll learn lots of other cool tips as well so just like you can say i uh double quote to do inside double quotes you can make your own custom text objects or find others that are out there so bii is select inner indent for me so everything in the same indentation level gets selected and once you learn all these cool normal commands you also learn how to repeat them so first you learn about dot to repeat them but then you might learn about macros you might learn about norm which is my favorite short for normal uh and that lets you just say you know if i want to all these lines i want to do something to them i can say like norm insert at the beginning you know a double quote now every line it's going to insert after the space is a double quote after norm you can also use norm with g so you can say just do that for lines that match a certain pattern so i can say like uh well i don't need that i can just say g any line that matches this ci thing i want to norm i double quote maybe you learn about recording a macro and realize you can use your macros in these g patterns and my personal favorite plugin is actually quick fix reflector which does a very simple thing all it does is it says your quick fix list that you use when you search is now a buffer that you can edit so let me show you what that means so let's go over to some test code that i wrote that's very simple dumb stuff and let's say that you have a bunch of json files here they have some ids in them and they have sensors in them and these sensors someone told you oh those actually shouldn't be uppercase all the sensor ids are supposed to be lowercase well i can use rip grep in this case to search the whole directory find all of these thanks to quick fix reflector i can delete these additional entries that i don't care about and resave the file because it's a quick fix list i can browse it now by the single entries or by file and then uh the cool thing that quick fix reflector lets you do which a few other plugins have picked up and gotten on this is i can edit the text on the right side here and save all these files simultaneously so for instance in this case if i say like norm v to in double quote so i'm using that targets to select everything in the second pair of double quotes and then i want to lowercase them now they're all lowercased in both files i hit save it saves both files so now i can go over here and look at both files and it's all lowercase throughout your whole project so now you're not only editing a single file you can edit your entire project as a single buffer using this and it all comes from the simplest extension of bim just saying oh yeah that quick fix list now it's a buffer so that's one of my favorite examples there and that covers most of what i wanted to talk about i'm gonna hop over to a few resources for people that are trying to learn this stuff so when i first learned vimcas were very helpful drew neil has since written a book called practical vim that's a very great guide to learning and my one bit of advice with that is that uh you don't want to read the whole book cover to cover and try to memorize it all read it a little bit practice with it until you get it down and then read the next bit while you're learning you want to have a channel of tips and tricks and ideas in the background so that if a new idea sounds really cool you can think oh you know what i'm ready for this i want to try this out and it helps teach you what's possible in vim in a way that's very passive for you you just check your email or do something else so vim tricks and the vim subreddit is a great way to get this information you just kind of learn about all these cool features passively the free node uh vim channel is great and there's a vimconf discord where people can help you if you get stuck and if you ever want to write a plug-in you should read learn vim script the hard way cover to cover and practice it out because vimscript is a complicated language with lots of crazy stuff going on so i learned a lot through that and i'm so glad i didn't just try copying what other people had done and try to skip the hard part of learning so that covers everything i had to talk about i'm going to take a quick look at the comments so feel free to ask questions in there i know there's a bit of a delay yes so someone mentioned the cin command that's called targets dot them targets so that's the one you want for cin yep vimscript is a complicated language uh yes so normally you can't see i double quote outside of the double quotes but with that targets and cin that's what lets you do that remapping jj to escape my key mappings wrong one let me see if i can find the right one there it is if you want to remap jj to escape i know remap jj escape and again in my case i use hh as well see my plugins by my plugins do you mean the ones i wrote or just the plugins i use this is a big laundry list of plugins that i have installed that i'm happy to go through uh you can find these on my dot files i still need to push a commit with a newer version so if you look up matte bone you can find a lot of this there fuzzy file finder i still use control p pretty heavily because that's what i'm used to even though i've been told i should switch to fcf and i'll just leave some of these on screen in case you want to see some of the plugins some highlights you want a way to toggle comments on and off in my case nerd commenter see what else is really good in here uh i use vim fugitive pretty heavily uh i recently switched from act to rip grep but i still have ack installed so i have to remove that uh a couple other great plugins over here oh you're still seeing the slides i'm so sorry yep so part of doing this whole talk was trying to learn about streaming and i took to hacking this little between my scenes but i'm still getting used to it a little bit so here's some of my plugins over there uh i'll quickly show what i use to run python files since that's one of the cooler bits so this is the mapping that i currently use to run a python file by hitting enter by remapping the current buffer enter to silently make this the only window just to make moving around the windows easier then i split open a terminal and i run in my case ipython3 dash i and then the file that i was just on uh ipython is just slightly nicer than python if you have it installed but python works just as well and the dash i is what jumps you into that interactive debugging mode so if something goes wrong you can print out all the variables there oh i'm so sorry let me move this to the top with zp and i even have another view if you just want to see the terminal great everyone thank you so much i'll keep an eye on the questions here until someone comes in to kick me out i believe the next talk is going to be the primogens talk which was originally going to be before mine but is now moved while i have you here another cool perk of this little macro pad i'm hacking is it's a sound board so i don't know if you'll hear it but blow out my eardrums so someone asked about using vimscript for plugins given neovim's new plugin system i haven't written vim plugins in a little bit so i haven't kept up lua seems very cool you'll still probably want to write some plugins that support both bim and neovim and i'm not clear on how to do that with lua but hey and this macro pad i'm trying to figure out how to use it better for programming honestly you can remap anything in vim and any kind of chorded keyboard combination so every time i've played with foot pedals or external boards it's just easier to find a mapping on your keyboard then go externally uh yep so i did switch to neovim it's fairly easy you have to you know simulink your config files or something but after that you really won't notice any downsides to switching these days it comes with a lot of cool features some of them have been back ported to vim but it was first with the terminal emulation mode that was super useful it was first with the async stuff so i'm really happy i switched to neovim and i think if you switch you'll never look back favorite built-in trick i don't know what to call a built-in trick uh let me talk about some of my mappings i guess some mappings that i'm just trying with is you can use control p and control n to auto complete words but i only want to use this when i want to accept the first result so if i want to type i know remap again i don't want to type this hit ctrl sorry ctrl p i just hit command p try to print my terminal so i don't want to do this type control p and then have a list that i have to like select the entry from i only want to use it if it's automatic and i know that it's going to type the right word so instead i'm trying this mapping where i just do control p control y and i was running out of things in insert mode that wouldn't clash with things so i'm trying out this uh comma period to uh insert a word there and line complete works as well with this oeu so since i'm in dvorak i can type i know remap and type oeu for a line completion let's see what other tricks there's a bunch of little things so i highly recommend you browse other people's vmrcs and just try to pick up little tricks so having vv select to this end of the line kind of like cc does is a big help i use x for escape in visual mode since d already works for that uh lots of great stuff you can find by reading other people's uh mrcs there's a visual star plugin down there not really a plugin but this means that you can highlight any arbitrary bit of text and just hit star to search on it it's very useful uh so definitely read other people's vmrcs and figure out what plugins are there four minutes i got four minutes that i can talk about other great stuff with let's see what else is good to talk about in here happy to eat up the uh time very minor plug-in that's pretty useful is brightest so as you might notice the word under my cursor is highlighted in red this makes it very easy to catch spelling mistakes takes a little getting used to but once you do it's just a nice little feature on top of them i use wombat 256 mod for my plugin as i said text objects are pretty useful that vii comes from this bim text object indent to select everything in an indentation level targets is a great plugin here's vim snake which you can actually change your f and t to use vim sneak which is why when i type ff i get these highlight results and instead of typing semicolon i can type f to repeat uh surround is of course one of the good old very great plugins to change single quotes to double quotes for instance quick fix reflector is that great plugin i talked about that lets you edit your quick picks list uh and to answer your question i'm not using relative line numbers just because i'm not fast enough yet at typing the number keys so for me it's i'm fine moving around with searches and sneaks and honestly a lot of jk or using curly braces to move between sections switch.bim is the plugin i recently found to switch between true and false and things like that it falls back to speed dating which is for incrementing or decrementing dates and i'm still use control p just because i tried the knight and it was a little too complicated for me um and i haven't given a good shot at fcf yet uh you're gonna find that i am big on customizing them as much as you want so i have a ton of old plugins in here i'm fine with remapping things like crazy uh let's see if anything else sticks out here a lot of these are somewhat more standard plug-ins nothing quickly demo-able but yep that's about all i got so i hope you enjoyed my talk feel free to find me on twitter on github chat me up here uh about anything whether it's uh dim stuff programming stuff uh you're interested in the company i work for whatever you can do and i use regular dvorak and on that note i'm out see you guys later
Info
Channel: Matt Boehm
Views: 6,220
Rating: 4.9411764 out of 5
Keywords: vim, tech-talk, vimconf
Id: SZ-xHGD7sMc
Channel Id: undefined
Length: 35min 4sec (2104 seconds)
Published: Mon Sep 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.