Vim

I waited so long to write about technology! This is my first technology post and it’s about the best text editor in the world: Vim.

Note:

  1. Following is everything I want to write about Vim, so might be a bit long.
  2. You need to practice multiple times till the commands become your physical memory. Practice makes perfect! Nobody is a genius!

Why Vim

I envy the geeks on YouTube who could just type Vim commands and my eyes couldn’t follow. To be a geek, I have to know Vim first.

My Experience with Vim

I started to use Vim when I took the course Computer Architecture and Systems Programming in my junior year, because the only other option for an editor in terminal is Nano, which I hated a lot. I didn’t seriously wrote my .bashrc, .vimrc, and tmux.conf till the following summer. However, I didn’t improve my Vim skills afterwards, since there weren’t many scenarios to force me to, and I was pretty happy with VSCode and Sublime Text.

When I worked at GS, I used less more than Vim. There were few times requiring my Vim skills to edit files on a server, but far from getting more familiar with it. Though I was busy with work, I always wanted to refresh my skills on Vim when I get time.

Now is the time.

Vim or NeoVim?

TBH, essentially both behave mostly the same, but NeoVim gives me a better time. So I will write about NeoVim in the following article. ???

Fun fact: Vim stems from Vi and stands for Vi IMproved. The prefix “Neo” in “NeoVim” signifies that NeoVim stems from Vim. But why isn’t NeoVim called “Vimim”? However, if were so, how do you pronounce this word? 😬

Note: for NeoVim, you don’t type neovim to start it, but nvim. Shorter and cooler, huh?

How to download and install NeoVim

For my Mac, I use brew install nvim to install NeoVim and brew upgrade nvim to upgrade it.

###

Commands

If you don’t know a command in Vim, you could always do :help <Command>, despite your command being a key combination or plain chars.

Start Vim, insert, save, and quit

When you enter Vim with command vim, the default mode is normal. To start typing, you need to enter the insert mode, and here is how:

By pressing a, or i, or o, you can insert at different places:

Command Usage Comment
a append after the current character
i insert before the current character
o open a new line below  

And here are A, I, O:

Command Usage Comment
A append at end of line  
I insert at start of line  
O open a new line above opposite from o, which opens a new line below

To go back to normal mode, you just press esc.

In normal mode, press:

  • to save, :w and Enter
  • to quit, :q and Enter
  • to quit w/o saving, press :q! and Enter
  • to save and quit, press :wq, or :x and Enter; or just Shift + ZZ, w/o “:” to enter command mode

Understand the Modes

Most users are surprised that there are so many modes in Vim: normal, insert, visual, command.

Why does Vim need so many modes while in most other editors you just click with your cursor at the desired spot and start editing? Initially it’s designed for use both from a command-line interface and as a standalone application in a GUI. Without a mouse/cursor, the user needs the normal mode to navigate around the file.

When we go into a file, rather than just editing, more often we read, select, and go to the spot where we want to edit. Thus, we need faster ways to navigate, which is what Vim is good at and why it is still used today after almost 3 decades since its initial release in 1991.

Here are what the four modes are for and how to enter them:

Mode Usage How to enter Common Commands
normal navigate and enter commands - default mode when entering vim.
- in other modes, press esc or Ctrl + [ to come back to this mode
see next section ???
insert edit the document, like any other editor see last section ???link also works in shell:
- Ctrl + h, delete prev char
- Ctrl + w, delete last word
- Ctrl + u, delete from current current position to start of line (will keep any char afterwards)
visual select chars - v, select from current char
- Ctrl + v, select a block using hjkl
- Shift + v, select the whole line, could then hjkl to select more lines
after selecting, could d (delete), y (yank/copy)
command execute commands : - :w, :q, :wq, see last section ???
- :set nu, set line number
- :vs, vertical split
- :sp, horizontal split
- :% s/foo/bar/g, substitute foo with bar globally
- Ctrl + D, see possible completions

How to move your cursor around in normal mode

Again, the default mode of Vim is normal, and normal mode provides us so many cool commands!


Left, Down, Up, Right, and last edit

Vim uses hjkl becauses they are the keys your right hand could reach the fastest. Once I get used to them by pressing them and moving my cursor around, they are pretty intuitive to me.

Move by one char:

  • h $\leftarrow$
  • j $\downarrow$
  • k $\uparrow$
  • l $\rightarrow$

To go to the position of last edit, press gi.


move between words

w or W (word) moves to the start of next word

e or E (end) moves to the end of next word

b or B (back) moves to the start of last word

The difference between the three lowercase and respective capitalized commands above is how you define a word:

  • the lowercase command sees a word delimited by non-alphanumeric char
  • the capitalized command sees a word delimited by space

So the capitalized commands move faster than the lowercase ones.

Personally, I use w and b most often.


move to a char on the current line
  • f<char> (find) moves to the next <char>
  • t<char> (till) moves to the char before <char>

Despite search successful or not, could press ; to go to the next and , to go to the last.

To search for a prev char, could F<char>, which is equivalent to f<char> then ,.


move to the start/end of current line
  • 0 moves to first char
  • ^ moves to first non-whitespace char

To move to the end of current line, could:

  • $ moves to last char
  • g_ moves to last non-whitespace char

Personally, I use 0 and $ most often. To avoid remembering ^ and g_, I would press 0 and then w to move to the first non-whitespace char, and press $ and then b to move to the last non-whitespace char.


move between sentences/paragraphs (not used often)
  • ( or ) to move between sentences
  • { or } to move between paragraphs

could use easy-motion


move to start/end of document and last edit
  • gg goes to the start of document
  • G goes to the end of document

To go back to position of last edit, could do Ctrl + O.


move to the head/middle/bottom of a page

On a screen,

  • H (Head) goes to the head
  • M (Middle) goes to the middle
  • L ( Low) goes to the bottom

move pages up/down

Move by half page:

  • Ctrl + U (Up) goes $\uparrow$
  • Ctrl + D (Down) goes $\downarrow$

Move by full page:

  • Ctrl + B (Backward) goes $\uparrow$
  • Ctrl + F (Forward) goes $\downarrow$

zz positions the current line to the middle of screen.

Created on Dec 3, 2019