1 00:00:00,000 --> 00:00:02,590 [Seminar Vim] 2 00:00:02,590 --> 00:00:04,210 [Brandon Liu] [Harvard University] 3 00:00:04,210 --> 00:00:07,500 [This is CS50.] [CS50.TV] 4 00:00:07,500 --> 00:00:09,880 >> Today is going to be an introduction to Vim. 5 00:00:09,880 --> 00:00:12,310 How many of you guys have heard of Vim? 6 00:00:12,310 --> 00:00:15,620 What have you heard about Vim? 7 00:00:15,620 --> 00:00:17,590 [Student] It solves all your problems. 8 00:00:17,590 --> 00:00:19,450 [Brandon L.] It solves all your problems. 9 00:00:19,450 --> 00:00:23,840 Anything else you guys have heard about Vim? Any impressions? 10 00:00:23,840 --> 00:00:27,990 [Student] Almost everyone I've talked to likes to use Vim. 11 00:00:27,990 --> 00:00:29,960 Oh, sweet. 12 00:00:29,960 --> 00:00:31,090 [Student] A lot of people I know like to use Vim. 13 00:00:31,090 --> 00:00:35,050 Okay, that's good, because a lot of people in CS51 14 00:00:35,050 --> 00:00:38,000 start to pick up Emacs because Emacs has slightly better support 15 00:00:38,000 --> 00:00:40,100 for the language you're going to use in CS51, 16 00:00:40,100 --> 00:00:42,770 and I worry that not enough people at Harvard use Vim, 17 00:00:42,770 --> 00:00:46,190 so I really try and do a good job of encouraging Vim usage. 18 00:00:46,190 --> 00:00:51,110 >> But here is a classic xkcd comic which you guys might find interesting. 19 00:00:51,110 --> 00:00:53,800 Have you guys used Nano before at all? 20 00:00:53,800 --> 00:00:57,380 This comic says Nano? Real programmers use Emacs. 21 00:00:57,380 --> 00:00:59,040 Hey, real programmers use Vim. 22 00:00:59,040 --> 00:01:03,130 No, real programmers use Ed which is this—you don't ever want to use Ed. 23 00:01:03,130 --> 00:01:05,990 It's really, really old and really bad, and real programmers use 24 00:01:05,990 --> 00:01:08,440 a magnetized needle and a steady hand. 25 00:01:08,440 --> 00:01:16,010 And you can read the rest, but it's pretty amusing. 26 00:01:16,010 --> 00:01:18,320 And the funny thing is at the end. 27 00:01:18,320 --> 00:01:20,310 They say, "Oh, yeah, of course, there's an Emacs command to do that." 28 00:01:20,310 --> 00:01:25,050 Good old C-x M-c M-butterfly, which is sort of a jab at Emacs 29 00:01:25,050 --> 00:01:28,520 because Emacs is historically known to be a little bit bloated 30 00:01:28,520 --> 00:01:32,310 and to have commands for almost anything, so that's sort of a joke for Emacs. 31 00:01:32,310 --> 00:01:35,320 >> What is Vim? Vim is an advanced programmer's text editor. 32 00:01:35,320 --> 00:01:40,040 I really want to make the distinction between text editor 33 00:01:40,040 --> 00:01:42,000 and something like a document processor. 34 00:01:42,000 --> 00:01:44,340 Microsoft Word is known as a document processor. 35 00:01:44,340 --> 00:01:49,090 You can set margins, make things bold, underline things, 36 00:01:49,090 --> 00:01:51,480 strike through things, change font size and whatnot. 37 00:01:51,480 --> 00:01:54,380 A text editor does nothing more than just edit text. 38 00:01:54,380 --> 00:01:56,020 Literally you just have characters. 39 00:01:56,020 --> 00:02:01,650 There's no sort of styling, sizes, fonts, colors, or anything like that. 40 00:02:01,650 --> 00:02:06,360 All you do is edit text, and Vim has been especially made for programming. 41 00:02:06,360 --> 00:02:09,240 It has a lot of features built in just for programming. 42 00:02:09,240 --> 00:02:11,360 It's designed expressly for efficient text editing. 43 00:02:11,360 --> 00:02:15,190 >> In general, Vim is known to be basically the fastest text editor 44 00:02:15,190 --> 00:02:19,110 you can possibly use compared with any other text editor that's possible. 45 00:02:19,110 --> 00:02:22,480 A lot of IDE-like features are not built directly into Vim, 46 00:02:22,480 --> 00:02:26,850 so if you've used an IDE and you're used to things like compile and run automatically 47 00:02:26,850 --> 00:02:30,880 or debugging what's in your IDE or a whole host of different things 48 00:02:30,880 --> 00:02:32,290 Vim doesn't come with that naturally, 49 00:02:32,290 --> 00:02:36,330 but there's a whole host of plugins that you can use to add that sort of functionality to Vim. 50 00:02:36,330 --> 00:02:40,890 Vim by itself focuses on text editing and does text editing really, really well. 51 00:02:40,890 --> 00:02:43,340 And finally, it's really customizable and extensible, 52 00:02:43,340 --> 00:02:48,740 and basically Vim has been around, or Vi, which Vim is built upon, 53 00:02:48,740 --> 00:02:53,790 has been around for over 40 years, and it is a very, very deep program, 54 00:02:53,790 --> 00:02:58,850 and people will use it for a lifetime and never truly master the program to its fullest, 55 00:02:58,850 --> 00:03:01,530 and you will always continue learning throughout your life over the years, 56 00:03:01,530 --> 00:03:03,390 always becoming better and better. 57 00:03:03,390 --> 00:03:07,480 >> There's basically no end to what you can learn and how much you can improve with Vim, 58 00:03:07,480 --> 00:03:09,300 which is really exciting. 59 00:03:09,300 --> 00:03:12,730 You don't want to view it as something scary because you can become very, very proficient 60 00:03:12,730 --> 00:03:16,650 in Vim in just a few weeks but it's sort of like a continual— 61 00:03:16,650 --> 00:03:18,870 you can always learn more and learn more and do things better and better, 62 00:03:18,870 --> 00:03:20,880 which is kind of exciting for me. 63 00:03:20,880 --> 00:03:23,810 This is an example of what Vim looks like. 64 00:03:23,810 --> 00:03:27,630 This is not mine. You'll see some live demonstrations right now. 65 00:03:27,630 --> 00:03:29,350 This is an example of what Vim might look like. 66 00:03:29,350 --> 00:03:33,590 >> What we're going to do today is we're going to start off with 67 00:03:33,590 --> 00:03:35,260 a quick showcase of what Vim can do. 68 00:03:35,260 --> 00:03:39,040 I'm going to do some tricks and whatnot for you guys 69 00:03:39,040 --> 00:03:43,020 so you can see why I like Vim so much and why people like Vim so much, 70 00:03:43,020 --> 00:03:46,470 and then from there we'll go into a gentle introduction. 71 00:03:46,470 --> 00:03:49,530 Actually, it will be a bit fast, an introduction to Vim. 72 00:03:49,530 --> 00:03:53,410 I'll ask you guys to download a text file and to follow along with me if you can. 73 00:03:53,410 --> 00:03:58,080 This is Vim, and right here I'm using MacVim, 74 00:03:58,080 --> 00:04:00,520 so if you guys have Macs you may want to consider using MacVim 75 00:04:00,520 --> 00:04:04,210 because it's a graphical version of Vim, although the regular Vim, 76 00:04:04,210 --> 00:04:06,210 what's in your command line, works just fine. 77 00:04:06,210 --> 00:04:09,890 I'm going to show you a few random tips and tricks 78 00:04:09,890 --> 00:04:15,330 that you can use in Vim to show you why it's so cool. 79 00:04:15,330 --> 00:04:18,940 >> Just a few random things I'm going to go through. 80 00:04:18,940 --> 00:04:21,700 The first thing is text objects, and there are a few commands around text objects, 81 00:04:21,700 --> 00:04:25,010 and I'll actually teach you what text objects are in more detail later in the seminar, 82 00:04:25,010 --> 00:04:30,050 but for example, let's say I spelled my name wrong, 83 00:04:30,050 --> 00:04:32,380 Barndon Liu, and I want to change it. 84 00:04:32,380 --> 00:04:36,140 All I have to do is get my cursor somewhere in my name and type in the command 85 00:04:36,140 --> 00:04:38,900 ciw, and it will delete my whole name, put me in insert mode, 86 00:04:38,900 --> 00:04:41,050 and I can type in my name and fix it. 87 00:04:41,050 --> 00:04:45,550 Similarly, if I want to change something inside quotes there's also a command for that. 88 00:04:45,550 --> 00:04:47,330 I can delete things inside quotes. 89 00:04:47,330 --> 00:04:50,260 No more having to double click, move your hand over to the mouse, 90 00:04:50,260 --> 00:04:52,230 double click, and then type something back in. 91 00:04:52,230 --> 00:05:02,000 You can do everything from the keyboard, whatever it is. 92 00:05:02,000 --> 00:05:06,430 Or alternatively, let's say I have this image tag inside this anchor tag. 93 00:05:06,430 --> 00:05:08,160 >> Have you guys covered HTML in class yet? 94 00:05:08,160 --> 00:05:10,570 Yeah, so maybe I want to delete this tag, 95 00:05:10,570 --> 00:05:13,080 which normally what you have to do is you have to use your mouse 96 00:05:13,080 --> 00:05:16,040 and carefully highlight exactly what you want to delete and delete it. 97 00:05:16,040 --> 00:05:21,320 But in Vim that's simply dit, and you can delete it out really quickly, 98 00:05:21,320 --> 00:05:28,020 and I'll explain to you what all those commands mean and how you use them in a second. 99 00:05:28,020 --> 00:05:30,320 Another example, I have some code here, 100 00:05:30,320 --> 00:05:33,640 and let's say I want to indent the code, but I want to indent the code 101 00:05:33,640 --> 00:05:39,940 just within the parentheses, and I can also indent the code using 3 commands 102 00:05:39,940 --> 00:05:45,360 using basically >iB. 103 00:05:45,360 --> 00:05:49,960 Another thing that's really cool about Vim is something called visual block editing. 104 00:05:49,960 --> 00:05:52,380 I don't know if you guys have ever wanted to highlight 105 00:05:52,380 --> 00:05:55,690 a rectangular block of text in your code sometimes 106 00:05:55,690 --> 00:06:00,570 instead of having to do this weird sort of line thing that spans all different lines. 107 00:06:00,570 --> 00:06:02,450 For example, in Vim you can do that. 108 00:06:02,450 --> 00:06:05,260 I can highlight rectangles like this, which is pretty cool, 109 00:06:05,260 --> 00:06:07,950 but furthermore, I can edit using that functionality. 110 00:06:07,950 --> 00:06:12,050 For example, if I want to give some sort of prefix to all of our names 111 00:06:12,050 --> 00:06:18,100 I can highlight a block and then add something. 112 00:06:18,100 --> 00:06:22,060 >> Similarly, I can also highlight this block and choose to delete it, 113 00:06:22,060 --> 00:06:26,300 or maybe I'll highlight this block and change it to D because we're all doctors. 114 00:06:26,300 --> 00:06:29,430 Whatever it is, you can do all those different things. 115 00:06:29,430 --> 00:06:31,270 Finally, folding is really cool. 116 00:06:31,270 --> 00:06:33,840 A lot of IDEs have this where you can fold different functions out of view 117 00:06:33,840 --> 00:06:35,590 in case you don't want to see them. 118 00:06:35,590 --> 00:06:40,120 For example, suppose I have this bunch of text here, and I don't really want to see it. 119 00:06:40,120 --> 00:06:42,710 I want to keep it out of view because I'm editing something else in the file. 120 00:06:42,710 --> 00:06:50,580 I can fold it up so it's all collapsed, 121 00:06:50,580 --> 00:06:52,150 and that way it's out of view, 122 00:06:52,150 --> 00:06:54,310 and I can edit other things that are on the page, and this is really great. 123 00:06:54,310 --> 00:06:57,550 You can fold up functions and whatnot or fold up classes when you're programming 124 00:06:57,550 --> 00:06:59,380 just to keep them out of view when you're not editing them, 125 00:06:59,380 --> 00:07:04,330 and later when you want to see them you can open them up. 126 00:07:04,330 --> 00:07:09,260 And you can see it just opened everything up. 127 00:07:09,260 --> 00:07:11,220 Tabs, buffers, and windows. 128 00:07:11,220 --> 00:07:13,260 Vim is pretty cool in that 129 00:07:13,260 --> 00:07:16,280 normally in a lot of text editors you can only edit one file at a time, 130 00:07:16,280 --> 00:07:18,960 like in Nano, or maybe if you're using Notepad or Gedit. 131 00:07:18,960 --> 00:07:21,470 I've actually never used Gedit very much, but at least in Notepad++ 132 00:07:21,470 --> 00:07:26,700 you can have tabs, but Vim has the concept of tabs, buffers and windows. 133 00:07:26,700 --> 00:07:28,760 For example, you can open up a bunch of new tabs. 134 00:07:28,760 --> 00:07:31,790 >> You can see here at the very top I have now a bunch of tabs, 135 00:07:31,790 --> 00:07:34,350 and I can close them. 136 00:07:34,350 --> 00:07:37,550 Furthermore, there's this concept of buffers, 137 00:07:37,550 --> 00:07:40,110 which is the idea that once you've opened a file 138 00:07:40,110 --> 00:07:43,910 it stays alive in the buffer, and if you want to navigate to that file really quickly 139 00:07:43,910 --> 00:07:46,290 you can navigate to it really quickly within the buffer as well, 140 00:07:46,290 --> 00:07:51,890 and furthermore, within one pane you can also split your window up into multiple windows. 141 00:07:51,890 --> 00:07:55,890 For example, if I want to split this window up into multiple windows 142 00:07:55,890 --> 00:08:00,080 you can see right here that I can have the same code twice, 143 00:08:00,080 --> 00:08:03,800 and I can split more and more and more, and right now it doesn't look that good 144 00:08:03,800 --> 00:08:07,340 because by default I have the default window size be kind of big 145 00:08:07,340 --> 00:08:09,490 so all the others are squashed out, 146 00:08:09,490 --> 00:08:12,000 but normally when I'm on my computer and not on this projector 147 00:08:12,000 --> 00:08:15,920 I can have several panes open at the same time, which is really nice for 148 00:08:15,920 --> 00:08:21,450 editing multiple files at the same time. 149 00:08:21,450 --> 00:08:23,800 You can also have things like select and replace, 150 00:08:23,800 --> 00:08:27,900 and we'll actually cover that in the seminar, 151 00:08:27,900 --> 00:08:29,250 so I'll skip that for now. 152 00:08:29,250 --> 00:08:32,039 You can also interact with the command line in all sorts of different ways. 153 00:08:32,039 --> 00:08:36,380 For example, if I have some sort of list here and I want to sort it— 154 00:08:36,380 --> 00:08:43,039 I don't know, let's add another fruit, grape, 155 00:08:43,039 --> 00:08:45,450 and I can highlight this using the highlight function 156 00:08:45,450 --> 00:08:48,980 and call sort, and it will sort it automatically for me. 157 00:08:48,980 --> 00:08:52,150 >> Similarly, if you're going to be editing CSS, which I presume you probably will be doing 158 00:08:52,150 --> 00:08:54,190 for your final project, most people do, 159 00:08:54,190 --> 00:09:00,200 and maybe you want to keep your CSS organized by sorting them in alphabetical order. 160 00:09:00,200 --> 00:09:04,410 You can also highlight them and call sort, and it will sort them for you. 161 00:09:04,410 --> 00:09:07,380 These are some of the examples of the things that Vim can do, 162 00:09:07,380 --> 00:09:10,760 and you'll notice that the key thing that I really want to draw your attention to 163 00:09:10,760 --> 00:09:14,740 is that I'm doing everything from the keyboard, 164 00:09:14,740 --> 00:09:18,020 and if you are able to see where my hands are placed 165 00:09:18,020 --> 00:09:21,210 you'll also see that my hands never really move from the home row. 166 00:09:21,210 --> 00:09:24,790 Normally when you're editing text you usually have to use a mouse 167 00:09:24,790 --> 00:09:28,030 and click around and whatnot, and that sort of movement drags you down 168 00:09:28,030 --> 00:09:32,280 and wastes time, or perhaps you're moving between the home row and the arrow keys, 169 00:09:32,280 --> 00:09:35,620 and so you're moving back and forth like that, which is a bit cumbersome 170 00:09:35,620 --> 00:09:38,750 but also slows you down, and the thing is once you get really good at Vim 171 00:09:38,750 --> 00:09:42,350 everything you want to do is done without having to move your hands 172 00:09:42,350 --> 00:09:50,730 from the home row, which is great. 173 00:09:50,730 --> 00:09:54,870 Hold on, I'm going to open up this application. There we go. 174 00:09:54,870 --> 00:09:58,320 >> Now you can see what commands are running down the lower right window. 175 00:09:58,320 --> 00:10:03,190 All the functions that I just showed you are things that are included by default with Vim, 176 00:10:03,190 --> 00:10:06,820 but there's also a huge number of plugins that you can also use 177 00:10:06,820 --> 00:10:08,390 to improve your Vim experience. 178 00:10:08,390 --> 00:10:10,540 If you downloaded the configuration files that I included, 179 00:10:10,540 --> 00:10:13,150 and there are instructions on how to install them in the PowerPoint 180 00:10:13,150 --> 00:10:17,840 and also in the email I sent out, I have about 40 plugins installed 181 00:10:17,840 --> 00:10:20,490 or maybe 30 plugins, and every single one 182 00:10:20,490 --> 00:10:24,000 brings additional functionality to Vim that improves your Vim experience. 183 00:10:24,000 --> 00:10:26,960 For example, one of my favorites is something called surround.vim, 184 00:10:26,960 --> 00:10:29,760 and it basically allows you to work with surrounding 185 00:10:29,760 --> 00:10:32,200 or delimiting characters much more easily. 186 00:10:32,200 --> 00:10:35,960 >> One example, I don't know if you guys ever code in LaTeX, 187 00:10:35,960 --> 00:10:38,830 but LaTeX is a math typesetting language that is used to make 188 00:10:38,830 --> 00:10:42,950 really nice looking math, and whenever you want something to look like math 189 00:10:42,950 --> 00:10:47,050 you wrap it in dollar signs, and let's say I'm typing some equation like F=ma, 190 00:10:47,050 --> 00:10:49,240 and I forgot to wrap it in dollar signs. 191 00:10:49,240 --> 00:10:58,270 Then I can highlight this and wrap it in dollar signs just like that. 192 00:10:58,270 --> 00:11:03,640 Alternatively, Vim allows you to delete things inside tags 193 00:11:03,640 --> 00:11:07,300 and delete tags and whatnot, but Vim by nature 194 00:11:07,300 --> 00:11:10,400 doesn't allow you to change tags, so for example, I have this div tag, 195 00:11:10,400 --> 00:11:13,060 and I want to upgrade to HTML5 and use a new header tag. 196 00:11:13,060 --> 00:11:22,290 I can do change surrounding tag and type in header, 197 00:11:22,290 --> 00:11:26,540 and it will automatically change that for me. 198 00:11:26,540 --> 00:11:29,620 Similarly, let's say we have something in parentheses, 199 00:11:29,620 --> 00:11:33,230 and I wanted to change that from parentheses into brackets or something like that. 200 00:11:33,230 --> 00:11:36,520 I can also change that really easily by typing in c for change, 201 00:11:36,520 --> 00:11:39,330 s for surround, parentheses. 202 00:11:39,330 --> 00:11:42,170 Oh, I guess I have to type it faster. 203 00:11:42,170 --> 00:11:45,380 Parentheses and braces or brackets, and it will automatically change that for me, 204 00:11:45,380 --> 00:11:48,570 which is really nice, otherwise normally it's really cumbersome. 205 00:11:48,570 --> 00:11:51,310 >> This is really nice to switch between perhaps single quote 206 00:11:51,310 --> 00:11:53,060 and double quote strings or something like that. 207 00:11:53,060 --> 00:11:56,250 That's one example use case. 208 00:11:56,250 --> 00:11:58,750 There's a plugin called Fugitive 209 00:11:58,750 --> 00:12:00,260 which integrates with Git. 210 00:12:00,260 --> 00:12:03,000 I'm guessing maybe you guys have not used Git that much in CS50, 211 00:12:03,000 --> 00:12:05,530 but you'll find yourself using Git more and more and more, 212 00:12:05,530 --> 00:12:09,530 not only in classes throughout Harvard but also at jobs and whatnot, 213 00:12:09,530 --> 00:12:12,650 and normally you have to switch back and forth between 214 00:12:12,650 --> 00:12:15,950 the command line and Vim to interact with Git, 215 00:12:15,950 --> 00:12:18,590 but there's plugins to integrate with Git from within Vim 216 00:12:18,590 --> 00:12:23,020 and also to give you even additional functionality on top of Git. 217 00:12:23,020 --> 00:12:25,520 There's something called NERDTree, which you can use to explore 218 00:12:25,520 --> 00:12:27,860 the file structures from within Vim. 219 00:12:27,860 --> 00:12:31,710 >> For example, I can open up NERDTree on the right pane, 220 00:12:31,710 --> 00:12:35,100 and you can basically see random files 221 00:12:35,100 --> 00:12:38,720 and open—not taxes, don't look at my taxes. 222 00:12:38,720 --> 00:12:43,460 You can look at files and whatnot and open other files and so on and so forth. 223 00:12:43,460 --> 00:12:47,610 And I won't go into these other plugins, so we can basically dive in 224 00:12:47,610 --> 00:12:51,640 and start working on Vim, but there are plugins that allow you to 225 00:12:51,640 --> 00:12:55,420 comment more easily, plugins that allow you to jump in between files more easily, 226 00:12:55,420 --> 00:12:57,820 plugins that give you syntax checking from within Vim, 227 00:12:57,820 --> 00:13:00,210 which is really cool, and it gives you syntax checking 228 00:13:00,210 --> 00:13:03,710 for most languages right out of the box, 229 00:13:03,710 --> 00:13:07,460 things like rainbow parentheses, which is actually really, really cool. 230 00:13:07,460 --> 00:13:09,110 Let me give you an example of that right now. 231 00:13:09,110 --> 00:13:11,150 >> Let's see. 232 00:13:11,150 --> 00:13:15,460 Here's some random code that I was writing, 233 00:13:15,460 --> 00:13:19,720 and you'll notice that the brackets and parentheses are all colored, 234 00:13:19,720 --> 00:13:26,120 and parentheses and brackets of the same color are matching, which is nice 235 00:13:26,120 --> 00:13:29,380 and sometimes improves the readability of your code. 236 00:13:29,380 --> 00:13:32,030 You also have autocompletion, tabular formatting. 237 00:13:32,030 --> 00:13:37,240 When you have something that you want to get all the comments to line up 238 00:13:37,240 --> 00:13:40,820 or you want to make a table in your text and you want to 239 00:13:40,820 --> 00:13:43,870 get everything to look like a nice table, instead of having to format that manually 240 00:13:43,870 --> 00:13:46,830 there are plugins that will format that for you and so on and so forth. 241 00:13:46,830 --> 00:13:49,400 The list goes on and on and on. 242 00:13:49,400 --> 00:13:53,890 I could spend hours exploring all the different Vim plugins that exist, 243 00:13:53,890 --> 00:13:59,780 and it's incredible what's out there. 244 00:13:59,780 --> 00:14:05,880 >> Any questions so far? Sweet. 245 00:14:05,880 --> 00:14:10,180 In that case, we're going to start playing around with Vim, 246 00:14:10,180 --> 00:14:12,620 and I want you guys to work with some Vim with me 247 00:14:12,620 --> 00:14:17,650 so you can get a feel for it, and I can guide you in your first steps working with Vim 248 00:14:17,650 --> 00:14:20,590 because a lot of people get overwhelmed when they open Vim up, 249 00:14:20,590 --> 00:14:24,730 and sometimes if you don't even know the command to exit you can't even get out of Vim, 250 00:14:24,730 --> 00:14:26,350 but it's not that bad. 251 00:14:26,350 --> 00:14:28,820 You just need a few minutes of guidance from someone who has used Vim before 252 00:14:28,820 --> 00:14:32,220 or to read some tutorials online, and you'll be on your way in no time. 253 00:14:32,220 --> 00:14:34,570 Here are some instructions to install Vim if you haven't already. 254 00:14:34,570 --> 00:14:39,130 You can look at these slides after the presentation if you want to install more things. 255 00:14:39,130 --> 00:14:42,660 >> Also, these are some instructions for installing my configuration files, 256 00:14:42,660 --> 00:14:48,230 and you can also follow these instructions later if you haven't already. 257 00:14:48,230 --> 00:14:52,390 If you guys could go to this link and download a text file. 258 00:14:52,390 --> 00:14:56,640 The link will take you to this text file, which I would like you to download and open up in Vim, 259 00:14:56,640 --> 00:15:01,580 and there are a bunch of exercises and whatnot in there that you can follow around with me 260 00:15:01,580 --> 00:15:09,160 and start getting some practice with some of the basic operations in Vim. 261 00:15:09,160 --> 00:15:12,740 Good. 262 00:15:12,740 --> 00:15:18,430 While you're opening that up I'm going to give a brief intro to 2 really key principles 263 00:15:18,430 --> 00:15:23,080 in Vim editing which are really important to understand before you dive into Vim, 264 00:15:23,080 --> 00:15:25,720 and that's modal editing and operators and motions. 265 00:15:25,720 --> 00:15:28,600 I'll cover modal editing first, and then we'll cover operators and motions 266 00:15:28,600 --> 00:15:30,980 in a little bit. 267 00:15:30,980 --> 00:15:36,910 >> The first key distinction between Vim and pretty much all other 268 00:15:36,910 --> 00:15:39,870 text editors of any sort is that Vim is a modal editor, 269 00:15:39,870 --> 00:15:42,260 and what that means is when you're using Vim 270 00:15:42,260 --> 00:15:44,120 you could be in one of many different modes. 271 00:15:44,120 --> 00:15:46,590 There's normal mode, there's insert mode, there's visual mode, 272 00:15:46,590 --> 00:15:50,790 so on and so forth, and the thing is, your keystrokes take on different actions, 273 00:15:50,790 --> 00:15:54,160 take on different meaning, depending on what mode you're in. 274 00:15:54,160 --> 00:15:58,110 Just as an example, normal mode is the mode you're in when you move around, 275 00:15:58,110 --> 00:16:01,820 and so I would use the key J to move downward, 276 00:16:01,820 --> 00:16:05,430 but in insert mode, you use the key J to actually type out J on the screen, 277 00:16:05,430 --> 00:16:08,230 and that's why a lot of people get confused because they're not used to 278 00:16:08,230 --> 00:16:11,630 being in different modes all the time, and so they'll be typing things out, 279 00:16:11,630 --> 00:16:14,490 and you're used to having whatever you type appear on the screen, 280 00:16:14,490 --> 00:16:18,370 but in Vim maybe your cursor is moving all over the place 281 00:16:18,370 --> 00:16:21,020 because you're typing weird characters, and suddenly you get 282 00:16:21,020 --> 00:16:24,710 shifted into insert mode because you typed some key 283 00:16:24,710 --> 00:16:27,150 that puts you into insert mode, and then text starts flying out, 284 00:16:27,150 --> 00:16:29,940 so people get confused because of that. 285 00:16:29,940 --> 00:16:32,960 But you have to keep track of what mode you're in, 286 00:16:32,960 --> 00:16:35,260 and if you look at the very bottom toolbar of Vim 287 00:16:35,260 --> 00:16:37,410 there should be an indicator that tells you what mode you're currently in, 288 00:16:37,410 --> 00:16:39,350 which should be helpful. 289 00:16:39,350 --> 00:16:42,650 >> But in general, the 3 main modes you should know about, 290 00:16:42,650 --> 00:16:46,440 there are only probably 6 or 7 or 8 modes or so, 291 00:16:46,440 --> 00:16:49,180 but you spend most of your time in 3. 292 00:16:49,180 --> 00:16:52,150 The first thing is normal mode. This is where you spend most of your time. 293 00:16:52,150 --> 00:16:55,620 All keystrokes are logged as some sort of command, 294 00:16:55,620 --> 00:16:58,870 and we'll be learning a lot of those commands throughout the day today. 295 00:16:58,870 --> 00:17:03,160 The other mode is insert mode, which is where you actually are typing things out, 296 00:17:03,160 --> 00:17:06,410 and insert mode is basically the mode you're usually used to. 297 00:17:06,410 --> 00:17:08,650 You type something, and it just comes on the screen, 298 00:17:08,650 --> 00:17:11,800 and visual mode is something where you can do highlighting. 299 00:17:11,800 --> 00:17:14,750 You saw earlier where sometimes I would be highlighting text 300 00:17:14,750 --> 00:17:21,900 and manipulating it in different ways, and that's an example using visual mode. 301 00:17:21,900 --> 00:17:27,819 >> We'll start off with the absolute basics you need to survive in Vim, 302 00:17:27,819 --> 00:17:30,610 and it's basically these 7 commands right here. 303 00:17:30,610 --> 00:17:34,250 HJKL are used to navigate around the file, 304 00:17:34,250 --> 00:17:37,140 and they correspond to left, down, up, and right, 305 00:17:37,140 --> 00:17:39,460 and you're free to use the arrow keys as well, 306 00:17:39,460 --> 00:17:43,110 but we really encourage you to use HJKL as much as possible 307 00:17:43,110 --> 00:17:46,690 because the idea is that while you're editing, you can move around the document 308 00:17:46,690 --> 00:17:49,350 without having to shift in between the arrow keys 309 00:17:49,350 --> 00:17:52,210 and where you're typing, which is really, really nice. 310 00:17:52,210 --> 00:17:54,390 You just want to move a few characters over. 311 00:17:54,390 --> 00:17:58,020 You don't have to shift your entire hand over, and it's kind of nice to have that. 312 00:17:58,020 --> 00:18:01,920 Some Vim experts actually map their arrow keys to other functions 313 00:18:01,920 --> 00:18:06,880 because they already have HJKL, and they can use their arrow keys for other functions as well. 314 00:18:06,880 --> 00:18:09,080 HJKL moves you around while you're in normal mode. 315 00:18:09,080 --> 00:18:11,650 If you want to go into insert mode and type something 316 00:18:11,650 --> 00:18:15,370 you can press i for insertion mode, and that puts you in insert mode, 317 00:18:15,370 --> 00:18:17,820 and you can type something, and you type escape 318 00:18:17,820 --> 00:18:20,270 to get from insertion mode back to normal mode. 319 00:18:20,270 --> 00:18:23,280 Escape always sends you back to normal mode 320 00:18:23,280 --> 00:18:26,740 no matter what mode you're in, and if you want to leave 321 00:18:26,740 --> 00:18:29,760 you can type in :wq enter, 322 00:18:29,760 --> 00:18:32,640 and the way you can remember this is w stands for write, 323 00:18:32,640 --> 00:18:34,960 and q stands for quit, so :wq, 324 00:18:34,960 --> 00:18:39,680 and colon runs the command, and so :wq enter to run the command 325 00:18:39,680 --> 00:18:41,840 saves and exits. 326 00:18:41,840 --> 00:18:46,700 >> If you open the file up we'll go through the first exercise together. 327 00:18:46,700 --> 00:18:49,450 Step 1 is surviving in Vim, 328 00:18:49,450 --> 00:18:53,490 and hopefully you guys all have this open. 329 00:18:53,490 --> 00:18:58,460 Good, and we have some simple exercises for you to get used to using these keys. 330 00:18:58,460 --> 00:19:03,000 HJKL really doesn't seem intuitive at first, 331 00:19:03,000 --> 00:19:08,710 and this is one of the initial blocks that people have while using Vim 332 00:19:08,710 --> 00:19:10,820 is using these keys to navigate around. 333 00:19:10,820 --> 00:19:13,650 It's not intuitive, but once you get used to it 334 00:19:13,650 --> 00:19:19,590 it really integrates into your mind, and many people often find themselves 335 00:19:19,590 --> 00:19:23,550 using J and K in Word or also in Chrome, 336 00:19:23,550 --> 00:19:26,530 and actually people have created this Chrome plugin called Vimium 337 00:19:26,530 --> 00:19:29,220 which you can use in Chrome or Chromium to navigate through Chrome 338 00:19:29,220 --> 00:19:31,380 using your keyboard using Vim-like shortcuts, 339 00:19:31,380 --> 00:19:35,050 so people are very enthusiastic about it, but go ahead and try and use 340 00:19:35,050 --> 00:19:41,720 HJKL to navigate between the numbers just so you can get familiar 341 00:19:41,720 --> 00:19:45,660 with how these things work. 342 00:19:45,660 --> 00:19:50,440 >> Once you finish that, you can also go and play around with this maze I created 343 00:19:50,440 --> 00:19:53,580 just for fun, and you can sort of follow the dotted line 344 00:19:53,580 --> 00:19:59,060 and try not to crash into the walls 345 00:19:59,060 --> 00:20:07,860 and try to make it to the end of the maze. 346 00:20:07,860 --> 00:20:11,840 If you use shortcuts in Gmail or any number of different applications 347 00:20:11,840 --> 00:20:15,130 you'll find that J and K, for example, are often used to move up and down, 348 00:20:15,130 --> 00:20:19,930 and that basically comes from Vim essentially, so you have J and K, 349 00:20:19,930 --> 00:20:22,690 and it's really nice because you can move up and down without having to 350 00:20:22,690 --> 00:20:26,210 move your hand back and forth from the arrow keys. 351 00:20:26,210 --> 00:20:29,410 Now let's try going into insertion mode and getting out of insertion mode. 352 00:20:29,410 --> 00:20:34,370 You'll notice that right now my cursor is a big, black box, 353 00:20:34,370 --> 00:20:37,300 and when you go into insertion mode you should find that 354 00:20:37,300 --> 00:20:39,670 it turns into a narrow cursor like you're used to. 355 00:20:39,670 --> 00:20:43,730 If you're using Vim in the terminal you may not find this to be the case 356 00:20:43,730 --> 00:20:46,970 because the terminal doesn't support this kind of thin cursor, 357 00:20:46,970 --> 00:20:49,890 but if you're using MacVim or GVim on Windows 358 00:20:49,890 --> 00:20:54,590 then you should find that it turns into a cursor, and that's a really quick visual indicator. 359 00:20:54,590 --> 00:20:58,270 It's for you to tell whether you're in normal mode or insertion mode. 360 00:20:58,270 --> 00:21:02,890 >> Let's go ahead and try editing this line. 361 00:21:02,890 --> 00:21:06,220 Fill it in with your own information or whatever it is, 362 00:21:06,220 --> 00:21:11,750 and how you can do that is you can move up to the end of the blank, 363 00:21:11,750 --> 00:21:16,040 type i to get into insertion mode, and you can backspace 364 00:21:16,040 --> 00:21:21,100 and type in your name, and when you're done typing 365 00:21:21,100 --> 00:21:26,520 you can press escape to get out of insertion mode, 366 00:21:26,520 --> 00:21:35,930 and you can move over here, type insert to delete that, 367 00:21:35,930 --> 00:21:42,700 type escape to get out of insertion mode, and you can type :w enter 368 00:21:42,700 --> 00:21:44,720 to save the file. 369 00:21:44,720 --> 00:21:47,420 You can type in just :w to save the file. 370 00:21:47,420 --> 00:21:50,560 You can type in just :q to quit without saving, 371 00:21:50,560 --> 00:21:53,750 or you can type in :wq to save and quit. 372 00:21:53,750 --> 00:21:58,850 Granted, we don't want to quit yet, so you can type in :w. 373 00:21:58,850 --> 00:22:01,780 And maybe that seemed a little inefficient at first 374 00:22:01,780 --> 00:22:05,600 because to move to that spot you had to type J a bunch of times, 375 00:22:05,600 --> 00:22:08,670 maybe L a bunch of times to get to that spot, 376 00:22:08,670 --> 00:22:11,260 and you have to press backspace a bunch of times, and it seemed really inefficient 377 00:22:11,260 --> 00:22:13,260 whereas normally you would probably move your mouse over there, 378 00:22:13,260 --> 00:22:15,880 double click and start typing, and of course, 379 00:22:15,880 --> 00:22:19,190 the thing is, when you first start with Vim and you only know these basic commands, 380 00:22:19,190 --> 00:22:22,810 yes, it will be a little bit slower, but soon you learn some more powerful commands, 381 00:22:22,810 --> 00:22:26,380 which will allow you to move across documents much more quickly, 382 00:22:26,380 --> 00:22:29,410 and you won't have to have this very cumbersome use of 383 00:22:29,410 --> 00:22:34,330 repeating Js and repeating Ks and Ls and so on and so forth. 384 00:22:34,330 --> 00:22:41,340 >> Any questions so far? Solid. 385 00:22:41,340 --> 00:22:44,000 Back to the PowerPoint. 386 00:22:44,000 --> 00:22:49,140 Now we're going to introduce a few more commands for advanced movement. 387 00:22:49,140 --> 00:22:52,680 You experienced having to type maybe L a lot or H a lot, 388 00:22:52,680 --> 00:22:57,200 and here are some more commands you can use to move around more quickly. 389 00:22:57,200 --> 00:23:02,990 Suppose that your cursor is at the O over in the some in "This is some text," 390 00:23:02,990 --> 00:23:06,840 and here are 5 more commands you can use to move across the line more easily. 391 00:23:06,840 --> 00:23:09,820 So ^ and $ move you to the beginning and end line, 392 00:23:09,820 --> 00:23:14,300 and at some point when you learn regular expressions or other various things 393 00:23:14,300 --> 00:23:17,390 you'll find that these sort of conventions are very common. 394 00:23:17,390 --> 00:23:19,360 The ^ generally refers to the beginning of the line. 395 00:23:19,360 --> 00:23:21,090 The $ usually refers to the end of the line. 396 00:23:21,090 --> 00:23:24,130 These are very easy to remember, and similarly, b, e, and w, 397 00:23:24,130 --> 00:23:28,340 they're easy to remember as well because b corresponds to back or beginning, 398 00:23:28,340 --> 00:23:30,630 e corresponds to the end, which is the end of the word, 399 00:23:30,630 --> 00:23:35,530 and w corresponds to the next word, so w for word. 400 00:23:35,530 --> 00:23:37,750 And if you keep typing e or keep typing w 401 00:23:37,750 --> 00:23:43,520 you notice you can jump between words, and it's much faster than just typing l. 402 00:23:43,520 --> 00:23:46,600 >> Now, the next thing is the Operator-Motion Model. 403 00:23:46,600 --> 00:23:50,310 This is actually one of the key reasons why Vim can be so powerful, 404 00:23:50,310 --> 00:23:54,890 and it's because you can essentially compose different elements 405 00:23:54,890 --> 00:23:58,250 into more powerful functions, so generally 406 00:23:58,250 --> 00:24:01,000 when you do anything in Vim you can think of it as 407 00:24:01,000 --> 00:24:03,280 combining an operator with a motion. 408 00:24:03,280 --> 00:24:07,310 So far we've just dealt with motions, like HJKL. 409 00:24:07,310 --> 00:24:10,770 They move you around, and then b and w, they also move you around different ways, 410 00:24:10,770 --> 00:24:14,600 but we haven't gone yet to being able to delete things or change things or whatnot, 411 00:24:14,600 --> 00:24:19,320 and when you combine operators with motions then you're able to have 412 00:24:19,320 --> 00:24:22,510 much more powerful effects, so for example, 413 00:24:22,510 --> 00:24:26,360 if I wanted to delete 2 words, I can use d for the deletion operator, 414 00:24:26,360 --> 00:24:29,420 and I can add a 2 to indicate that I want to delete 2 words, 415 00:24:29,420 --> 00:24:31,130 and then I include a motion. 416 00:24:31,130 --> 00:24:33,490 I could include w to say delete 2 words. 417 00:24:33,490 --> 00:24:37,740 I could use something like J to say delete downward 2 times 418 00:24:37,740 --> 00:24:39,880 and delete 2 lines below me. 419 00:24:39,880 --> 00:24:43,870 I could say d2b to delete backwards twice, 420 00:24:43,870 --> 00:24:46,450 and there are even more motions than the ones that I've shown here 421 00:24:46,450 --> 00:24:52,120 to really, really make your Vim editing much more powerful. 422 00:24:52,120 --> 00:24:55,450 >> These are some of the most popular or the most common operators 423 00:24:55,450 --> 00:24:57,030 and are pretty easy to remember as well. 424 00:24:57,030 --> 00:24:59,040 D for delete, c for change. 425 00:24:59,040 --> 00:25:01,560 C is basically the exact same thing as change except that 426 00:25:01,560 --> 00:25:05,990 when you use c you automatically get put into insert mode afterwards. 427 00:25:05,990 --> 00:25:08,050 Normally when you use deletion you use it from normal mode, 428 00:25:08,050 --> 00:25:10,950 and after you delete something you stay in normal mode. 429 00:25:10,950 --> 00:25:13,380 But if you want to change something, you can use c 430 00:25:13,380 --> 00:25:15,960 to delete something and then immediately go into insertion mode 431 00:25:15,960 --> 00:25:17,740 so you can start typing and fix something. 432 00:25:17,740 --> 00:25:21,160 Y is for yanking. 433 00:25:21,160 --> 00:25:25,130 It's basically Vim's notion of copying. 434 00:25:25,130 --> 00:25:29,220 For example, if I want to copy the next 2 words I can do y2w or something like that, 435 00:25:29,220 --> 00:25:32,120 and v is for highlighting, essentially. 436 00:25:32,120 --> 00:25:35,550 And also what's really cool is if you type an operator twice 437 00:25:35,550 --> 00:25:38,890 it will act on an entire line, so if I want to delete an entire line 438 00:25:38,890 --> 00:25:41,060 I can type dd to delete the entire line, 439 00:25:41,060 --> 00:25:47,370 or if I want to copy the entire line I can use yy or something like that. 440 00:25:47,370 --> 00:25:51,600 >> Let's try this some more. 441 00:25:51,600 --> 00:25:56,280 There's an exercise here for an advanced movement operator as well 442 00:25:56,280 --> 00:25:59,230 and even some other things I'm not going to cover in the PowerPoint today, 443 00:25:59,230 --> 00:26:02,250 but you can feel free to look over them later and play around with them 444 00:26:02,250 --> 00:26:05,990 and get familiar with them. 445 00:26:05,990 --> 00:26:07,630 Operators and motions. 446 00:26:07,630 --> 00:26:14,220 For example, suppose there are these 2 words you just want to delete in the sentence. 447 00:26:14,220 --> 00:26:17,110 What you'll want to do is you'll want to make sure that your 448 00:26:17,110 --> 00:26:21,000 cursor is at the beginning of the first word you want to delete, 449 00:26:21,000 --> 00:26:26,230 and you can type in d2w, and it will delete those 2 words for you. 450 00:26:26,230 --> 00:26:33,140 And using this exact command, d2w, may seem a bit cumbersome 451 00:26:33,140 --> 00:26:35,940 and pointless, but it's supposed to express the idea 452 00:26:35,940 --> 00:26:39,380 that you can compose these different things, and also just in case 453 00:26:39,380 --> 00:26:42,730 you do happen to be in the situation, then you can end up using dw or d2w, 454 00:26:42,730 --> 00:26:45,600 which I do use occasionally. 455 00:26:45,600 --> 00:26:50,030 Dd and cc I use much more often because you can act on entire lines. 456 00:26:50,030 --> 00:26:52,000 >> For example, let's say I have this random list here, 457 00:26:52,000 --> 00:26:59,030 and maybe I want to delete the line that says Huskies because it doesn't fit, just because, 458 00:26:59,030 --> 00:27:01,200 and I don't want it to go into insertion mode afterwards. 459 00:27:01,200 --> 00:27:03,530 I want to delete it, in which case I could type dd, 460 00:27:03,530 --> 00:27:07,090 and that will delete the entire line. 461 00:27:07,090 --> 00:27:11,680 But let's say now there's this other line that I want to delete, 462 00:27:11,680 --> 00:27:16,850 but I also want to fix it and rewrite it, in which case I'll try something like cc, 463 00:27:16,850 --> 00:27:19,550 and that deletes the entire line but puts me in insertion mode, as you can see, 464 00:27:19,550 --> 00:27:24,970 and I can fix it from there. 465 00:27:24,970 --> 00:27:28,510 And there are many, many different ways you can combine operators and motions, 466 00:27:28,510 --> 00:27:31,360 and as you use Vim more and more you learn more about different ways 467 00:27:31,360 --> 00:27:36,300 you can use these commands, and you'll become better and better. 468 00:27:36,300 --> 00:27:40,180 Awesome, now here are some other assorted commands 469 00:27:40,180 --> 00:27:44,700 that also are very, very commonly used and also will help you become better at Vim. 470 00:27:44,700 --> 00:27:48,780 >> For example, we talked about lowercase v for visual highlighting. 471 00:27:48,780 --> 00:27:51,530 We can also use uppercase V for visual line highlighting. 472 00:27:51,530 --> 00:27:55,880 That way you can highlight entire lines at a time much more easily. 473 00:27:55,880 --> 00:27:59,180 You can use o when you're in normal mode to basically insert a new line 474 00:27:59,180 --> 00:28:03,620 below the line you're currently on and put it into insertion mode to start typing. 475 00:28:03,620 --> 00:28:07,270 You can use lowercase x to delete the character that's currently under your cursor. 476 00:28:07,270 --> 00:28:10,230 You can also use u to undo, which is kind of nice, 477 00:28:10,230 --> 00:28:14,180 and you can use capital A to append to the end of the current line, 478 00:28:14,180 --> 00:28:18,310 so perhaps your cursor is somewhere on the line, and you want to add to the end of the line. 479 00:28:18,310 --> 00:28:21,150 Then you can type capital A to immediately jump to the end of the line 480 00:28:21,150 --> 00:28:25,140 and go into insertion mode so you can add to the end of the line. 481 00:28:25,140 --> 00:28:30,840 >> I think I have a few more exercises for that which we can try out. 482 00:28:30,840 --> 00:28:33,240 Actually, no, I don't. Not for those. 483 00:28:33,240 --> 00:28:35,810 But we have some more exercises for the next section which we'll see. 484 00:28:35,810 --> 00:28:38,420 But these you can try out yourself as well. 485 00:28:38,420 --> 00:28:45,610 For example, if you type in regular v, you'll get regular highlighting that you're used to like this. 486 00:28:45,610 --> 00:28:49,800 If you type in capital V, you can get full line highlighting like that, 487 00:28:49,800 --> 00:28:55,850 or if I type in o it basically will open a new line for me, which is kind of nice. 488 00:28:55,850 --> 00:28:58,850 X I can use to delete characters on the cursor. 489 00:28:58,850 --> 00:29:00,800 I can press x a bunch of times and delete stuff. 490 00:29:00,800 --> 00:29:06,720 I can press u to undo all that. 491 00:29:06,720 --> 00:29:09,600 A, I can type in A to immediately go to the end of the line, 492 00:29:09,600 --> 00:29:14,040 and I also included a few other nice keys 493 00:29:14,040 --> 00:29:19,240 that you can also look at after the presentation to learn some other commands. 494 00:29:19,240 --> 00:29:21,250 >> [Student] What's replacement about? 495 00:29:21,250 --> 00:29:23,050 [Brandon L.] Replacement is also really cool 496 00:29:23,050 --> 00:29:26,240 and basically if you go somewhere— 497 00:29:26,240 --> 00:29:30,060 have you ever—people don't really use it that much anymore these days, 498 00:29:30,060 --> 00:29:34,340 but have you guys ever seen in Microsoft Word if you type insert 499 00:29:34,340 --> 00:29:37,840 you suddenly go into this weird mode where every single character you type 500 00:29:37,840 --> 00:29:39,570 basically replaces the next character? 501 00:29:39,570 --> 00:29:41,020 I don't know if you guys have ever seen that. 502 00:29:41,020 --> 00:29:43,250 This is similar to that. 503 00:29:43,250 --> 00:29:47,150 For example, if I want to overwrite what I currently have for some reason 504 00:29:47,150 --> 00:29:51,300 I can type in capital R, and I'll now be in replace mode, as you can see in the bottom left. 505 00:29:51,300 --> 00:29:58,380 If I type stuff it overwrites things, and I can also backspace, which is kind of cool, 506 00:29:58,380 --> 00:30:00,140 and I can undo that. 507 00:30:00,140 --> 00:30:06,980 I don't use that very often, but every now and then it becomes handy. 508 00:30:06,980 --> 00:30:14,150 >> Let's talk about text objects, which is one of my favorite things about Vim, honestly. 509 00:30:14,150 --> 00:30:19,660 Text objects are basically this notion of defining some sort of chunk of text 510 00:30:19,660 --> 00:30:26,550 inside your text file, and I'll go over these keys first, 511 00:30:26,550 --> 00:30:29,860 and then I'll give you some demonstrations for you to more fully understand what that means. 512 00:30:29,860 --> 00:30:34,650 W refers to a word, like a text object as a word, 513 00:30:34,650 --> 00:30:36,480 so some word as you normally know it, 514 00:30:36,480 --> 00:30:39,300 and if there's punctuation that's next to the word 515 00:30:39,300 --> 00:30:43,490 then it won't include the punctuation generally. 516 00:30:43,490 --> 00:30:46,440 But for example, if you want to include the punctuation for some reason 517 00:30:46,440 --> 00:30:49,080 or you're doing something that's not English and you want to include 518 00:30:49,080 --> 00:30:51,180 all the characters that are delineated by whitespace 519 00:30:51,180 --> 00:30:55,390 you can also use capital W to get everything that's surrounded by whitespace. 520 00:30:55,390 --> 00:30:58,510 You can also use parentheses to refer to a text object 521 00:30:58,510 --> 00:31:00,170 that's inside a set of parentheses. 522 00:31:00,170 --> 00:31:03,950 You can do the same thing with square brackets, angle brackets, 523 00:31:03,950 --> 00:31:06,450 braces, and quotes as well, and finally, 524 00:31:06,450 --> 00:31:10,970 you can also use t to refer to some tag. 525 00:31:10,970 --> 00:31:15,390 >> Let me show you how that actually plays out. 526 00:31:15,390 --> 00:31:21,380 For example, this is my very first example in my showcase. 527 00:31:21,380 --> 00:31:26,950 There's some word in this sentence which I want to change, 528 00:31:26,950 --> 00:31:30,560 and we talked about W being used as a motion, 529 00:31:30,560 --> 00:31:33,170 but you can also use it as a text object like so. 530 00:31:33,170 --> 00:31:40,940 For example, one way I could change Barack's name or basically modify it 531 00:31:40,940 --> 00:31:46,730 is I could go to the beginning of this word and type in c for change and w for word, 532 00:31:46,730 --> 00:31:51,650 and that will delete the entire word and put it into insertion mode. 533 00:31:51,650 --> 00:31:54,240 But it's very annoying for me to have to go to the beginning of the word all the time. 534 00:31:54,240 --> 00:31:56,530 That's really annoying. I don't want to have to do that. 535 00:31:56,530 --> 00:32:00,390 I can actually be anywhere inside his name 536 00:32:00,390 --> 00:32:03,930 and type in ciw, and the i stands for inner, 537 00:32:03,930 --> 00:32:06,590 so if I type in ciw it stands for change in a word, 538 00:32:06,590 --> 00:32:10,150 or in other words, change the word I'm currently inside, 539 00:32:10,150 --> 00:32:13,280 and it will give you the exact same effect. 540 00:32:13,280 --> 00:32:17,130 You can fix that. 541 00:32:17,130 --> 00:32:21,390 >> Similarly, perhaps I have some case like this 542 00:32:21,390 --> 00:32:27,920 where for some reason there are some delimiters or something like that, 543 00:32:27,920 --> 00:32:31,700 or there's some punctuation, and I wanted to delete the whole thing. 544 00:32:31,700 --> 00:32:36,530 If I type in ciw it will delete what's inside these delimiters, 545 00:32:36,530 --> 00:32:39,830 but I want to get the whole thing that's surrounded by whitespace, 546 00:32:39,830 --> 00:32:42,750 so I can use ciW to delete the whole thing. 547 00:32:42,750 --> 00:32:51,000 I'll type in five, do the same thing, and type in six. 548 00:32:51,000 --> 00:32:53,670 Similarly, if I want to change something that's inside the parentheses 549 00:32:53,670 --> 00:32:58,340 I can type in ci) to delete everything that's inside the parentheses 550 00:32:58,340 --> 00:33:05,460 and then fill in whatever I want to do, like int argc, whatever you want to do, 551 00:33:05,460 --> 00:33:07,330 and similarly, the same thing with quotes. 552 00:33:07,330 --> 00:33:13,130 I can fix text that's inside of quotes, 553 00:33:13,130 --> 00:33:21,540 and I can also change things that are inside of tags. 554 00:33:21,540 --> 00:33:24,400 Does that make sense? 555 00:33:24,400 --> 00:33:27,020 >> Another thing you can do is you don't have to use c. 556 00:33:27,020 --> 00:33:30,690 You can also use d if you want to delete what's inside these tags. 557 00:33:30,690 --> 00:33:35,370 If I want to delete the text inside a tag I can use dit to delete that tag. 558 00:33:35,370 --> 00:33:40,360 I can also use vit to highlight what's inside the tag, for example. 559 00:33:40,360 --> 00:33:43,920 The other thing is instead of using i in the middle you can also use a, 560 00:33:43,920 --> 00:33:49,490 which stands for all, so remember if you use i it will refer to the text object 561 00:33:49,490 --> 00:33:52,260 or use a text object that's contained inside the delimiters, 562 00:33:52,260 --> 00:33:56,000 but if you use a it will include that text as well as the delimiter itself. 563 00:33:56,000 --> 00:33:59,180 For example, if I want to highlight this text and the surrounding tag 564 00:33:59,180 --> 00:34:05,390 I can use vat, v for visual highlight, a for all, and then t for the tag. 565 00:34:05,390 --> 00:34:08,350 You can do something like that as well. 566 00:34:08,350 --> 00:34:15,810 Maybe I want to delete this google.com with the quotes around it as well, 567 00:34:15,810 --> 00:34:22,860 and I can use something like da" to delete all of that, for example. 568 00:34:22,860 --> 00:34:26,520 >> Any questions so far? 569 00:34:26,520 --> 00:34:32,270 I realize that I'm dumping on you a lot of Vim commands all at once, 570 00:34:32,270 --> 00:34:36,210 and this is basically because I want to expose you to all the different commands 571 00:34:36,210 --> 00:34:39,480 and give you an introduction to what these commands do in Vim. 572 00:34:39,480 --> 00:34:41,460 But what's going to have to happen is when you go back 573 00:34:41,460 --> 00:34:44,250 you're going to have to slowly pick up these commands one by one. 574 00:34:44,250 --> 00:34:47,880 You don't want to overwhelm yourself, because that's too overwhelming. 575 00:34:47,880 --> 00:34:50,460 If you want to pick up Vim what you have to really do is 576 00:34:50,460 --> 00:34:53,590 start with the first basic set, and I've broken it up into lessons for you 577 00:34:53,590 --> 00:34:57,830 that make it easier for you to follow, and try and master each set of commands 578 00:34:57,830 --> 00:35:00,400 one by one, one at a time. 579 00:35:00,400 --> 00:35:03,500 It's too much to do everything at once, so what you should do when you go back is 580 00:35:03,500 --> 00:35:10,950 go to the first lesson, which is HJKL, insertion mode, escape into normal mode, 581 00:35:10,950 --> 00:35:13,790 and saving and quitting, and that's all you really need to survive in Vim, 582 00:35:13,790 --> 00:35:15,220 and get really, really comfortable with that. 583 00:35:15,220 --> 00:35:18,270 And after a few days once you're very comfortable with that 584 00:35:18,270 --> 00:35:21,170 you should move on to the more advanced motion operators, 585 00:35:21,170 --> 00:35:25,890 the more advanced motions, and on to the operators and so on and so forth. 586 00:35:25,890 --> 00:35:32,160 >> I know I'm going through a lot, but I want to get you exposed to a lot of different things. 587 00:35:32,160 --> 00:35:36,080 But in case you're up for the challenge, I can show you 588 00:35:36,080 --> 00:35:43,690 how you can put this all together using the most of what we just learned today, 589 00:35:43,690 --> 00:35:46,280 and you can also follow along if you want, 590 00:35:46,280 --> 00:35:48,590 but I created some random exercise. 591 00:35:48,590 --> 00:35:51,140 Suppose we have some sort of roster like this 592 00:35:51,140 --> 00:35:53,200 where you have these blocks with people's names, 593 00:35:53,200 --> 00:35:55,440 houses, years, and concentrations and whatnot, 594 00:35:55,440 --> 00:35:59,240 and you want to add a new entry for yourself. 595 00:35:59,240 --> 00:36:01,530 What's the best way for you to do this? 596 00:36:01,530 --> 00:36:03,240 Let's do it like this. 597 00:36:03,240 --> 00:36:06,620 Why don't we first highlight this block. 598 00:36:06,620 --> 00:36:08,710 You're going to select it, and we can press y to yank it, 599 00:36:08,710 --> 00:36:11,180 so now we can basically copy and paste this template first 600 00:36:11,180 --> 00:36:16,500 without having to rewrite the whole thing from scratch. 601 00:36:16,500 --> 00:36:20,390 Now I'll press j down a bunch of times to go down, 602 00:36:20,390 --> 00:36:22,560 and I'll press p to paste it. 603 00:36:22,560 --> 00:36:26,990 Maybe I'll add some extra lines for a space, 604 00:36:26,990 --> 00:36:31,130 and let's say now I want to change your name to someone. 605 00:36:31,130 --> 00:36:36,220 Then I can go here and type in c2w like before to change that name. 606 00:36:36,220 --> 00:36:38,860 Bob Jones, who knows. 607 00:36:38,860 --> 00:36:41,470 Maybe if I want to change the house I can go into the middle here 608 00:36:41,470 --> 00:36:44,230 and type in ciw and put Cabot House. 609 00:36:44,230 --> 00:36:47,850 >> Here may be an example of where you may want to consider using 610 00:36:47,850 --> 00:36:52,970 the replacement mode to change your graduation year if you want, 611 00:36:52,970 --> 00:36:54,890 or we can also use ciw as well. 612 00:36:54,890 --> 00:36:57,550 It doesn't matter. 613 00:36:57,550 --> 00:37:07,180 Maybe you want to change your major. 614 00:37:07,180 --> 00:37:11,310 And that's what you do. 615 00:37:11,310 --> 00:37:17,170 And maybe I will give you a demonstration of what I might do 616 00:37:17,170 --> 00:37:23,820 if I were—if you continue to learn more advanced commands in Vim 617 00:37:23,820 --> 00:37:25,690 because we're barely scratching the surface here, 618 00:37:25,690 --> 00:37:28,960 and I could do this sort of editing even faster than I've shown you here 619 00:37:28,960 --> 00:37:32,860 because I know more commands that I haven't gotten a chance to show you for today. 620 00:37:32,860 --> 00:37:37,500 For example, if I want to highlight this I can do it like that, 621 00:37:37,500 --> 00:37:39,960 move down. 622 00:37:39,960 --> 00:37:43,860 I've added my own mapping 623 00:37:43,860 --> 00:37:48,100 to basically automatically highlight stuff for me 624 00:37:48,100 --> 00:38:02,610 and I can—I don't know. 625 00:38:02,610 --> 00:38:05,260 I mean, this is just trying to show you that you can really, really 626 00:38:05,260 --> 00:38:08,180 start editing things more quickly, especially when you have all these different fields, 627 00:38:08,180 --> 00:38:10,620 and normally what you might be doing is sort of clicking 628 00:38:10,620 --> 00:38:13,530 and highlighting and then typing. 629 00:38:13,530 --> 00:38:18,530 You can do everything all at once, copying and pasting, 630 00:38:18,530 --> 00:38:21,640 moving up and down between the file, 631 00:38:21,640 --> 00:38:24,060 just delimiting stuff, formatting stuff. 632 00:38:24,060 --> 00:38:29,670 All this can be done much, much more quickly in Vim. 633 00:38:29,670 --> 00:38:32,580 >> Just one last little thing which I think may be really nice, 634 00:38:32,580 --> 00:38:34,820 which is find and replace. 635 00:38:34,820 --> 00:38:36,460 Those of you who have used Gmail shortcuts 636 00:38:36,460 --> 00:38:41,050 you'll also know that if you type in / it sends you to the search box, 637 00:38:41,050 --> 00:38:44,210 and in a lot of different applications in general you'll find that slash 638 00:38:44,210 --> 00:38:47,300 or maybe control slash or something to do with slash 639 00:38:47,300 --> 00:38:49,870 will put you into some sort of search mode, 640 00:38:49,870 --> 00:38:53,240 and that also basically comes from Vim essentially 641 00:38:53,240 --> 00:38:58,670 because the slash operator or the slash command allows you to input 642 00:38:58,670 --> 00:39:01,770 some sort of string that you can use to search within the document. 643 00:39:01,770 --> 00:39:05,350 You can n and capital N to go to the next and the previous occurrence, 644 00:39:05,350 --> 00:39:10,700 and you can also use something similar to search and replace within the document. 645 00:39:10,700 --> 00:39:15,060 I'll give you a demonstration on what can be done. 646 00:39:15,060 --> 00:39:18,200 >> Maybe if I want to find all instances of the string yank within this document 647 00:39:18,200 --> 00:39:23,310 I can type in /yank, as you can see in the bottom left corner, 648 00:39:23,310 --> 00:39:26,350 and when I type enter it highlights all instances. 649 00:39:26,350 --> 00:39:34,160 I can use basically n to jump back and forth between all those different instances, 650 00:39:34,160 --> 00:39:40,420 and let's suppose I want to search and replace yank with some random word 651 00:39:40,420 --> 00:39:42,180 that's also in the document. 652 00:39:42,180 --> 00:39:47,070 I can press : to start typing in some sort of command within Vim, 653 00:39:47,070 --> 00:39:49,610 some command that's more sophisticated than moving around 654 00:39:49,610 --> 00:39:51,370 or deleting or something like that. 655 00:39:51,370 --> 00:39:56,860 I will type in the % sign to refer that I want to use this substitution command 656 00:39:56,860 --> 00:39:58,230 on the entire document. 657 00:39:58,230 --> 00:40:02,380 Type in s for substitution. This comes from PEARL essentially. 658 00:40:02,380 --> 00:40:07,040 And this next command is a pretty standard command that you'll find in PEARL 659 00:40:07,040 --> 00:40:09,790 instead and in other programming languages that you'll 660 00:40:09,790 --> 00:40:13,170 probably start to learn as you do more computer science, 661 00:40:13,170 --> 00:40:20,130 and I can basically use / to type a /, type in the word I want to find and replace 662 00:40:20,130 --> 00:40:24,280 and then type another / and type in whatever word I want to replace that word with. 663 00:40:24,280 --> 00:40:27,450 Who knows, maybe nyancat. 664 00:40:27,450 --> 00:40:30,390 I do that, and then you'll see that everything got replaced. 665 00:40:30,390 --> 00:40:34,000 >> But you'll notice that there's this word here. 666 00:40:34,000 --> 00:40:37,620 For some reason some of our instances of yank are replaced but not all, 667 00:40:37,620 --> 00:40:40,250 and that's because the way this sort of language has been written 668 00:40:40,250 --> 00:40:43,550 not only in Vim but in other places is that when you run the substitution command 669 00:40:43,550 --> 00:40:46,470 it will only substitute the first instance of that word in each line, 670 00:40:46,470 --> 00:40:52,810 so if I want to actually substitute all instances of the word on every single line 671 00:40:52,810 --> 00:40:56,890 then I have to do the same thing but add a g flag at the very end, 672 00:40:56,890 --> 00:41:00,810 which stands for global, and then that will actually 673 00:41:00,810 --> 00:41:06,200 substitute all instances. 674 00:41:06,200 --> 00:41:09,710 And once you learn the regular expressions, there's actually a CS50 seminar 675 00:41:09,710 --> 00:41:12,120 on regular expressions, so if you're curious about regular expressions 676 00:41:12,120 --> 00:41:16,970 you can also take that seminar and learn more. 677 00:41:16,970 --> 00:41:19,920 >> But for example, let's say I have a list of names like this, 678 00:41:19,920 --> 00:41:23,920 and I want to rearrange it, so instead of having last name, comma, first name 679 00:41:23,920 --> 00:41:27,490 I want to rearrange it so it says first name, space, last name. 680 00:41:27,490 --> 00:41:30,750 You can actually use this command right here 681 00:41:30,750 --> 00:41:33,600 to essentially do that for you, so as you can see here 682 00:41:33,600 --> 00:41:37,170 we start with an s, which means we want to do a find and replace, 683 00:41:37,170 --> 00:41:42,970 and then I added a flag called \v, which is something called this magic flag 684 00:41:42,970 --> 00:41:45,810 in Vim which allows you to do regular expressions more easily. 685 00:41:45,810 --> 00:41:49,370 Then from there we basically match for any single line 686 00:41:49,370 --> 00:41:55,960 that is of the form a set of words, comma, space, and a set of words or a set of letters. 687 00:41:55,960 --> 00:41:59,240 \w+ basically means one or more characters, 688 00:41:59,240 --> 00:42:04,030 and the parentheses are used to capture these groups 689 00:42:04,030 --> 00:42:06,120 so I can refer to them later. 690 00:42:06,120 --> 00:42:09,910 As you can see, once these capture 2 blocks of words 691 00:42:09,910 --> 00:42:11,690 that are separated by a comma, space 692 00:42:11,690 --> 00:42:15,510 then what we want to replace it with is I use \2 and \1 693 00:42:15,510 --> 00:42:18,130 to refer back to the words that were captured 694 00:42:18,130 --> 00:42:20,950 in the actual find command, 695 00:42:20,950 --> 00:42:24,970 and so \2 refers to whatever is contained in the second set of parentheses, 696 00:42:24,970 --> 00:42:28,140 \1 refers to whatever is contained in the first set of parentheses, 697 00:42:28,140 --> 00:42:31,680 and as you can see, I'm putting 2 before 1 separated by a space 698 00:42:31,680 --> 00:42:36,650 to try and move the first name before the last name. 699 00:42:36,650 --> 00:42:38,380 Does that make sense? 700 00:42:38,380 --> 00:42:43,450 >> And what I can do is I can hide these first so I only run this command on these lines, 701 00:42:43,450 --> 00:42:53,720 and then I'll type in this command. 702 00:42:53,720 --> 00:42:57,050 You can see it does exactly as divided. 703 00:42:57,050 --> 00:42:59,400 And again, this is barely scratching the surface, 704 00:42:59,400 --> 00:43:03,020 and if you go to the regular expression seminar then I'm sure you'll learn 705 00:43:03,020 --> 00:43:05,070 a lot more different things you can do with regular expressions, 706 00:43:05,070 --> 00:43:08,910 and this basically starts to hint at what you can really do with Vim 707 00:43:08,910 --> 00:43:10,850 if you really start to become a master of it. 708 00:43:10,850 --> 00:43:14,520 Perhaps you want to refactor something, and you want to find all instances 709 00:43:14,520 --> 00:43:16,980 of some function that follows some sort of naming pattern. 710 00:43:16,980 --> 00:43:18,570 You want to change the way that's named. 711 00:43:18,570 --> 00:43:21,140 You can write some sort of command like this to do that for you, 712 00:43:21,140 --> 00:43:23,850 and that's the sort of flexibility you can get within Vim 713 00:43:23,850 --> 00:43:30,210 that you're just not going to get in other text editors. 714 00:43:30,210 --> 00:43:34,830 >> That basically covers all the things I wanted to teach you today in today's seminar, 715 00:43:34,830 --> 00:43:38,580 but again, I really want to stress that if you want to pick up Vim 716 00:43:38,580 --> 00:43:42,120 it is true that there's a slightly higher learning curve in the beginning. 717 00:43:42,120 --> 00:43:44,230 In the very beginning you will be slightly less productive 718 00:43:44,230 --> 00:43:47,010 than you probably would be otherwise, but if you stick with it 719 00:43:47,010 --> 00:43:49,890 within a few days or a few weeks, depending upon how fast you learn, 720 00:43:49,890 --> 00:43:56,300 you'll become much, much faster, and I tried to give you a demonstration 721 00:43:56,300 --> 00:43:59,070 on what Vim is capable of but I didn't really get a chance to show you 722 00:43:59,070 --> 00:44:03,230 what happens when I normally—for example, in class I type into my notes with Vim. 723 00:44:03,230 --> 00:44:07,920 That's because I can work so much faster in Vim than I can with any other program. 724 00:44:07,920 --> 00:44:14,480 It is literally a 2 or 3 time productivity increase in Vim compared to other programs, 725 00:44:14,480 --> 00:44:18,800 not only with the speed of text editing but also in the way I can move around the document 726 00:44:18,800 --> 00:44:21,510 and the way I can access other files on my computer 727 00:44:21,510 --> 00:44:23,110 and jump in between them and so on and so forth. 728 00:44:23,110 --> 00:44:27,600 It's just a really, really huge productivity boost. 729 00:44:27,600 --> 00:44:30,550 >> But when you're learning, you want to make sure you learn one thing at a time, 730 00:44:30,550 --> 00:44:34,970 and try not to overwhelm yourself with too many different commands 731 00:44:34,970 --> 00:44:39,090 because there are a lot of commands and a lot of different shortcuts you can learn in Vim. 732 00:44:39,090 --> 00:44:41,780 You want to pick up a little at a time and over the years 733 00:44:41,780 --> 00:44:48,990 gradually amass a larger and larger understanding of Vim. 734 00:44:48,990 --> 00:44:51,790 If you want to continue to learn more, these are some of the more advanced topics 735 00:44:51,790 --> 00:44:55,900 you can continue to learn about, and even this barely scratches the surface. 736 00:44:55,900 --> 00:44:59,050 Here are some popular plugins you can feel free to check out and Google them 737 00:44:59,050 --> 00:45:03,750 and play around with them if you want to and just see what sort of plugins are out there. 738 00:45:03,750 --> 00:45:06,260 And then at the end of this PowerPoint, I include a bunch of links 739 00:45:06,260 --> 00:45:08,740 to various resources you can use to learn from. 740 00:45:08,740 --> 00:45:12,700 Here are a bunch of different links you can use to get started with Vim. 741 00:45:12,700 --> 00:45:15,150 They have different tutorials and whatnot, 742 00:45:15,150 --> 00:45:17,770 and finally, these are some more advanced things you can use 743 00:45:17,770 --> 00:45:19,790 to also check out as well, 744 00:45:19,790 --> 00:45:22,340 some screen casts and some more advanced techniques in Vim 745 00:45:22,340 --> 00:45:26,450 and also some different pieces of source code you can look up on GitHub 746 00:45:26,450 --> 00:45:28,760 just to look at other people's configuration files 747 00:45:28,760 --> 00:45:32,710 or to look at plugins in Vim and so on and so forth. 748 00:45:32,710 --> 00:45:40,380 >> That's all I have for you. 749 00:45:40,380 --> 00:45:43,000 [CS50.TV]