Vim

Metadata

Hello! This is the markdown transcript for a presentation held on October 13th. You can view the writeup in PDF format here.

Video Recording

Note that the first section discussing why Vim? and History were cut due to recording errors. Sorry about that!

Why Vim?

Why Vim?

History

Bill Joy, the creator of vi

Basic Knowledge

Goals

We will try to learn:

Modes

Modes vim is known for a modal editing scheme.

What is modal editing?

In vim, various things that you typically do while editing text is split into different modes.

Mode Purpose How to Enter
Normal Move around and manipulate existing text Default, can return to by pressing in other modes
Insert Allows you to type text like in any standard editor i, a, x, c, o, among others in Normal mode
Visual Allows you to select regions of text as if you were using a mouse v, Shift + v, Ctrl + v in Normal mode
Command Allows you to execute commands like save and exit : in Normal mode

There are other additional modes that vim has out-of-the-box, but these are the basics.

Why should editing be split into modes?

Keybinds

Modes allow for vim perform actions using simple and easy to remember keystroke sequences! These are known as motions!

Example

Navigating can be done with h, j, k, l in Normal mode. These corrispond to <Left>, <Down>, <Up>, <Right> respectively.2

This allows you to keep your hands on the home-row of your keyboard (especially good if you are a touch-typist)!

Example 2

Additionally, an arbitrary number can be prefixed to modify motions.

One usecase is 5 + j, which moves down 5 lines in one input sequence.

How can you easily remember these keybinds?

Most keybinds are mnemonics for their corrisponding action.

There are a billion keybinds in vim, so don’t fret about learning all of them at once. Learning as you go is the method here!

vim motions are extremely powerful. They let you condense the work of selecting text and changing it into easy to remember input sequences!

Example 1

cc lets you delete a line and start retyping text in one shot (change a line)! Double pressing most actions will allow you to apply it to the line your cursor is on.

Before

                This is a sentence!
                The cursor is below this line.
                _Emacs users are stinky :(
                another wacky line!

After

                This is a sentence!
                The cursor is below this line.
                |
                another wacky line!

Example 2

dw lets you delete a word.

The works to specify your selection after any action! So, this will work with too!

Before

                This is a sentence!
                The cursor is below this line.
                _Emacs users are stinky :(
                another wacky line!

After

                This is a sentence!
                The cursor is below this line.
                _users are stinky :(
                another wacky line!

Example 3

Commands can be chained too! 2k and dip Lets you move 2 up (k) and delete the inner paragraph.

Before

                This is a sentence!
                The cursor is below this line.
                _Emacs users are stinky :(
                another wacky line!

After

                _

Other useful actions include (but not all)...

Commands

There are lots of commands in vim, just like actions, but we’ll just go over a few. To enter Command mode, just prepend a : before typing it!

You can also chain commands, so :wq for example saves your file and exits in one shot.
Also, / and ? let you search within your document.5 n and N let you go backwards/forwards through your search results.

Crash Course

Lets try a hands-on demo!

Firstly, open a terminal!

Challenge!
I want you to create a simple C program using vim and sucessfully compile it! It can be as simple as "Hello, World!" if you’d like!

        #include <stdio.h>

        int main(int argc, char **argv) {
            printf("Hello World!\n");
        }

If thats too easy for you, try these extra challenges:

Closing Remarks

Me :3

image

The information in this presentation will be made available on our website!
https://lug.cs.uic.edu

Join our Discord!

https://discord.gg/NgxTR7PX5e

  1. a.k.a. commands↩︎

  2. People consider using the arrow keys in vim a cardinal sin.↩︎

  3. you can specify a filename afterwards to emulate↩︎

  4. add an ! to quit and ignore unsaved changes↩︎

  5. use :noh to clear highlighted results after↩︎

October 13, 2023