Skip to main content

Vim Part 2

 

Navigation is the very essence of using Vim. If you are used to pushing arrow keys to navigate around, it's time to break the habit. Vim will not come in handy if you insist on using arrow keys. The more productive (less distracted) way is to always have your hands in normal keyboard position and use the letters to navigate, as I describe below. 


In Normal Mode these are the keys you will use to navigate. Notice I always mention Normal Mode, so it's always best to be in Normal Mode as often as you can. 


h

left

j

down

k

up

l

right

This might be difficult to adjust to at first. Within a few weeks of usage, I started to love it. I no longer have to think about going up, down, left, or right. It just happens. Muscle memory!

Tip: The best way to learn Vim navigation is to use it. Chrome's Vimium plugin is the best way to get used to this navigation. 

Relative Jumping

Vim allows you to concatenate multiple commands together. Let's say you want to jump to 10 lines down, you use 10j. To jump 10 lines up, use 10k


Jumping to a Line in Command Mode

You can also go into Command Mode and enter a line number. Your cursor will jump to that line number. For example, to go to line 1, enter :1


Jumping to Top and Bottom

gg navigates you to top of the document.

G navigates to the bottom of the document. 

The scroll bar bites the dust with these 2 commands!


Jumping between Paragraphs

Shift + } : jump down one paragraph.

Shift + { : jump up one paragraph.

This is easy and convenient for jumping between methods or paragraphs of text.


0 to jump to beginning of a line

$ to go the end of line

w to navigate to next word. For example, 2w will navigate two (2) words forward.

b to navigate backward. For example, 2b will navigate two (2) words back. 

These navigations are super useful when navigating through code. 


Create a New Line Above/Below

I never thought this was "a thing" with other editors, and I never thought of using it. My repertoire was go to the end of line hit 'Enter,' and start typing or hit 'Home' and 'Enter' to create a line above. 

Well in Vim, you just press o to create a new line below no matter where you are in the line. It puts you in Insert Mode so you can type.  Use capital O if you want to create a line above.


I bet you're exhausted! Take a break, have some coffee, and come back for some Vim goodness. If Vim were karate, you'd be a yellow belt once you mastered navigation. 


Click above load demo



Vim Cool Features

Managing real estate, aka screen space, is really important. If you are working from home, you may not have the luxury of additional monitors like you do in an office. Fear not! Vim may look like a terminal, but it has powerful features packed in it. 

Splits

Lets do a split!

In command mode enter vsp for a vertical split

In command mode enter sp to do a horizontal split. 


​​​​​​​Jumping between Splits 

Ctrl + w and then h,j,k,l, depending on where you want to go. By now you know what keys to use to go up, down, left, and right. If not, it's okay. We all fumble once in a while.


Tabs

Are you a tabs person and don't enjoy splitting screens? 

Go to Command Mode and type tabedit to create a new tab. Ta da. You have a new tab!


How do I cycle through tabs? 

Type gt.


How do I go back a tab? 

Type gT  (Notice the capitalization)


Folds

Folds is a modern IDE feature used to collapse a piece of code. 

Go to the method you want to fold. Type zf10j to fold 10 lines down, and you have a fold.

Type zo to open a fold. 

Folds works in visual mode too! If you are visual person, select a region and type zf.

Splits demo. Click above



Tabs demo. Click above




Folds demo. Click above


Basics of Copying

Editing is what developers do most of the time. We love to delete code more than refactor. We like to make minor nits here and there to make the code better. Sometime the lucky ones get to write a lot of green field code. Vim helps you with all of this.


Undo

Undo is really important. For example, you might be in Insert Mode and type a command that is used in Normal Mode. Suddenly you're cringing. What did I just do? Don't worry.

Go back to Normal Mode and press u. Keep pressing u to undo as far back as you want to go. 


Redo

Redo is Ctrl + r in Normal Mode. 


You will be surprised by the speed of undo and redo and the lengths to which Vim remembers your edits. 


Copy (Yanking)

In Normal Mode you can press yy to copy a line and then use p to paste it some place else. 


Now let's use some navigation tools we learned earlier. Let's copy two (2) lines. 2yy. Then press p. Wasn't that easy? 


If you prefer Visual Mode, or if you want to see what you are copying, then enter Visual Mode by pressing in Normal Mode. Select the text to copy and press y. Navigate to your favorite spot and hit p to paste.  


Fantastic Copying 

Copying within braces.

There is no such thing as fantastic copying in Vim, but I named it so because I feel it is awesome. You have method body which you need to copy between braces. void foo{ // some bar voodoo } 

Navigate to the any position inside the braces and type following in normal mode: yi{

This can be read in plain English as "yank in {"

Navigate to the destination where you need to paste and press p

Isn't that amazing? 


Copying within round brackets 

Some times you want to copy all the parameters inside a round bracket. Try yi(

Copying a sentence is yis. Copying a paragraph is yip. 

I was a happy man the day I learned this.

Copying undo redo with vim. Click above for demo

Conclusion:

If you made all the way down here, give a pat on your back. As I said before, if Vim were karate you'd now be an Orange belt with more skills in your repertoire. You are really determined to learn something new, so I would like to congratulate you and hear from you. I hope you learned something interesting about Vim. I hope to see you again when I publish Part 2 where we will dive into more interesting topics. Please leave your comments, suggestions, questions and hit that "Like" button if you liked this post. 

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