Tech

Vim–The Vi IMproved Text Editor

As happens SO MANY times when I am learning to program, I start off on one topic, and then, the journey quickly swerves into unchartered territory and I find myself in the deep reaches of the Google-verse searching up topics I didn’t even know existed or previously knew little to nothing about.

It was during one such journey that I stumbled across Vim.

Upon first learning about it, I quickly opened it up in my command line to see what it was, and then, just as quickly, I ended up hating it. As seems to be the norm for many a Vim newbie.

But seeing that is was a lightweight program that didn’t seem to use many resources, as opposed to the some of the machine hungry larger text editing programs, I decided to explore it more. This was partly out of necessity, because the aging MacBook Pro I’m using is getting up their in retirement-age years and I have to use programs wisely to extend memory and other resources.

But this was also partly because it seemed to be very esoteric and like an impressive skill to know.

So bridging these two reasons I set out to force myself to learn it at a simply basic level.

Thus my Vim journey began.

According to it’s website:

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X.Vim is rock stable and is continuously being developed to become even better. Among its features are:

  • persistent, multi-level undo tree
  • extensive plugin system
  • support for hundreds of programming languages and file formats
  • powerful search and replace
  • integrates with many tools

After initial hesitation and skepticism, I’ve come to really enjoy using Vim. I like how quickly I can get it up and running. And it pushes me to continually look up information and learn new things about it (a gift and a curse, I suppose).

As I’m learning Vim, there’s obviously a plethora of info available through a google search, but Vim also comes loaded with a tutorial called vimtutor.

There’s also a handy online learning tool here: https://www.openvim.com/.

Vim–How to Cut, Copy & Paste Text

Copy & Paste:

Select/Highlight:

First to select the characters use ‘v’ to enter ‘VISUAL’ mode. (Hit ‘v’ again to go back to “normal” mode.) We can select (highlight) one character at a time by hitting the cursor left or right. We can also hit up or down to select the current line and the next line up to the character column that we are spaced at.

Shift-‘v’ or Uppercase ‘V’ enters ‘VISUAL LINE’ mode. (Hit shift-v again to go back to “normal” mode.) This will select the entire current line. We can then move up or down to select (or deselect), by highlighting, the lines we want.

Control-‘v’ enters the ‘VISUAL BLOCK’ mode. (Hit control-v again to go back to “normal” mode.)

The best way of learning this and getting used to it while having fun is to open and dummy/test file and just start playing and experimenting with what happens. We can always ‘Google’ to find out exactly what a command does if you’re unsure about pressing it first (usually always recommended).

Copy:

In VISUAL mode hit ‘y’ to yank (copy) the selected characters.

Hit ‘x’ to cut the selected characters.

Paste:

Hit ‘p’ or ‘P’ to paste the cut/copied characters.

Links:

VIM User Manual: http://vimdoc.sourceforge.net/htmldoc/usr_toc.html

Vim Commands

The following is a list of helpful Vim commands that I forget often enough that it warrants a ‘quick-reference, look-up guide’ for me, and maybe others will find it valuable was well.

First, one key thing when learning Vim is to simply do the “Vim Tutor” tutorial program habitually when you are serious about learning Vim.

You can simply type the command “vimtutor” in a terminal and it should come up.

It says completion time can vary from 25-30 minutes depending on user experimentation. Start there.

Okay. Let’s dive in to the rest…

Scolling the Screen:

  • Ctrl + f (to scroll forward one screen)
  • Ctrl + b (to scroll backward one screen)
  • Ctrl + d (to scroll forward half screen)
  • Ctrl + u (to scroll backward half screen)

Setting The Stage

Setting up a great environment to write in is key. Vim lets us use the :set command to change two types of options. These are either toggle options (on or off), and options that take a numeric or string value (such as the name of a file or the location of a margin).

:set option (To turn a toggle option on.)

:set nooption (To turn a toggle option off.)

Examples are :set ic (to specify that pattern searches should ignore case.)

:set noic (to return to being case-sensitive in searches.)

Or :set nu (to show line numbers)

:set nonu (to not show line numbers)

Note: During a session, you can check which options are being used by typing:

:set all

which will display the complete list of options, including options I have set and defaults that vi has “chosen”.

You can find the current value of any individual option by name, using the command:

:set option?

Copy & Paste:

Select/Highlight:

First to select the characters use ‘v’ to enter ‘VISUAL’ mode. (Hit ‘v’ again to go back to “normal” mode.) We can select (highlight) one character at a time by hitting the cursor left or right. We can also hit up or down to select the current line and the next line up to the character column that we are spaced at.

Shift-‘v’ or Uppercase ‘V’ enters ‘VISUAL LINE’ mode. (Hit shift-v again to go back to “normal” mode.) This will select the entire current line. We can then move up or down to select (or deselect), by highlighting, the lines we want.

Control-‘v’ enters the ‘VISUAL BLOCK’ mode. (Hit control-v again to go back to “normal” mode.)

The best way of learning this and getting used to it while having fun is to open and dummy/test file and just start playing and experimenting with what happens. We can always ‘Google’ to find out exactly what a command does if you’re unsure about pressing it first (usually always recommended).

Copy:

In VISUAL mode hit ‘y’ to yank (copy) the selected characters.

Hit ‘x’ to cut the selected characters.

Paste:

Hit ‘p’ or ‘P’ to paste the cut/copied characters.

Execute External Unix Commands–Vim allows users to execute and display or read Unix command-line controls from the Vim program itself. An exclamation mark (!) helps get this done.

For example,

:!command

So you can enter,

:!date

to display the time and date to the screen (or feed that output somewhere else), and press “Enter” to continue on in the Vim environment.

Vim Tutorial Lessons:

1.1: Moving the Cursor

** To move the cursor, press the h (left), j (down), k (up), l (right). **

1.2: Exiting Vim

Type :q! to exit the editor, DISCARDING any changes you have made.

1.3: Text Editing-Deletion

Press x to delete the character under the cursor. **

1.4: Text Editing-Insertion

** Press i to insert text. ** (insert before the cursor)

1.5: Text Editing-Appending

** Press A to append text. ** (append after the line)

1.6: Editing a File

** Use :wq to save a file and exit. **

Links:

VIM User Manual: http://vimdoc.sourceforge.net/htmldoc/usr_toc.html

See also:

Vim–The Vi IMproved Text Editor

Vim–How to Cut, Copy & Paste Text

Biblio:

Vim, accessed October, 23, 2020, https://www.vim.org/.

OpenVim, https://www.openvim.com/.

“Vim (text editor),” Wikipedia, accessed October, 23, 2020, https://en.wikipedia.org/wiki/Vim_(text_editor).

“Where Vim Came From,” Two Bit History, August 5th, 2018, accessed October 23, 2020, https://twobithistory.org/2018/08/05/where-vim-came-from.html.