 Well, hello everybody, welcome to HackerTools lecture number three. Today we will be covering text editors and version control, I think two really important and exciting topics. As for text editors, I think as programmers, we end up spending most of our time interacting with plain text files, like all the different tools you'll learn to use outside of this class on programming-related things. You're going to spend most of your time in your text editor, not your shell, not devourers, not other tools. And so for that reason, I think it's really worth investing time to figuring out what text editor works best for you, because people have lots of different opinions on this. And so it's worth finding an editor that fits your needs, really putting the time into learning the tool really well, and putting the time to customize the tool to make it do exactly what you want it to do. And I think the way you learn in your editor is you just force yourself to use that editor for whatever you do for a couple weeks, so like you're taking a class or whatever you do to write programs. Well, if you want to learn Vim, force yourself to use Vim for a month. And maybe initially you'll be slightly less productive than you were with whatever tool you were using previously, but really it's the only way to become really good at using some new editor. And if you switch to a new editor that has a kind of difficult learning curve, maybe it'll be slightly less productive for a couple weeks, but it'll begin to pay off starting maybe two or three weeks in. So in this class, we're going to teach you the basics of Vim, which is one powerful, commonly used text editor. But we encourage you to experiment with other options. We're teaching Vim because John and Jose and I all use Vim, and so that's the only tool we really know super well. But different people have different opinions. We even have some links in the course notes linking to the editor wars. Like people have really strong opinions on Vim versus Emacs and have many flame wars on used add-in stuff based on comparing these two editors. So yeah, in this lecture, so in this 50 minute unit, we're going to introduce you to the basics of Vim. We can't really teach you how to use this powerful editor in just 50 minutes. And so the focus is more on teaching you the basics and then showing you what some of the more advanced functionality looks like, teaching you some of the philosophy behind the tool, and then giving you links to resources so you can master the tool in your own time. It's really to inspire you to put in the effort to learn this tool well. And a lot of philosophy we're going to teach you are going to be in the context of Vim, but the idea should translate to any other tool you want to use, even if the exact sequences of commands are not the same. So let's get started. We have a couple, if you look at the course notes, there are a couple links in there that you might find interesting. Looking at surveys for which editors are popular in many different programs. Like Stack Overflow ran this survey just last year, and the results are kind of interesting. You can get some inspiration from there if you want to figure out which tools you might want to experiment with. And so there are two main classes of text editors. They're ones with graphical interfaces, things like atom or sublime text. And then there are tools that we're going to spend our time talking about here, which are command line text editors. So this is the command line. You learn how to use the shell. Last week when John taught you how to use the shell. Even if you eventually decide on using an editor with a graphical interface, it's worth learning some command line editor, at least knowing the basics, because it's really useful when editing files on row machines. So say for a class you need to log into Athena and run some code there. Well, it's really convenient if you can just SSH in and then run a text editor in your terminal on that row machine. Rather than having to deal with copying files back and forth or using some of the more complicated techniques we'll cover slightly later in today's lecture. So before we get into them, I'm actually going to show you a different text editor. There's this text editor called nano. And so the way you invoke it is just like any other shell command type nano, and then it takes an argument the name of the file you want to edit. So in this lecture, we're actually going to be editing the course notes for this lecture. Kind of meta. This is a good text editor to be aware of because if you eventually end up settling on using something with a graphical interface, like say, Adam, well, if you SSH into a machine, log in remotely to a different machine and want to be able to edit text there, you're going to need to be able to use something like this. And this is one that's really simple to use. So this is what the interface looks like. There are basically instructions always visible at the bottom of the screen. And you can move around using the arrow keys. So there you see the cursor moving around. And I can just go and edit text directly, type in whatever I want. And it works more or less as you'd expect it to. And basically the only key combinations you're going to really need to use among these options down here are this one here. That care, oh, does anybody remember what that notation means? Talked about it a little bit during the shell lecture. That means that so that care is referring to the control key. So if I type control, oh, then it goes to that write out command. And this editor asks me, okay, what file name do I want to write it to? And it's pre-populated with the name that I open the file with. So if I want to read any of the files I can do that here. And save it with a different name. Anyways, I can do control, oh, and press enter and it saves the file. So pretty straightforward. And if I want to close the editor, I can just press control, x. That's also given down here in the bottom left. And there we go, I've modified that file. If I open it up again, I can see that I deleted that word and that change is persisted. So very simple text editor will be pre-installed on all the systems you use. And so it's just convenient to know that this is a thing and you can use it if you need to. Any questions so far? Okay, so this is a text editor that's great to use when you don't know how to use anything else and you're logging into a remote machine that has no other editors installed and you need to make a small change to a file. This is not the thing you want to use to write complicated programs because it doesn't really have super great support for doing anything fancy that you would want to do using your text editor. So instead, what should you do? Well, we're going to teach you how to use VIM, which is one powerful text editor. VIM is a cell VIM and you can launch it just like you want the other editor. So VIM and then you can give it the new file and you'll see this interface. Okay, so to tell you a little bit about the philosophy behind VIM, this is actually like a pretty complicated tool with a lot of neat ideas behind it. So before I actually go and teach you how to use different key commands to make you do things, I'll tell you a little bit about the philosophy behind the tool. So one idea that the programmers behind this tool, and behind the earlier versions of this tool have, was that when you're programming, like we're not using this to write essays or something, we're using this to edit code, or edit plain text files. And so when you're doing that, you actually don't spend most of your time writing characters. You spend most of your time moving around the file, reading the text and manipulating the text in different ways. You don't just kind of like put your cursor somewhere and then just go write a bunch of text, right? You go and like find a function somewhere and like edit some text there and then go jump somewhere else in the file, edit some text there. And so if we want a tool that's designed for doing that sort of thing, maybe it shouldn't behave like other text editors like say Microsoft Word. And so there's this idea in them called modes. Like Vim is a modal editor. The entire tool can be in one of different modes designed for doing these different kinds of things. And so there's a mode for inserting text, like when you want to go and type in a bunch of characters. But there's also a different mode for moving around the file or manipulating the file in different ways. That's called normal mode. And since in programming, you actually spend most of your time reading or manipulating code rather than just inserting code. You spend most of your time in Vim in what's called normal mode. We'll talk a little bit about how to switch between modes and how to know which mode you're in in a moment. Another powerful idea behind this tool is that Vim is programmable. And so this tool is, it comes with this programming language called VimScript. And you can customize a lot of aspects of this tool by writing configuration or even more complex programs or plugins in VimScript. And it's also programmable using bindings for different languages like Kilmer and Code and Python or Ruby or whatever. They're probably bindings for whatever language you want to use. And so this editor is really heavily customizable through that. Another thing is that even the interface of the tool is kind of like a programming language. Like the way you move your cursor around and make changes to the file is through a sequence of commands. These are usually single keystrokes. These commands have mnemonic names. So like, we'll talk about the details in a moment, but when I'm in normal mode and I want to go to insert mode, I can press I. Or if my cursor's somewhere and I want to substitute the character under my cursor, I press S. Or if I want to put my cursor somewhere and replace a single character with some other character, I can type R and then the character I want to replace it with. Don't remember those specific commands or a lot more about those in a moment. But it's this underlying idea that the interface is like a programming language. They're these keystrokes with mnemonic names. And the commands are composable. So you can learn different movement commands and different editing commands. And then kind of combine them in any way you want to make these compound commands. And that's what makes this tool so powerful. So for example, there's command to move forward by a word if you just type the command by itself. But there's also a command to change something. So if you combine the command to change something with the command to move forward by a word, then you can change the word, which means it'll replace the word that's currently highlighted or where your cursor is and then put you an insert mode where you can type in a new word. There's also a command to move to the end of a line. So that's movement command. But if you combine the change command with the command to move to the end of a line, what'll happen is you'll delete everything from where your cursor is to the end of a line and go into insert mode where you can change that content. So this general idea is super powerful. And we'll go through the details of different movement commands and different manipulation commands and see how they can be put together in a moment. Any questions about that general idea? This is probably really different from tools you've been using so far. Actually, it's a quick survey. Like what kinds of editors do people use? Oh, so you do these, but cool. How many people use graphical editors and terminal based on this? Pretty no time. No time? Oh dear. Okay yeah, so those are some of the underlying ideas behind this tool. Some of the things that are kind of important ideas too. One is that we don't want to use the mouse for navigating a file because it's like a kind of crappy input device that's not super precise and it's very slow. Instead we want to use the keyboard. Like we can use all 10 fingers and once we know the commands and kind of have some muscle memory, you can move around a file and manipulate a file really fast using the keyboard. So when using this tool, even though it does have mouse support, select text and move my cursor around and stuff, you really shouldn't be using the mouse. And really the editor should work at the speed you think. Like you should be able to think, I want my cursor to be over there and then a moment later your cursor should just be there. And using the mouse, I can't really do that but maybe using the keyboard and knowing the proper way to talk to my editor, I can make that actually happen. My tool should just get out of my way and manipulate the file in the way I want kind of at the speed I think. Okay, so now we're going to get into some of the details. Any questions about philosophy or ideas so far? Cool, okay, feel free to follow along I'm sure you, just by default, have Vim install on your computer or if you don't, if you're running Linux, like app get install Vim, if you're running macOS, screw install Vim and you'll have the tool. You can just open up a plain text file play along if you want or just watch me type things into my journal. Okay, so the first thing we need to know about Vim is the different modes. So remember I said Vim is a modal editor kind of behaves in a mode that's designed for inserting text versus a different mode that's designed for moving around the file or manipulating the text. And so the way you know which mode you're in is that it's shown in the bottom left of your editor. So see here, this says insert, that means I'm in insert mode. There are different modes. Here's visual line mode, visual block mode, just visual mode, replace mode. We'll talk about what the different modes are in a moment but the way you tell which mode you're in is that it's just shown to you on the bottom left. And if it doesn't say anything that means you're in normal mode. Now the way you switch between modes is if you're in normal mode, there's a key combination to take you into any of these modes. So if I'm in normal mode and I press lower case I, it puts me to insert mode after I can type in text. And then the different key combinations for what are called visual modes which are for selecting blocks of text or I press V, then I go into visual mode where I can move the cursor around and selects a block of text kind of linearly. There's also visual line mode. If I do capital V I can select a block of text by lines. And there's visual block mode or if I do control V I can select a block of text. And so selections are useful for different things which we'll talk about in a moment. So normal mode press I go to insert mode, normal mode press V, capital V or control V I can go into different types of selection modes. And the way to get back from one of these special modes back to normal mode is by pressing escape. And so small side note here. On your keyboard the escape key is probably this tiny key in the top left and it's kind of inconvenient to reach with your vinky. The keyboard that the programmer VIA used was designed such that the escape key was basically where the caps lock key is on your keyboard. So it was a much more accessible key that's easier to press. In Vib you're always gonna be switching back and forth between insert mode or visual modes and normal mode really spending most of your time. And so it's convenient to have an escape key that's not this like obnoxious thing in the corner. And so what you can do is you can remap your caps lock key to escape because caps lock isn't really useful unless you want to shout at people if you can do something. And so I think in a later lecture we'll talk about how you can do this on different systems but that's a quick thing to show you how easy this is on Mac OS. If I go into system preferences, keyboard, modifier keys. Here I can just remap caps lock to escape. Kind of zero effort required. Get rid of a useless key and have a very useful second escape key that's easy to press. Or to control, there's control. Yeah, different people map their keys in different ways. So I think for example, Emacs users are more likely to map their caps lock key to control or something else. Whereas for Vib I think escape is a very popular way of mapping that otherwise useless key. John has strong opinions about mapping it to the control key I guess which you can talk about later or now if you want. So the reason I map it to control is because it's a key that's useful outside of VIN and then I have control J in VIN set to escape. And control J is on your home rows so it's very quick to type but then the control key is still useful in other places, the escape key not so much. Okay, so any question about modes or how to switch between them? Or kind of the general idea of what the modes are meant for? So you can see in normal mode, if I start typing text on my keyboard it actually won't do the right things. I'm giving commands to the editor. That's what normal mode is designed for. But if I press I to go into insert mode and then start typing text it just goes into my type file. I to go into insert mode which is what we're gonna be doing for the most part today and then escape to go back to normal mode. Cool, okay. So now we know what modes are. Let's talk about basic VIM commands next. I don't know if any of you have heard the joke of like, I've been using VIM for 10 years now mainly because I don't know how to quit the thing. You notice if you press control C it doesn't quit. The way you quit VIM and the way you do a lot of things is by issuing what are called X commands. X is a different editor or like different component to the editor. Basically, if you're in normal mode and you press colon you'll see that the cursor jumps down to the bottom left. And it's like colon and then I have my cursor and I can give the editor a command. And so there are lots of different X commands that do different, useful things. Some basics are, if I do colon Q which is short for quit and press enter it'll quit the editor. So that's actually how you get out of the software. Other useful things are I do colon W which I think is short for write. That now you see it says file written. That's how you save a file. Sometimes there are nice combinations of these things so like there's a colon W, Q. By the way, can everybody read that text or do I need to make it larger? Can they make it larger for the videos as well? Oh, I'm just gonna repeat. Oh, yeah. So they're, for example, a combination of W and Q which saves and quits. Other useful things, if I do colon E this is for edit and this one is a command that takes an argument. So I can give it the name of a different file, for example, and go to edit that file. Then kind of multiple buffers open. So this is showing a different file open. If I do colon LS it will show me a list of the different buffers that are open. Colon BN switches to the next buffer and the list of buffers. You don't need to remember all these commands because I will give you resources to learn all these different things. But this is just to show you. If you think about using whatever energy we were using before, say, Adam or Visual Studio Code or whatever, these kinds of things like opening files or saving files, you probably did through the menus, right? Like you took the mouse and you moved the mouse to the top left there are a bunch of menus to do things. Instead of this software, you issue commands as text. Once you memorize these commands most of them just become muscle memory and super fast. So another really useful X command is the command called help. This editor actually has a ton of built-in documentation and then we access it through this help command. And so if you do colon help you can give it the name of a topic. So I can do help write and it will open up this thing telling me about the right command that I showed you a moment earlier. So colon queue is actually not exactly quit. It's more like closed window. And so you'll notice by it didn't help write to look at this documentation. I did colon queue to go back from looking at that documentation to edit the file it opened earlier. So these are some very basic X commands. This is basically telling you the equivalent of like what was in your menu bar at the top of your editor you used previously. Also beyond looking up help for commands like this write command you can look up commands for the different key strokes that are part of this programming language that we'll look at in a moment. So how do you actually move the cursor around the file? You don't want to use the arrow keys even though they do work. There are better ways of navigating a file because like if I press the W key my cursor moves forward by an entire word at a time. How do I know what the W key does if I look at help W? See this documentation says okay this moves by word forward. So that's the help command and that's a quick overview of some basic X commands to quit to save a file colon queue colon W colon W queue of save and quit LS to look at a list of buffers and help to learn more about the editor. And there are a ton of these commands that are useful for different things and we will give you resources to learn more about those. Any questions about X commands? Okay, so you can save yourself one character of typing by writing colon X instead of colon W queue. Saves you so much typing. Yeah, so colon X is a shortcut for W queue. There are lots of things in BIM like this. So BIM is kind of like a programming language, right? And so there'll be a combination of keys that can accomplish a certain thing. And oftentimes there might be a pure combination of keys that accomplishes the same thing. Well, I think we have some of those examples in here too, probably. Like X versus CL or S versus CL or things like that. CQ as well, it's handy. Okay, so the next thing we wanna talk about is how do we actually move around this file? Well, there's insert mode where if I press I I can actually type in text and just goes into the file. But for the most part when I'm programming I'm like moving around the file and manipulating it. So how do I move? Well, one way I can do it is with the arrow keys just like a nano and so I can like press and hold the down arrow key and scroll down to my file. But this is a really inefficient way to do things. They're much better movement commands. And so I actually recommend that you disable your arrow keys when you're learning BIM just to avoid getting into these bad habits. You can do that by putting a bunch of commands into your VIM RC file. So you can actually do that right now. Let's open up, I'll just quit the editor and do tilde slash dot VIM RC. We'll talk more about what these dot files are in the dot files lecture, which I think is next week. But basically VIM is configured through this plain text file called dot VIM RC where you write a sequence of VIM scripts that I commands. And these are the settings for your editor. And so this editor is super heavily customizable and you can remap keys and do all sorts of other fancy things. And so one thing you can do is you can even remap the arrow keys. So I can say don't worry about what exactly this command or what exactly this VIM script means. You can learn more about VIM script if you care. So this line of code says when I press the, I want to remap the left key to instead be the command this like echo. So it'll just give me a message instead of actually doing what a movement it was supposed to do. And so now for example, if I close and open my editor again to get to reload this file, which it loads by default. Now if I press the left key, you'll see that it doesn't actually move left. I'm pressing left. It says use H at the bottom. So VIM actually instead of using the up, down, left, right arrow keys on your keyboard, for those motions you use the H, J, K, L keys. And so H moves left, L moves right and J and K move up and down. And so if we could go and remap all our arrow keys so they don't actually do anything except complain at us and tell us to use the right motion key. So I'm gonna colon W to save this file, colon Q to quit. Go back to my headers.md file. And now if I put my mouse somewhere and try using the up, down, left, right keys, it'll instead tell me if I press offices, oh, use K instead and so on. So okay, so the most basic movement to the up, down, left, right is done with the H, J, K, L keys and there's some historical reason for that. But today like this is useful because you don't need to move your fingers from the home row, you don't need to look anywhere. You can just move around with that thing too much. But in general, you don't want to move these really inefficient motions. Like if you wanna go down 100 lines, you don't wanna like press and hold J and wait for half an hour while your editor scrolls down. So there are a bunch of other movement commands that I'll work in this normal mode. So we can move by words. So if I press W, it moves to the next word. See, it's giving forward word by word. If I press B, it goes backward by word and it goes to the start of the word. Kind of the middle of the word, it goes to the beginning of the word. If I press E, that's another command. It goes to the end of the current word and jumps to the next word if it's already at the end of the word. So like if my cursor is here, come to go to the Y in productivity, I can press E to go to the end. If I'm here and I wanna go all the way back to the beginning to the P in productivity, I can press B. If I wanna go back by another word and press B again, it will go to the Y in your, and so on. So that's how you move forward and backward by words. Now there are other useful commands to do kind of similar things in different scopes. So they're commands to move to different parts of the lines. The zero key moves to the beginning of the line. Dollar sign moves to the end of the line. And then the carrot key moves to the first non-white space character in the line. So I ended up to this line a little bit just to demonstrate. If I'm here and I press zero, it goes to the beginning of the line, dollar sign end of line, and carrot this symbol, ship six on your keyboard, moves to the first non-white space character. Any questions so far? Now don't be worried about memorizing these things. They'll become muscle memory soon enough and we'll give you tools to learn these things. There's a program called VimTutor that'll walk you through this kind of stuff. And they're also interactive games and things that people put online that are actually kind of cool. And now there are lots of other movement commands. Demonstration is some other ones. H goes to the top of the screen. M, sorry, capital H goes to the top of the screen. Capital M goes to the middle of the screen. Capital L goes to the bottom of the screen. If I want to move by entire pages, control D goes down by a page, control U goes up by a page. And so that's how you can scroll around in a file. If you want to move by the entire file, if I do lowercase gg, that moves all the way to the beginning of the file, capital G moves all the way to the end of the file, I can go to specific line numbers in the file by typing colon and then the line number where I can type the line number, like one, two, three, capital G, and it goes to that specific line in the file. I'm just overwhelming you with movement commands, but we'll see what these are useful for in a moment, just besides moving around a file. And then you can learn these over time. Some of the kind of useful things, there's a command, a movement command percent, which is move to the corresponding item. This is another kind of neat and useful thing. If my cursor is inside here, well, this is inside a parenthesized block. So if I press percent, my cursor jumps to one of these parentheses and highlights the other. And if I press percent again, it jumps to the matching print and then highlights the other matching thing. So if I'm inside here and I press percent, now my cursor is right here. And if I press percent again, now my cursor jumps to the closing parentheses on the right and highlights the one on the left. Other kind of useful things to navigate around lines, if I press lowercase F and then a particular character, that means find the first occurrence of that character on the current line. And so if I press F and single quote, for example, my cursor jumps to the first single quote here. If I press F and zero, my cursor jumps here. There's also capital F, which is sort of backwards for the first occurrence of the thing. So if my cursor is here and I do capital F single quote, my cursor jumps back here. And there's another variation of F for fine, which is T, which stands for two. So instead of finding a thing, you can jump to a thing, which is jumping right before the first occurrence of a thing. So if I press T zero, it jumps to the zero, but not on top of it. Capital T does the same thing, but backwards. So like if I was here, and a capital T single quote, I jump all the way back here, right before the single quote. Okay, so that's some movement commands in them. And there are a ton more. They're all useful for different purposes and you'll learn them over time. Now one kind of interesting thing is that, right, I told you not just the underlying thing, but the interface is really a programming language. And so if I want to say move down three times, I can press J, J, J to move down, down, down. But I can also do, I can press the number three and then I can press J. That means three times do whatever I do next. And so three Js move down three lines. If I'm, I'll let the people have moved somewhere else in my file. If I'm here and I want to move forward by five words, I can just press five W and it moves forward by five words, three B backwards by three words and so on. And so this is the first exposure again to seeing how these different commands can be composed together to kind of make more powerful editing motions. And then one more thing I'll show you right here before we move on to the next type of thing, which is slow, which we'll talk about is searching. So if you're somewhere in a file and you want to move somewhere else, one really fast way of moving there is by searching for that text. And so what I'm going to do that is in normal mode you press slash forward slash and your cursor will jump down to the bottom and you can type in reg X that you want to search for. So remember my cursor was up here. Say I want to jump down to this capital B editor. If I press slash and type in editor and press enter, my cursor just jumps down to the next occurrence of this. So it's a really fast way of moving around. And one time in the search mode, if I press N, it'll jump to the next occurrence of whatever I just searched for. So here's the next occurrence of capital B editor and then my cursor's down there. And again, it jumps to the next occurrence. If I press capital N, it moves backwards in this list. So N moves me down here, capital N, capital N moves me back here. There I press escape GG to move all the way back to the top of the file. Okay, so some of the movement commands in BIM, these will eventually become muscle memory if you use this thing for a couple of weeks. So that's some of the stuff you can do in normal mode. And another thing you can do is once you actually find where you want the cursor to be, say right here, I can press I to go into insert mode and start typing in text. So I want to put an underscore there and I'm gonna put an underscore at the end of the line and press dollar sign, A for, a lowercase A for append. And then type in my underscore and I'm gonna press escape to go back to normal mode. Usually all editing in BIM will be like, you are in normal mode. You move your cursor somewhere, you go into insert mode, you make whatever change you wanna make and you go back into normal mode. That's what the regular editing workflow looks like. Now if there are modes besides just normal mode and insert mode, remember I talked about doing different kinds of selection? And so there's visual mode, if I press B, I can use hjkl or the arrow keys to move this cursor around and select a block of text. I can also use all the regular movement commands from earlier, so again, you see how these things compose. So if I have this selection mode and I press W, it just jumps a word at a time. If I press 10j, it'll jump 10 lines down and so on. But if I press escape, it cancels and goes back to normal mode, gets rid of my selection. So remember V for visual mode. If I do capital V, it selects by lines rather than selecting from one character to some other character. Again, I can use all the same motions I learned earlier. 3j jumps down by three lines. 3k moves up by three lines and so on. And there's also something called visual block mode which I enter by pressing control B and selects a rectangular chunk of text. And we'll see what selections are useful for in a moment. Any questions so far? Any comments from John or anything? Cool. Are you covering marks later? We were skipping marks. This is such a complicated tool, we're not even gonna cover like 1% of this functionality today, but hopefully I will inspire you to learn more about it on your own. Okay, so now we know how to move around a file somewhat comfortably. We know how to open and close files and save files. How do we actually manipulate text? Well, I think that we should think about it just like this. Before, with your old editor, everything you were doing with the mouse, you're now doing with the keyboard. Like any kind of moving around, like selecting and dragging things around that you're doing with the mouse, there are commands for doing that that are way more efficient. So the most basic thing is you get your cursor somewhere. You press I to enter insert mode, you can start typing characters. There's a different way to enter insert mode which is pressing A and it's like jump forward by a character and then enter insert mode and I can type in characters, press escape to go back to normal mode. But just using Vib's motion commands and then using insert mode to edit my text is not gonna be super efficient. Like say I want to go and change the word productivity to the word high. Well, one way I could do that using the commands I've learned so far is like, okay, I press E to go to the end of the word, press A to move my cursor one to the right and enter insert mode where I can actually edit the text and then press backspace a bunch of times to delete this word. And that's not gonna be super efficient. Instead, there are commands for doing different kinds of manipulations that you can kind of stack or combine with the different motion commands we learned. So, let's see what happens. Before we talk about combining commands, one other kind of useful thing is if you're in normal mode, you press lower case O, it inserts a line below and puts you in insert mode so I can start typing text here. If I press capital O, it inserts a line above and let me start inserting text there. Okay, so now for actual text manipulation commands. So the D command for delete is a command that takes a motion and then applies a deletion to that motion. So if I'm here and I press E, I go to the end of the word, right? If I'm here and I press D, E, it's delete to the end of the word. If I'm here and I press D, does anybody remember the command for end of line? Yeah, dollar sign. So if I press D, dollar sign, it's delete to the end of the line. If I'm here and I press D, zero, it's delete to the start of the line. If I'm here and I press D, carrot, it's delete until the first non-white space character of the line. If I'm here and I press D, three J, it's delete three lines down. Right, so you can combine these pretty sophisticated ways. There, I combine the manipulation command with a number command for repetition and then the actual movement command, right? If I'm here and I want to delete these three words, I can do D, three, W to delete three words, right? The way you probably do this with your old tax editors, you move your hands from the keyboard to the mouse and you go and you select the tax that you wanna delete and you press backspace. It's way slower than D, three, W, especially once it becomes muscle memory, which would work if you use this tool for any amount of time. So other kind of useful things that are variations of say the delete command. Here we deleted three words by doing D, three, W, and then we can press say I to go into insert mode and type in whatever new text you want. But instead of doing D and then the motion, if you press C and then do motion, C is for change. So if I do change word C, W, it'll delete the word and then put me in insert mode so I can type in a new word as a replacement. So if I'm here and I do C, three, W, it changes the next three words, delete the next three words and put me in insert mode so I can type in something different. And now here we're starting to see how there's some overlap between these commands. Like D, three, W, followed by I to put me in insert mode where I can type in new text is the same as C, three, W. But you stay with keystroke by doing the more specific command. I'll tell you about a couple more manipulation commands that are kind of useful. There's another command called X, which is delete the character underneath the cursor. So if I wanna go and delete this period, I can go over it and press X in normal mode and it just gets rid of that period. Now again, there's some overlap here. There's a motion to move right. Remember, HJKL is up, down, up, right? Not in that order though. So L is the key that moves you to the right by one. If I'm here and I do D, L, that actually ends up being the same thing as X. Like delete one thing to the right is the same thing as delete thing underneath the cursor. But you save a character by pressing X so that the commands are kind of useful. There's a command S, which is substitute character. So S, delete the character under the cursor into an insert mode so you can start typing in new things. So that's the same as D, L to delete a character and then I to go into insert mode. It's similar to X followed by I to go into insert mode, but if I just press S, that's a single keystroke that does the same thing as those other things. So that's a couple of these text manipulation commands. You can also apply all these commands like change or delete two selections. So say for example, I go into visual line modes and I'm pressing shift V and then J, J to go down by two lines to select this chunk of text. If I press C, it'll delete this chunk of text and put me in insert mode where I can type in something new. Or if I just select it, capital V for visual line, J, J to select two lines down and press D, it'll delete that block of text that was selected and keep me in normal mode. So again, here you see how all these different things can be combined in whatever way you want. Like here we're combining, well, we're using visual mode then we're using motion commands to select blocks of text like I press five, J, it goes down by five lines and then we're using a manipulation command to actually apply that operation to the block that was selected. Another couple of keys that'll be kind of useful as we play around with this stuff. If you make a change and you want to get rid of it, you can press U to undo. That's how I was undoing all these random deletions I was making. And you can press control R to redo a change. That's another example of operations that were previously in the menus or maybe haven't worked, it's from like control Z and control I. But now we have really efficient decompansions for doing all of these different things rather than like edit, redo or something in the menus. There are lots more exotic manipulation commands to learn and actually they're kind of useful. If you're using this tool for months, you'll probably end up learning all of these different combinations and you will make use of them sometimes. Like here's an example of one kind of exotic thing. If you press the tilde key, which is like the key to the left of your one key, you press shift and press that key. Mark like that on your keyboard, the swivel line. What that does is it flips the case of the character that's under the cursor. So like if I want to remove the capitalization of this W, I can press tilde and it uncapitalizes it. It's like a kind of exotic thing, you probably don't really need it, but if you know the command, it can save you a couple keystrokes. Like how would I do this? Otherwise I might press S to substitute and then lower case W and then escape to go back to normal mode. That's a little bit more accurate than a single keystroke. Another related command that's kind of useful is if you press lowercase R followed by a key, it's replace what's underneath the cursor by something else. Like R, C changes this to C. R capital W changes this to W. It keeps you in normal mode. So again, there are lots of these different manipulation commands that can be combined with selection and with movement commands and with using numbers for repetition that kind of make up the programming language of the interface for this editor. And so how do you actually learn all these different things? Like you're probably gonna forget what I've showed you, right? They're all the Vigilian commands. And they're maybe not so hard to remember because they're kind of demonic. Like D is delete, C is change and S is substitute and so on. But there are lots of nice resources out there for learning these commands. So one thing that's probably pre-installed on your computer already is something called VimTutor. If you enter VimTutor, ideally do this in an 80 by 24 terminal. There you go. Basically VimTutor opens up a special text file which is a VimTutorial. And it will walk you through basic movement commands all the way up to doing pretty advanced things. Like I think if you spend the time to go through this tutorial that you do by using VimTutor, like you will know the tool well enough that you can immediately start using it as a replacement for whatever you were using before without having too much of a hidden productivity. And then as you learn more things that you can do with the tool, you'll end up getting even faster and faster than you were with your previous editor. There's another kind of cool thing, something called VimAdventures. This is a vim-adventures.com. If the terminal-based VimTutor is a little bit too dry for you and wants to be a little bit more entertaining, there's this online game that teaches you how to use the movement commands and manipulation commands and so on. It's actually kind of fun to play through. If you try to use the arrow keys, I think it'll do something similar to what I recommended for you, which is like complaining to you and telling you not to use the arrow keys. If you press the arrow keys, like, oh, don't use the arrow keys, use hjkl instead. And it's a kind of neat thing to explore around your own time as an alternative or supplement to using something like VimTutor. Any questions so far? Time flies. There's so much more we wanted to cover. Basically, I'm going to try to zip through this material to kind of show you some of the more advanced functionality of this tool. Just to kind of inspire you to put the time to learning it. One thing that we touched on briefly earlier was that Vim is configured through this plain text configuration file that lives in your home folder in a file called .vmrc. And again, we'll talk more about .files later. But some useful things you can put in there. I got rid of my, like I have a pretty heavily customized .vmrc and I got rid of it for showing you Vim basics in a Vim setup that looks similar to how it'll look like on your machine. But you can get a lot of things. By putting a bunch of basic settings in here, you can get a lot of nice things in your editor that are not there by default. So like, for example, you can see here I have line numbers on the left. That's a couple of settings that give you line numbers. You can get a setting that highlights text as you search for it by setting a couple of settings. You can have syntax highlighting by just turning it on. Setting the syntax on command. You can, one thing you'll notice if you try playing around with a default Vim configuration, it actually doesn't let you backspace over certain kinds of text. Like if you're at the beginning of a line, for example, you go to insert, go to press backspace, nothing happens. And so there's a, you can change that behavior as well. And so like this is another same thing that you might want in your editor's configuration. And so there are tons of different things you can customize. And there's no way, even if we wanted to like have a two hour long class and all the ways you could customize Vim, there's no way we could cover that all. And so some techniques you could use, people have these nice write-ups on the internet talking about like Vim configuration tips. And so you can look at those. And another really useful thing to look at is that people have their .files on GitHub. We'll link to some of these in our .files lecture. And you can basically take a look at how other people can figure their editor. Like for example, my configuration's all online. You can look at it if you want. And some people actually take the time to document their configuration. And so you can look at these, try out these settings yourself and see if you find them helpful. And so it's definitely worth putting time into customizing your editor. Do you mind if I cut into your time a little bit to show some fancy Vim? No, no problem. Okay. It'll just be efficient. Yeah. Minimize keystrokes. Okay. Yeah. Okay, so I think the last thing I wanna do before John teaches about version control is show you some fancy functionality that everything we've talked about so far you can already do using the tool you used before. But I wanna show you some kind of cool stuff. So let's go back to editing this, the lecture notes for this lecture. Now we have my configuration, so it looks slightly nicer than it did before. One thing you can do, which you probably could do in your older editor is search and replace. So the way you do that is go into X mode, percent s is search and replace in the whole file, and I can do something like slash foo, slash bar, slash g. This looks very similar to the set substitution that John talked about during his data rendering lecture. This is basically write X search for this, replace with this globally in the file. So if I apply this, replace foo with bar, now it replaced that text here, foo with bar, and so now this makes no sense anymore. You can even have it show the replacement line during a substitution. Yeah, so if I do that, I can do replace foo with bar, and I can see it as I type. And so this is more, this is really useful when doing fancier substitutions. Like say I want to take all these things which are like named markdown links, and I wanna get rid of the named link and just keep the like actual URL instead. Well, I can do that by say replacing, okay, I'm gonna like look for an open bracket followed by some text, followed by a closed bracket, followed by an open parentheses, followed by some text that I want to capture, and then replace that with the text I capture globally. And so here I can actually preview that change, and if I press enter it'll take effect. And now I've replaced all the named links in my markdown document with just the raw URL. Press U to undo that change. So that's some fancy search and replace. Another kind of useful thing is split windows. So I think some editors let you open up multiple files, but does your editor let you have multiple views of the same file? Like what if I want to see the bottom 20 lines of text here and the top 20 lines of text here in the same file? Well, you can do that with this tool. So like right now split windows in two, and I've loaded the same file in both splits. So that's another kind of neat functionality. Vip does have much support, which I recommend you don't use, but it's there if you wanna go and enable it. One final thing to show you, what I think is a really cool piece of functionality in them, let me open up a specific file that I prepared in advance. Okay, so say I have a task where I have this XML file, which has like a list of people and each people and it's saying that people list as a person with a name and email address. Like say I have this piece of data, and I want to convert this to JSON, like a different data format. Well, one way I could do it is I could just go ahead and write like a Python program that loads the XML and parses it using whatever XML parsing library and then writes it out as JSON. But maybe for a one-off thing or maybe if it wasn't exactly XML, that's some custom format, like I don't wanna write a parser and write like a full Python program or something to actually do that. So what else could I do? Well, maybe one way I could do it is with these RUGAX search and replace is like I could search and replace, say like I could search for like open like angle bracket name, closed angle bracket stuff followed by this and replace it with like that stuff surrounded by quotes and so on and like apply it onto these RUGAX substitutions. But that can actually get really complicated and sometimes like hard to think about these really fancy RUGAXes. So another cool piece of functionality in Vim is something called Macros. You can tell Vim to record a sequence of commands and you can have it replay that sequence of commands. And you can take a sequence of commands and store it in a register and then like record another sequence of commands and use a saved macro and recording another macro and so on and so you can like kind of interactively build these pieces of functionality kind of like implement small functions that you just implement by demoing it to Vim. And it's actually super powerful. And so here I'll show you an example of how we can convert this XML file into a JSON file just using Vim. So the key commands for doing macros are you should press Q and then a letter. It records a macro, see the bottom recording at W. It records a macro in the register W and then to replay that macro I can do the at sign followed by the name of the key. And so say I go here and I do QW to record in register W press like JJJ, Q to end the recording. Now if I press at W, it replays JJJ. Okay, so that's how macros work. Now how can I use that to convert this XML file into JSON? Well, let's work piece by piece. First let's just manually delete the first and last lines of this file. So capital G goes to the end of the file, DD deletes the line, lowercase GG goes to the start of the file, DD deletes the line. Okay, now just this inner data. Let me first write a macro to format like a single line, change this XML-ish looking thing into something that's more JSON looking. So let me do like QE for element, let me record in the register E. If I press the carrot key, it goes to the first non-white space element on the line. Now I can do R single quote to replace this with a quote. F, close angle bracket to find the next close angle bracket, S to go to substitution mode, quote, colon, space, quote, escape to go back into normal mode, F open angle bracket to jump to this first open angle bracket that's found. Capital C to delete from that point, to the end of the line, go to insert mode, and then quote, escape, Q to start recording. Now if I go to this line and do at E, it just applies all those changes to this line too. Okay, so now I have a macro that will nicely format a single line for me. Let me just go and undo those changes so I can work on a clean document again. Now let me implement a macro to format like one of these elements. So if I go onto the line that has a person on it and do Q say P to format person, capital S to substitute that line, open brace because it's gonna be a JSON object, escape to go back into normal mode, J to go into the next line, at E to replay the macro I stored in E, capital A to go to the end of the line and append going to insert mode comma, because I'm inserting comma there, escape to go back into normal mode, J to go to the next line, at E to replay that macro again, J to go to the next line, capital S to substitute, close brace comma or characters I'm typing in, escape, Q to start recording the macro. Okay, so now I have a macro that should format an entire person for me and I can try that out by doing at P. Okay, so this formats a person. And now all I need to do is apply this to every person in here and so I can record yet another macro. See I'll record this in the rest of your Q to record a macro for the register Q, at P to apply the macro to format a person, J to go down by a line and then Q to start recording. And now I can just replay this macro again and again at Q, at Q, at Q. And if I just do one zero zero at Q, it's like apply this macro 100 times. Just need to apply it a bunch more times. Let's apply it another 100 times. I was typing 1000 at Q and it replays this macro until it fails. So like when I got to the bottom of the file, when I tried to go down further, there's no more file left and so the macro stops playing. And I'm basically done now. I can just clean this up a little bit. I have this extra comma at the end. I can add the close square bracket to end the JSON array and insert an open square bracket here and move this forward. And here we go. This is a valid JSON file that I converted from XML to JSON using BEM. And so this is just a, I didn't really expect you to follow all the details there, but this is just to show you that this editor is capable of doing some pretty sophisticated and powerful things. We have a little bit more in the lecture notes in terms of resources. Like some cool things are the BIM plugins and they do lots of really fancy things and you should look into those. Like here's an example of seeing the undo history, visualizes a tree of changes. You navigate that super easily. Or tons of BIM plugins out there that do cool and useful things. So you should look into those and those are all in the lecture notes. So any questions about text editors? I convinced you all to learn a text editor really well. Well, okay, so let's take a 10 minute break and then we will resume with version control. Yeah.