Learn VIM and don’t die in the attempt VI replace and delete
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
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
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
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
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
4. Print the value in UTF-8 and ASCII
Also a very nice feature at the moment to use VIM
is a versatility when we start this journey to VIM
in deep, we don’t imagine the possibilities and capabilities for VIM
in action, so this is a feature that make my mind blow, for example we need to identify a UTF-8
value for the current char for that we press g8
In the same way but for ASCII value we can obtain that value without out from VIM
for that just press the key ga
and the magic update the status bar in VIM
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
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
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 dw
with d
we delete but using the w
multiplier we delete a content from current position to the next beginning of the word
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
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+Z
and didn’t work so please maintain the calm and press the u
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 VIM
we 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
The status bar was update with bot and execute the last command in the stack, in our case the dd
command.
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 w
write 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: