Learn VIM and don’t die in the attempt VI replace and delete

Jhonnata Gil Chaves
6 min readNov 21, 2022

--

All files are available in this repo

Time to open file, you can use another way to open the file, so guessing that you and your terminal is in the root of the download folder, you can use the VIM GUI for navigate to this files

vim .

or if you are out of this folder you can use

vim medium-articles-vim

Now you have a terminal similar to this

vim explorer

In the first section we identify in a high level our current path for my side is /home/rmrf/src/articles/medium-articles-vim , for the second part we see the content for this path

So move quick to the folder, for that option you can use:

  • The Arrows
  • The keys for move hjlk
  • Or your mouse

So independent for your way, the objective is go to the example/dont_die_attempt_vi/folder, also you identify that you are in the right folder seems the header’s section

vim in the right folder

Open the 6_file.txt plain text file

Go to …

Now we discover new ways to move fast when we use VIM

1. Go to the end of the file

By default when you open a new file in VIM the current position for the cursor is first line in the first character, so start moving to the end of the file, for that we can press the G key, yes in uppercase, just in case

VIM move to the end of the file

2. Go to the 1st line in the file

Now that we probe how to move to the end of the file can move to the first line in the file so we need to press double g gg , yep with a lowercase

VIM move to the beginning of the file

3. Go to the N line in the file

In previous post we talk about for this move, but we discover a new command to move also to the N line inside the file, so for that you can use the N, where N is the number of the line to go, and G in upper case, for example we need to move to the 8 line in the file so you press 8G and VIM move the cursor to this line

VIM move to the N line

4. Print the value in UTF-8 and ASCII

Also a very nice feature at the moment to use VIMis a versatility when we start this journey to VIM in deep, we don’t imagine the possibilities and capabilities for VIMin action, so this is a feature that make my mind blow, for example we need to identify a UTF-8value for the current char for that we press g8

VIM print the UTF-8 value

In the same way but for ASCII value we can obtain that value without out from VIM for that just press the key gaand the magic update the status bar in VIM

VIM print the ASCII value

These are a very nice features for move inside our files.

Replace content

BTW like an another editors we can replace the content for a current position, for example we need to replace the current content for another just press the r key and start to replace, but this just allow to replace a one char but if you want replace more characters you can press R in uppercase for enable the whole replace for the line, for example we replace in the current position the content for Boundary a little disclaimer for exit from Replace mode you need to press the ESC key

VIM start replace
Before the replace

Delete char or line

It’s not easy identify why by default we can’t delete the current content with the key for delete content that normally we use, but no worries for that we can use a x key for help us with this, for example we need delete just a char then just press a one x

VIM delete a char

But how to we can delete more than just a simple char, we can use a d command for that, also this support a multiplier for doing this, for example we need delete the content for the next word insert dwwith d we delete but using the w multiplier we delete a content from current position to the next beginning of the word

VIM delete with multiplier

I guess you have the next question, these commands seems pretty awesome but how to I delete a whole line, no worries about that just press the dd command with this you delete the whole line

VIM delete a whole line

Undo the previous action

In the previous content we discovered how to we can delete or replace content also in another post we talk about for insert commands, but how to we can undo or revert this changes, I try CTRL+Zand didn’t work so please maintain the calm and press the u command

VIM undo command

Now that you see, the content was restored and the status bar was update with information about this “restore”

Repeat the last action

It’s very common when you execute an action execute that action again, so in VIMwe have the opportunity to made this repeat more easy, for example we execute in the previous content how to undo the delete part, but I want to execute the delete command again so you can press the dd command again or use . dot to doing for you

VIM execute the last action

The status bar was update with bot and execute the last command in the stack, in our case the ddcommand.

For today we done, please enjoy this awesome journey to VIM with me.

Exit

For finish this little session for search and inserts, you can use a :wq for wwrite and q for quit, just in case that you modify the file save your changes in the current file.

Exercise

Delete the lines between 8 and 20 and use the undo command to restore the original content.

Download from this repo the files and execute this task:

<< Previous section

--

--

Jhonnata Gil Chaves

I’m a System Engineer and Software Developer. I love Linux and your philosophy it’s very nice share all knowledge that every day I learn around tech and make so