 Some of the most common comments that I get from you guys the viewers is about Vim I've done so much content about Vim in the three years of the YouTube channel And I often say that I really think everybody owes it to themselves to learn the Vim text editor If you spend any kind of time in a text editor You really owe it to yourself and that means if you're a programmer or a creative writer Whatever it is you do in a plain text editor Vim can save you so much time But people often ask me like hey Can you provide real-world examples of how Vim actually saves time because I think a lot of people assume You know Vim is so hard to learn it's gonna take you weeks or months Maybe even years to really learn Vim is it worth it in the end? Does it really save you time and yes? It is worth it and yes It saves you hours and hours and hours of work Compared to using a traditional text editor Vim can actually do some pretty amazing things So today I just wanted to give you some real-world examples of how Vim is Much faster at getting work done than your traditional text editor Now let me switch to my desktop and I've opened up this bash script here that I wrote This is just a simple d-menu script There's really nothing to see as far as the script is just a quick and dirty script But it's something we can play with and the first thing I want to show you guys is Moving lines around moving lines of text within the document because that's something you do often Especially when you do any kind of scripting or programming and that's something that Vim handles really well The most obvious way to move a line is simply to delete it and then go to the area where you want to then put it So to delete a line DD right and then navigate to wherever you want to put the line and then type P Now most of the time I find I'm deleting a line and pasting it Right after the next line and that that seems to be what most people end up doing For the most part when you're moving a line oftentimes You just want to swap the current line with the next line All you need to know is that DDP all one command swaps the current line You're on with the next line you see these two lines here doom dot D slash config dot e-mail and doom dot D Slash a knit dot e-mail if I just want to swap those lines. Just remember DDP Just swaps the current line with the next line You don't even have to think about it and again That's typically what you're doing most of the time when you're moving a line You're just moving it to the next line You only really have to think about DD and then where to paste it if you're moving more than one line away One of the cool things I've discovered with Vim is that you can quickly search for a string of text and find the line That contains that string of text and then move that line wherever it is you want in the document and to do this What I'm going to do is let me type capital G on the keyboard I want to go to the last line of this document and I've got screen key on and That is not wanting to get out of my way. I'm going to turn screen key off You guys really don't need to see the screen key commands But anyway, I went to the last line here in the document by typing capital G And really I just wanted to search for a string of text ultimately You see the word ultimately in the this line right here I've got my cursor above it because I want to be able to search for ultimately So I can't be past it so I'm going to do colon to get into command mode and then I'm going to type slash Ultimately slash meaning search for the word Ultimately then I'm going to do another slash and then I'm going to do M for move Where do I want to move this line that contains ultimately? I want to move it to the end of the document Let's just do the dollar symbol that means move it to the very end and there you go It searched for the line that contained the word ultimately and it moved it to the last line Now let me you to undo that and put it back in its correct place And you could do fancier things with that same kind of command instead of you know Searching for the string of text finding that particular line I could search for a string of text and then select one of the lines around it and move that line for example Once again, I'm going to do colon slash first search and we'll search for the word ultimately But this time I'm going to do slash and I'm going to do plus one Meaning find the line that contains ultimately and then plus one meaning I want the next line I don't want the line with ultimately. I want plus one lines So I want the line with a Lackardy that begins with a Lackardy here find that line M for move where to move it Dollar symbol of course would move it to the end of the document, but maybe I want to move it two lines up So I'm going to do minus two meaning go back to if I get plus to it would mean move it forward two lines Move it down two lines. Now when I run this command It may or may not move it to where you think because when I move this up two lines It's not going to be one Two, you know, you're thinking two lines up. Well, that's that blank line right there. Well, that's not exactly Imagine you're swapping these you know in place. So really it's going to end up above this ending case statement You see ESAC it's going to be the line above that actually is where it ends up I believe let me hit enter and actually check that. Yeah, that is exactly where it ends up So let me you to undo so that is you know moving lines after you search for a particular string of text I'm going to go back to the top of this document gg to go to the first line again Capital g to go the last line gg to go the first line by the way cursor positions I don't know a lot of you guys know this but to go back to the last cursor position that you were at G Semi-colon so if I do G semi-colon right now It goes back to that ESAC that ending ESAC statement because that's where my cursor was Previous to that gg back to the top of the document one of the really cool things about VM is visual mode Visual mode allows you to select blocks of text it selects multiple lines of text you can almost almost think of visual mode and VM is being kind of like a Multiple cursors kind of mode because you once you select all these lines You can then run the same command on all the lines. It's a really powerful thing first of all Let me indent multiple lines. I'm at this actually doesn't require visual mode You can actually just do this straight in command mode in them say I wanted to indent This line here line 10 all the way down to the end of this array line 43 So if I wanted to indent lines 10 through 43, I would type colon for command mode I could do 10 comma 43. That's simply a range lines 10 to 43 and then do the Greater than sign. That's the right pointing Chevron Hit enter and we just indented lines 10 through 43 You can see the lines before it were not indented the lines after it were not indented So that's a really easy way to indent a block of text and you can always tell you know The line numbers many of you guys are using line numbers in VM anyway So it's really easy to figure out lines. What to what for running any of these commands If you don't have line numbers turned on in VM I strongly suggest that you actually turn on the line numbers One of the things that I've discovered with VM and I use this all the time in my scripting or maybe I'm importing Comma-separated values from a spreadsheet. I'm getting a list of items or Importing maybe a table from my SQL table or something you get this kind of list of items And maybe it doesn't have punctuation and it needs punctuation or it has punctuation and you want to remove the punctuation You know you often find yourself with a list of items in lines And you need to add something to the beginning of every line or delete something from the beginning of every line or Add something to the end of every line That is something that's tedious and a lot of work and a standard text editor But in VM VM makes this really easy. So this declare statement here This is an array an array in bash scripting typically is Something that's got parentheses and then inside it It's got a bunch of values that are wrapped in double quotes and you can see that's exactly what is happening here But if instead of having these array items as Individual items, maybe I just want it to be all one item Which would mean I would need to get rid of all the double quotes itself the very first one and the very last one So it's really just one big item in the array. Now, how would you go about doing this? Well, Veeam makes this really simple in a regular text editor I would just have to go in here through the mouse or the keyboard get to every double quote and delete it Veeam I just get into visual block mode here to get rid of the opening double quotes I would type control V to get into visual block mode and then just go down Selecting all the double quotes. I went too far and then once I've got to the end of the double quotes Type D on the keyboard for delete and all of the opening double quotes are now gone Now I still have the ending double quotes to all of these lines So how do I get rid of the ending double quotes? Well for me, I would probably use a macro for this So let me go up one line and I'm going to type Q on the keyboard for record a macro So Q records a macro and then set it to any key you want I'm going to set it to two so you see at the bottom it says recording at two So everything I do from now on is a recorded macro and what do I want to record? I want to record this command here J to go down one line And then the dollar symbol to go to the end of the line X to delete the character at the end of the line, which is going to be double quotes That's the last character on each line and then I want to type Q to finish recording that macro And now I need to run that macro, but we need to know exactly how many times to run that macro I want to run it from this line here that starts with awesome That's line 11 and I want to run it all the way to this line here, which is line 42 So 11 to 42 how many times would that be? That's 31, right? So let's try that So let's do 31 at two and it ran that macro 31 times and it removed all of those double quotes Now that me talking it through you know takes a little longer, but honestly just doing it I could have removed that beginning double quote and the ending double quote from all of those items In seconds, you know like seriously like five seconds 10 seconds It would have taken a couple of minutes to do that in your standard text editor And this isn't a very long list imagine this was hundreds of items or thousands of items It would take you hours maybe days to do that in a regular text editor The traditional way going through and having to delete things Now let's imagine that this array started life looking like this You know it was just this one item here and we need to go back and actually add the double quotes at the beginning And at the end of each line because that's a little trickier. How do you go about doing that? Well, the first thing I would do is let's go back to this line here and What we need to do again is to get into visual block mode. So do control v And then select the next it looks like 30 lines. So you could do 30 j 30 down I need to go down one more. So unless j one more time select all of those lines Then you need to get into command mode from here So do a colon and when you do a colon from visual block mode selecting lines You're going to see single quote less than sign comma single quote greater than sign What does that mean the single quote less than sign is the first line selected the comma is just a comma The single quote greater than sign is the last line selected. It's a range, right? It would be like zero comma five is a range from zero to five This is a range from the first line I selected to the last line selected I want you to do this on all of those lines basically And what do I want you to do? Well, I want you to do this norm for normal mode space capital i What does capital i in normal mode do in vm? It gets you into insert mode at the beginning of the line that you're currently on So it's going to get me into insert mode on all of the lines selected at the very beginning of the line And then what do I want you to do? I want you to type single double quote And if I hit enter on that we just added a double quote to all of those lines Now we still need to go back and add the ending double quote Now can you guess how we would go about adding the ending double quote? Well, if you guessed very similarly then we're going to have to get back into the command mode Actually visual block mode first. Let's select all the lines Actually, I don't need the line with quit because it already has double quote at the end So let's start with zsh and go up to This line here because alacrity also is going to need an ending double quote So let's get it back into command mode So from here type colon and then again, it's a range from the first line We selected to the last line we have selected Again normal mode and this time in normal mode. What do you want to do? I want you to go to the end of every line So the dollar symbol and I want you to get into insert mode Just a lowercase i this time because I want you to get into insert mode where we're already at Which is at the end of the line and then I want you to just type a double quote And that actually did not work. We get Obviously an error there. It actually inserted the double quotes, but it inserted it not at the very end of the line We're one character off So what I'm going to do is I'm going to you to undo and then I'm going to type colon again to get back into The command mode. I'm going to do an up arrow to get back to that last command I did And what I want to do this time is instead of doing the dollar symbol. I I'm going to do capital a So in normal mode, I want to type capital a which means append to the end of the line Double quote. Let's see if this works and there we go So a little trial and error there I should have known that just I was not going to do the very last character But capital a gets us that double quote at the very end of the line Now one of the most common things I do within vim. I'm constantly typing cw for change word For example, if I go to the top of this document here, and maybe I want to search for derrick But maybe I want to change derrick So maybe I want to do cw for change word and I type dt instead of derrick, right? Let me undo that. Well, I could I could certainly do that So I'm always doing cd cw for change word You're often misspelling a word and the easiest thing to do is just cw and then retype the word the correct spelling But what if you want to change multiple words that you've searched for now? This is a really neat command So searching of course is just doing a slash and then I'm going to search for the word the Te because it's probably in this document a few times Did you know that you can actually Change the search terms for example, let me hit enter to actually go to that very first instance of te Now if I type cw, I could change that word But what if instead of cw I typed cgn cgn means change the next search match So change the next match of te so cgn and you see where I was at on te disappeared I could type something new Maybe I want to type te all caps and I'm going to hit escape And then I get to see g in again I go to the next instance of the and I could change that to te escape And if I wanted to do this multiple times instead of doing cgn and then typing the word and escape every time I could just type period on the keyboard. What is period period means repeat the last command I just did so if I did period and you see otherwise is now otherwise with te capitalized if I do period again The next instance of the is the all capitals te and if I hit period one more time, there's no more instances of te So let me escape and I'm going to you to undo a couple of times to get the correct spelling of te back into this document I'm going to escape one other thing that can save you all kinds of time if you're constantly typing The same things maybe lengthy names of something Maybe you're always adding the same copyright or you're always referring to the same Company or foundation or something, you know, you can set up abbreviations in them For example, if I wanted to let me set up an abbreviation with colon ab for abbreviation And I'm going to set this abbreviation fsf Space and what do I want the fsf abbreviation to stand for I want it to stand for free software Foundation with the correct capitalization going to hit enter on that now Let me just start typing something here for example If I wanted to write something about the free software foundation instead of having to write free software foundation three words Rather lengthy. I could now just do the fsf Was founded in 1985 Now did you guys notice when I typed fsf it immediately expanded to the free software foundation That's pretty neat now. Sometimes you may not want fsf to expand to the free software foundation For example, let me type o on the keyboard to get into insert mode on the next line I'm going to type the fsf Space and then expands to the free software foundation web address Is fsf and then I'm going to do control v meaning Don't expand this dot org And you see that time it it just keeps fsf as fsf It didn't expand it to the free software foundation. So abbreviations are really neat Again, if you find yourself typing the same Proper names for companies or places or in this case foundations. I strongly suggest to you Use abbreviations. They just make your life so much simpler and they save you a ton of work Now, let me delete those lines there. The last thing I want to mention is Running shell commands from within vim Most of you guys know you can run shell commands within vim All you need to do is do the colon to get into command mode type the exclamation point Meaning what I'm about to type is a shell command. For example, if I did exclamation point ls It's going to run a ls command in the directory I'm currently in where this file is located and you know, I get this ls here It really doesn't do anything. Of course, I could pipe that into this document You can do all kinds of really neat things by using shell commands within vim But where it really comes in handy I found is how many times have you opened a file in vim and started editing it And then you go to write it and it complains Hey, this file is read only because you don't have permissions to actually write that file Maybe it was a file that's owned by the root user and you opened it as your home user How do you get around that? Well, an easy way to get around that is simply using shell command mode within vim Now there's a lot of things you could do in command mode to fix this problem Obviously, you could chmod the file to get the correct permissions or change ownership of the file Whatever you want to do But what a command a lot of vim users just have memorized to get around that read only error Is this command right here? I'm going to do colon w for right, of course space and then exclamation sudo All one word and that's basically saying hey this next command is a shell command. What's the command? It's the sudo command space and then I want you to run this command line a utility t T redirects output somewhere. What where am I redirecting the output to I want you to redirect it to the file I'm currently working in so just do the percent sign And that is a easy work around if you ever go to do colon w And it says I can't write to that file because you don't have permissions Just try colon w space exclamation sudo space t space percent sign Now before I go I want to thank a few special people I want to thank the producers of the show devin fran gaib corbinion Mitchell a commie arch feature 530 chris chuck david the other david donnie dill and gregory loose paul pickvm Scott and willy they are the producers of this episode without these guys You wouldn't know about how vim can save you hours and hours of work. You wouldn't know about it You also wouldn't know about it if it wasn't for each and every one of these ladies and gentlemen These are all my supporters over on patreon because the distro tube channel is sponsored by you guys the community I sincerely thank each and every one of you that help support my work If you'd like to support me consider doing so look for a distro tube over on patreon. All right guys. Peace Eat your heart out vs code