Part 2 Practice Second Week
Deleting in Vim
Replacing in Vim
Multi-line cursor
Macros (killer feature)
Deleting in Vim
Why not use the dumb 'Delete' key on your keyboard as you usually would and get it done? I'll bet that by now even your Granny has figured by now how to select multiple lines with the mouse and then delete in chunk. Why stick to bad editing habits when there are simpler automations?
Let's look at some Normal mode commands. A quick recap on the Normal mode: this is the default mode when you launch Vim.
Now let's look at some delete commands.
dd to delete a current line
dw to delete current word
diw if your cursor is in the middle of a word and you want to delete it
d2j delete two lines below
d2k delete two lines above
dit delete in tag. If you're inside an html or xml tag.
ggdG delete entire file. Nuke it. Do you this if you are really mad with your code.
Let's break down ggdG. "gg" takes you to beginning of line. "d" is to delete and "G" is until the end of file.
di( delete all arguments inside in the brackets
di{ delete everything inside braces
d2w delete 2 words forward.
d2b delete 2 words backwards
dip delete in paragraph
dis delete in sentence
As you can see the combinations are endless but intuitive. You don't need to remember a lot since it's like natural language and just acronyms.
Deletions. Click above for demo |
Sometimes you might be more confident visualizing and selecting what you want to delete.
Select the content you want to delete by entering the Visual mode with "v." Then you can press "d" and you are done.
ggVG d to select the entire file and then "d" to delete.
Visual Mode. Click above for demo |
Replacing in Vim
Replacing is a mode in Vim. You can enter Replace mode with "R."
Now just type, and the buffer will be replaced with what you enter. To return to Normal mode, just press "Esc."
You can replace a single character without going into Replace mode.
Still in Normal mode, press "r" on the character you want to replace. Type the new character. You remain in Normal mode.
You can do search and replace too.
Use "Esc +:" to enter Command mode. Now, lets replace all foo's with bar.
:%s/foo/bar/g
Will replace all foo in your buffer with bar.
Let's say you want to replace foo's with bar. But not all of them.
:%s/foo/bar/gc
This will ask for confirmation. Keep hitting "y" for each occurrence you want to replace.
Replacing Vim. Demo |
Multi-line cursor
This is yet another interesting feature available with all advanced editors.
Let's enter a text Number before all numbers on each line.
Use "Ctrl + v" to enter Visual Block mode. Yes this is yet another mode!
Now use you navigation key to select the block of code you wish to select.
Now enter "Shift + I" to go to the first line. You'll switch to Insert mode automatically.
Now press "Esc" and watch the magic happen.
A number gets prefixed to each number on all the lines you selected before.
Let's delete a block from a table.
You can do that with with Visual Block mode (Ctrl + v). Select with navigation keys and hit "d."
Multiline cursor. Click above for demo. |
Macros
This is the killer feature of Vim which really gets me excited. I have used this feature to my advantage on numerous occasions. Not only in Vanilla Vim but also in Idea vim, VSCode etc. I am not a Vim purist. I just use the right tool for the job.
Macros is a way to record an action and then replay it as many times as you want. Any number of text editing actions can be recorded and then replayed.
Macros are stored in a register.
Here's a scenario. Let's say you want to convert everything to upper case. You can do that with a "~" in Normal mode.
Now lets record these interactions
qd to start recording a macro to register d
viw to select a word
~ to convert to upper case
w to move to next word
q to stop recording
@d to replay the macros
10@d to replay the macros 10 times.
@@ to replay the macros once.
Wasn't that cool?
Macro. Click above for demo |
Conclusion
You learned about some really awesome features in Vim. The Vim macros! You also learned some more about Vim editing, such as deletions, replace, and multi-line cursor. The things I talk about in this article are features I use frequently. I felt they deserved a mention. I try to keep the Vim experience vanilla, however. On some occasions I use plugins, like FZF (Fuzzy finder), Tagbar etc. I will write about Vim configs and plugins in future article.
You if you made it this far! Congratulations! If Vim were karate you'd still be an Orange belt because the road to Vim mastery is long. But it is a worthwhile goal. You won't regret your investment. Also, don't be worried if you forget something. The Vim help system is always there for you. In Command mode type "help" and the comprehensive Vim help is available to you.
Please share your comments on what you think of Vim editor. Whether you're a beginner or an expert, I look forward to hearing from you.
Comments
Post a Comment