Skip to main content

Vim Part 1

Vim Part 1 

Welcome to the first installment of a short multi-part series on Vim. Engineers are obsessed with optimization. Optimization is a scientific endeavor. As engineers, sometimes we look into our own workflows and try to figure whether we could write better code. We ask ourselves questions, like: what if we could write faster? What if we could get rid of the drudge work in our text editing? Can our tools support us better? If you think of improving your workflow, like I do sometimes, then welcome aboard. What you are going to learn in this article is something timeless, I promise. I have provided GIFs for some Vim in action which shows keys pressed. Vim is a powerful text editor. It is quite popular and makes you more productive. Do you want to code at the speed of thought? Dive right in! I remember reading about Vim some 15 years ago in a UNIX book. A lot has changed in the tech world since then. New editors have been born and perished. However, Vim has survived over the years and continues to be a powerful editor with new features being added almost every year. Some useless trivia knowledge: Vi was created in 1976 by Bill Joy and was released as Vim (Vi improved) in 1992 by Bram Moolenaar. You don't need to know these guys to use Vim. Some not-so useless truth: Vim has a steep learning curve. But don't let that act as a deterrent from your Vim journey. You will bless yourself after you have learned it. Estimated time to learn is about 20 minutes to read this tutorial + 2 week of practice to develop muscle memory.
Let's look at a quick enter and exit in our Vi editor to debunk this myth. 


We will cover the following topics in this two-part tutorial:

Part 1 Practice First Week

  1. Why Vim?

  2. Editor wars

  3. How to learn and practice

  4. Vim modes

  5. Navigating in Vim

  6. Cool features: tabs, folds and screen splits

  7. Basics of copying and undo redo

 

Part 2 Practice Second Week

  1. Deleting in Vim

  2. Replacing in Vim

  3. Multi-line cursor

  4. Macros (killer feature)

 

Why Vim?

Vim is ubiquitous and Vim is fast. By "ubiquitous," I mean Vim is available everywhere. Ssh into a remote terminal and you have Vim; you have a raspberry Pi and you have Vim. You don't need a powerful machine for Vim. It's great for programmers, writers, and DevOps engineers. What you are learning is not only Vim but also some really useful key bindings. If you use vanilla Vim, it works great with Python, JavaScript, CSS, C++/C, HTML and many more.

I believe Java folks should be fine using Vim emulation in VSCode or IntelliJ, since the auto-completion plugins requires Java 11 and many of you might not be on Java 11 yet. Whenever I am doing some text edits which don't need auto completion, I fire up my Vim to edit Java quickly. You have to use it in order to believe the speed of this editor. 

With a little bit of configuring, you can get autocomplete etc. to work. But that is not the point of this article. I will help you with the basics of Vim before we dive into language specifics in the future articles.  

A note about being fast: "Faster" doesn't mean you will type faster or that the editor is more responsive. "Faster" means you'll free up time for more meaningful work than meaningless keystrokes. Your brain cycles should be focusing on the problem instead of constantly context switching between thinking about editing something between braces, creating new lines, or how to copy, paste. For example, switching to a mouse for pointing to the right object on screen constitutes a context switch. 

 

Editor wars

Whether you are a power sublime user, Emacs user, IntelliJ, or Eclipse user with muscle memory developed for these editors/IDE, you are a great developer too. You don't need Vim, and you can stop reading. If you believe that you could improve your workflow because text editing is the most tedious task as a programmer, then read along. 

Vim is not a religion.

 

How to learn and practice

  • Just type "vimtutor" in your terminal on *nix computer and follow the instructions. In Windows use a git bash terminal. 
  • Install the Vimium plugin in your Chrome browser.
  • You can install Vim plugins in VSCode and IntelliJ Idea. True! You don't have to give up on your favorite IDE and still be better. 
    • Vim plugins are very popular on both VSCode and IntelliJ.

  • Read this article to avoid Googling random stuff on the internet. I will be honest and true to my word to make this an informative and enjoyable read for you. Hang in here with me. 

 

Don't be discouraged if something does not work. Be patient and work with this editor for two (2) weeks. Your productivity will grow many folds with this investment. I learn at least one new thing every week with Vim. 

 

Vim Modes

Vim is modal text editor which means it has several modes. 

  1. Normal Mode: You'll be in this mode most of the time. It's good for you. This is the default mode when Vim is launched
  2. Visual Mode: Use this mode if you are more visual person and want to see what's going on
  3. Insert Mode: Use this mode to insert text. Spend the least amount of time as you can in this mode
  4. Command Mode: Use this mode to execute certain commands, such as saving file, etc. 
  5. Replace Mode: Use this mode to replace text on screen (buffer)

 

Switching modes: 

  1. Normal Mode to Insert Mode: Press i in Normal mode to switch to insert mode
  2. Insert Mode to Normal Mode: Press Esc to go back to Normal mode
  3. Normal Mode to Visual Mode: Press v in Normal mode
  4. Normal Mode to Command Mode: Press Esc + : to enter command mode
  5. Insert Mode to Visual Mode: Switch to Normal mode first and then press v
  6. Normal Mode to Replace Mode: Press R to enter replace mode

 

Click to see demo





Phew! Was that a lot? It was for me at first. Think of it as an investment. You just covered the most important part of Vim, which are its modes. 

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