VIM Motions Tutorial

       Welcome, today I will instruct you on how to use my favorite text editor, VIM. Vim is a free and open-source, screen-based text editor program. It is an improved version of vi. Vim was authored by Bram Moolenaar in 1991, he remained the sole maintainer of this project until he died on august 3rd 2023.

You'd be surprised to know that every single person in this classroom with a mac computer has Linux installed on their computer. Writers, programmers and enthusiasts today continue to use vim despite its age due to its unique philosophy, which employs the use of different modes that allow you to navigate within a line, within a word and within a file.

Today I will teach you how to create and edit a new file with VIM on your Mac-book. In our case we will be using the "terminal" app on your mac to access vim and create a new python file. "python" is also something that is installed on all macs by default. We will be writing an running a simple python program that takes user input through the terminal command line interface and displays a message. Learning to write code with vim as opposed to plain text will be a bit more tricky and will reinforce VIM core concepts.

Who is Vim for?

Journalist
Registers can save large chunks of information for later so that potential paragraphs can be deleted but kept temporarily.
Programmer
Vim's powerful editing features, such as syntax highlighting and code formatting, make it ideal for writing and editing code efficiently.
Everyday Person
Vim can be used for quick text editing tasks on any computer without the need for installing additional software.
System Administrator
With Vim, system administrators can edit configuration files and scripts directly on the server, leveraging its powerful search and replace capabilities.
Academic Researcher
Vim's distraction-free environment and markdown support make it perfect for writing research papers and keeping notes organized.

Pre Requisites

Opening Vim and Creating a new File

Instructions for finding and opening VIM:

  1. Press the CMD and Space keys on your mac at the same time
  2. You should see a "Spotlight search" window popup
  3. Type "Terminal" to search for an application called terminal
  4. Select the application that shows an icon of a black square with '>' and "_" letters displayed within the image of the black box.
  5. Use the mouse to select and launch this application.

Ensuring that VIM and python are installed

  1. While mac should have these utilities installed by default, we will double check that "vim" and "python" are installed in your terminal
  2. Try typing "vim --version" into your terminal application, and hit enter to execute the command
  3. If the system responds with "command not found" then you should not continue the tutorial and you should seek assistance.
  4. Try typing "python --version" into your terminal application, and hit enter to execute the command
  5. If the system responds with "command not found" then you should not continue the tutorial and you should seek assistance.

Creating a new file

  1. Once you ensure that vim is installed you can launch the application
  2. Launch vim and create a new file called "hello.py" by running the command "vim hello.py" and hitting enter.
  3. You should see your screen change from a command line interface to an empty text buffer (the vim editor screen).
  4. Very importantly, do not try typing text as soon as you land on the vim editor.

Changing from Normal mode to insert mode

  1. When you first enter VIM you are in "NORMAL mode" there should be some kind of indicator somewhere on the screen that indicates that you are in normal mode
  2. In normal mode every single letter on the keyboard represents a command, for example in normal mode typing "dd" would delete the line that the cursor is currently on as opposed to a regular text editor which would always display the letters "dd" if you typed the letter dd.
  3. try to enter a traditional typing mode you must type the letter 'i' from normal mode to enter what is called "insert" mode.
  4. make sure you didn't press any keys in between first opening VIM and typing 'i' or you may not have actually entered insert mode.
  5. Verify that you have entered insert mode by looking for the word "insert" in the bottom left corner of the terminal window.
  6. Try exiting insert mode and going back into normal mode so that you can get a bit more comfortable with it
  7. Exit insert mode by pressing ESC
  8. Verify that you are now in normal mode by checking the bottom left corner of the terminal window.
  9. enter insert mode by pressing 'i' from normal mode

Typing our Starter Python Code

  1. you can copy and paste this code into vim or type the code below into vim from insert mode.
  2. note that there may be some errors in the code that we will need to go in and fix later.
    
    # script to take user input of a name That name display a personalized greeting
    
    try:
        # For Python 3.x
        userInputName = input("please enter your name: ")
    except NameError:
        # Fallback for Python 2.7
        userInputName = raw_input("Please enter your name: ")
    
    # Display the greeting
    print("Hello " + userInputName)
                                                        
  3. Double check that what you have typed matches this exactly, check that the tabs and spaces are the same as well. and that the first and last lines of the file are non-blank.

Navigating in normal mode to fix our issues:

  1. You should see a visual cursor on the screen when you are in normal mode, use the arrow keys to navigate your cursor around the words and lines.
  2. 'gg' will take you to the very first line of a file.
  3. 'G' will take you to the very last line of a file.
  4. DO NOT use your mouse for navigating the file, that's cheating and it's against the core philosophy of VIM. And also it might not work.

Fixing our Issues

Issue 1 line 1: "# script to take user input of a name that name display a personalized greetingg"

Issue explanation: "greetingg" has an extra 'g' at the end of the word

Issue 2 line 1: "# script to take user input of a name that name display a personalized greetingg"

Issue explanation: "That name" the 9th and 10th words in the line should both be replaced with the word "and" so that the sentence reads: "script to take the user input of a name and display a personalized greeting"

Issue 3 line 5: ' userInputName = input("please enter your name: ")'

Issue explanation: "please" - should be capitalized like it is in line number 8.

Exiting Vim and Running our Code

After fixing the issues: exiting VIM.

  1. After fixing the issues, make sure that the indentations did not change at any point throughout your editing compared to the original provided code, if they did then the code will not run.
  2. To exit vim and go back to the command line interface that you started from
    • First make sure you are in normal mode by pressing ESC.
    • Press "ZZ" (make sure you use capital letters) to go back to the terminal command line interface that you started from. This normal mode command will automatically save and quit the file.

Running the code.

  1. Type "python hello.py" into your terminal interface and hit enter.
  2. If you do not get any errors running your code and you followed all steps correctly, you should see a message input in your terminal that says "Please enter your name: " after running the command.
  3. Type your name into this terminal input program that you just created and hit enter.
  4. The program should display "Hello" + (The name that you entered).
  5. Your terminal should return back to its original state.