Basic Editing Commands in Vim
This guide walks through the basic editing commands that you can use in Vim.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide we're gonna start talking about how we can edit files in Vim. Because you've already gone through some of the previous lessons then you know some of the basic commands for example you know how to come down to a line like the code line we have here of some_method and because you know about the visual mode you could press the V key and then type in either l or W, W usually is not the one you wanna use for that but you can still use it in order to select items and then let's take a case study let's say that you want to edit this sum method name.

The way you could do that is you can type in v and then l and skip all the way head, and then you can use the D character to delete which will work, we'll have a whole section dedicated to all the Delete methods but just know you can use the D character to delete. Switch into insert mode and then give this another name, just like that.

large

However that's not really an efficient way of doing it and remember the whole point of using Vim is to be calm and efficient developers. So the keystroke you can use in this is one that I use literally every single day is CW, so if you type in CW that replaces that word or it gets rid of that one specific word and then you can start typing in "another_method_name" just like that.

Now C is going to allow you to do that not just for words but also for entire lines, so say that I came right here and instead of wanting to put "put hello world" say I wanna switch this entire thing out I can just type in shift C then that's gonna switch me in insert mode and it's gonna delete that one liner text and so now I can do p and put something else and that's a much more efficient way of being able to delete a line of text while switching them to insert and than instantly been able to type in any of those items.

large

So you know review those two items are CW if you want to replace whatever your current word is at, and one other interesting thing here is, say that you have a method name and Ruby and you don't wanna change the entire method name, just the second part or everything say from here on, I can type in CW from here and it doesn't delete anything to the left of that.

So CW is a very helpful tool, and it's part of the reason why I use it every single day is because it doesn't just replace that entire word (you can have it replaced a whole word) but it replaces wherever you start from, so that's also something helpful and then shift-c or capital C replaces everything from wherever cursor is all the way to the right, so if I do shift-c you can see that, that gets rid of that.

large

Now one thing that you may have noticed that I've done several times on this course, is using the undo process so say I wanna get rid of that change, all I have to do is I have to be in command mode and then press the u character and the u-key is going to go and it's going to run and undo and then additionally I can keep on pressing u and it's gonna keep on switching everything back just like that.

large

Now if you want to redo that's also pretty easy process and the keystroke for that is control r so control r is redo so we can take it all the way back and we can see on the bottom left-hand where it says "already at newest change", which means we can't redo any further, if I want to hit undo again then it changes the status on the bottom left-hand side and it says "one change before a number eight" and what that means is that Vim actually keeps track of all the changes so we have eight change so far and it also tells us how long ago that change was.

All these things are very helpful whenever you're building out a program, it's almost like you have someone watching over your shoulder giving you advice on when you made a change or anything like that so just practice undo a little bit more. I'm gonna hit shift v and come down and delete a few of these paragraphs, so I'm going to just hit the D character and that's gonna remove them and then if I go and I make any other changes just like this, switch back into command mode and all I have to do is hit the u-key a few times and I am back to where I wanted.

I can hit command-r to switch back and that way that you never have to be afraid of making mistakes or anything like, that you can very easily switch back in and undo just like any other text editor, however here I just I like the interface and I also love how it keeps track of all the changes and it gives you time stamps. So that is how you can start doing some more efficient editing

In review we talked about how you can use c and we can use CW we can also use c or capital C that's going to delete where ever the cursor is all the way to the right and then we also talked about how we can use undo and redo, and in the next guide we're gonna go through and see a few more efficiency items we can use when performing editing in Vim.