Skip to main content

Vim Part 3


Welcome to the second installment of a short two-part series on Vim! First of all, I want to thank you for your interest in this topic. If you haven't read the first part, I encourage you to start there. As I write this second part, I'm confident you are building your skills with the Vim editor. Take it slow, and like I said: learn one new thing in Vim every week. In the previous article, we learned the various modes of Vim, basic editing, and some cool features, like splits, tabs, and folds. I believe in developer productivity, and I think modal editing is really useful in that aspect. In Part II, we're going to learn some really cool stuff. Believe me: the minor tricks that you learn go a long way in the gains that you get with Vim. Let's jump right in.

Part 2  Practice Second Week

  1. Deleting in Vim

  2. Replacing in Vim

  3. Multi-line cursor

  4. 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

to move to next word

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

Popular posts from this blog

Command line tools for a happy life

Hello Readers! I'm back and better than ever with another article about developer productivity. I love the Terminal. After all that's where everything comes to an end.  Or begins. (Only nerds get that joke.)  Today I'm going to discuss four (yes, just 4 ) cool *nix tools , utilities that help you get things done faster once you integrate them into your workflow. tmux tree find ripgrep   tmux tmux is a multiplexor. Do you log in to remote servers using ssh clients, like ssh and/or putty? Do you have to spawn multiple of those just to get more done? Does that slow you down? Of course it does. Well, look no further.  Spawn one terminal and invoke the tmux . That's all you need! Spawn it with: $tmux  Note:  tmux may not be available by default. To install it, use your favorite package manager. This screen cast demos how to use tmux.    Tmux, click for demo tree tree provides a nice layout of the directory structure ...

Chat GPT and our(dev) future

 There's a lot of hype and anxiety over large language models like Chat GPT. Companies are scrambling to come up with their next AI offering as soon as possible to counter GPT. I've been thinking about how it will affect us as engineers and our company in the near future and in the longer term. Please read what follows with a grain of salt, as this is my opinion only. What is GPT capable of? GPT is an intelligent chat bot backed by a language model which can answer your questions contextually and make it appear creative.  It allows you to do your job faster.  Will GPT help us get to MVP faster? Maybe. It can write code but only in snippets. You're still responsible for gluing the code together.  Today it’s a fast stack overflow.    Will GPT replace you?  Certainly not in the near term. Software engineering is about communication. A stakeholder won’t be able to communicate with GPT and get an MVP.  As an engineer, you...

Configuring VIM

Let's configure VIM In this post, we will look at the basics for configuring Vim. Vim has some reasonable defaults, but it is malleable, so you can shape it to your needs. Master craftsmen all have a favorite tool which is custom-made and tailored to their needs. That is what Vim is for writers and programmers.  Let's get down to business. Locate your configuration file. If you're using Vim ~/.vimrc in your home directory is the file you will use to configure Vim.  If you're using Neovim ~/.config/nvim/init.vim  in your home directory is the file you will use to configure Neovim.   The standard workflow is to edit the vimrc/init file, exit Vim, and launch it again so the configuration take effects. (Optionally, you could source the file in command mode.) If vimrc is open in your terminal, just enter the following command: so % . "so" is the shorter form for "source" and "%" refers to current buffer.  This configuration is the same for ...