Linux Command Line

Intro to vim

A text editor (or just editor) is a program for opening and editing text files. This includes the source code files for computer programs. Your text editor is one of the most important programs you use, along with your shell and your compiler.

Text editors can be stand-alone programs or can be part of a larger integrated development environment (IDE). Popular IDEs among beginning programmers include VSCode for Windows and XCode for macOS. As you learn more about the process of programming you may choose to use an IDE and the text editor it provides.

A command line editor is a text editor that runs in your terminal. No matter what development environment you prefer, you should learn the basics of a command line editor. These can be used during ssh sessions with computers that do not support a graphical interface over remote connections.

Choosing a command-line text editor

Image source: xkcd: real programmers by Randall Munroe (image link)

The debate over which command-line text editor is best is one of the oldest internet flame wars, with strong opinions on all sides.

This reference recommends vim as your command-line editor for a few reasons:

  • Vim is universal: you will find vim installed on essentially every Linux computer you encounter. This is because vim extends vi, an older editor and a required utility on all IEEE-POSIX-compliant operating systems. MacOS is POSIX-compliant; major Linux distributions are (mostly) compliant.
  • Vim is popular: vim consistently rates among the most popular development environments, even when competing against graphical IDEs like VSCode or IntelliJ.
  • Vim is powerful: vim's controls let you quickly navigate and edit source code while keeping your hands in typing position. Experienced vim users work very quickly - even across multiple files!
  • Vim is what I use: I am not impartial in the editor war; I learned to program with vim and use it both for writing code and in-lecture demonstrations.

A text editor is a tool and, like all tools, takes practice to develop comfort and mastery. When you first begin, it can be frustrating and slow (especially if you are used to a different editor). Give vim time and you'll find yourself working more and more quickly - possibly even preferring vim to your old editor!

A word processor like Microsoft Word is different from a text editor. Word processors are designed to format written words for printing or reading by humans. They track font choice, size, and style; margin sizes and text alignment; embedded figures; and many other things.

Files created by word processors cannot be understood by compilers or interpreters that prepare or run program source code. You must use a text editor to write source code that these programs understand.

The best way to learn to use vim is by doing! Vim comes with a built-in tutorial that walks you through many common editing tasks.

You can start the tutorial with the command

$ vimtutor

Most people need about 30 minutes to work through the full tutorial.

You may also enjoy VIM Adventures, a browser-based adventure game controlled with vim keystrokes.

Starting out with vim

You can open a new, blank text file by typing vim into your shell. You can also open an existing file by providing its path as an argument.

vim - a programmer's text editor

$ vim [options] [file]...

If you begin typing immidately after opening vim you may find yourself at a loss. Letters may not appear on-screen; the cursor may jump through your file; letters (and whole lines) may change or disappear; and you may not be able to quit back to your shell prompt!

Fear not! Your initial confusion (or panic) won't last long once you understand how vim differentiates between keyboard controls and text input.

Vim operating modes

vim is a modal editor, meaning it switches between different modes of operation. The mode vim is operating in determines how it interprets your keystrokes. This lets the same key have different meanings. For example, pressing 'j' in insert mode types a 'j' in the file, while pressing it in normal mode moves the cursor down one line. Compare this to modeless editors (like emacs or nano) where you change the meaning of a key by holding down Ctrl or Alt (or both!).

vim has four operating modes, each with its own purpose:

  • Normal mode is the default mode and the mode vim starts in. While in normal mode your keystrokes move your cursor through the file; make edits like cuts, copies, or pastes; or switch vim to another operating mode. You can return to normal mode from any other mode by pressing Escape.
  • Insert mode is the mode for typing text in the file. While in insert mode most of your keys will type letters at the cursor. The easiest way to enter insert mode is to press 'i' while in normal mode; you'll know you're in insert mode if -- INSERT -- appears at the bottom of the screen.
  • Command mode is the mode for editor-level commands like opening a file; saving your work; and quitting out of vim. You enter command mode by pressing ':' while in normal mode. You'll see a colon and your cursor appear at the bottom of the screen while you type a command.
  • Visual mode is for selecting text. Once selected, you can use normal mode instructions or command mode commands (by pressing ':') to affect the selected text. It comes in two forms:
    • Visual mode lets you highlight letters, words, or lines using normal mode navigations. You enter visual mode by pressing 'v' while in normal mode; you'll know you're in visual mode if -- VISUAL -- appears at the bottom of the screen.
    • Visual line mode lets you highlight whole lines of text. You enter visual line mode by pressing 'V' while in normal mode; you'll know you're in visual line mode if -- VISUAL LINE -- appears at the bottom of the screen.

Example: Writing hello.cpp with vim

You can follow these steps to create a new text file, named hello.cpp, containing the "hello world" program in C++:

  1. Open your terminal/shell and navigate to the directory where you want to create your source code file.
  2. Start vim and open a new file named hello.cpp
    • At your shell prompt, type:
      $ vim hello.cpp
    • You'll see "hello.cpp" [New File] at the bottom of your screen.
  3. Press the 'i' key to enter insert mode.
    • You'll see -- INSERT -- at the bottom of your screen.
  4. Type the source code for your hello world program.
  5. When you are finished inserting text, press Escape to return to normal mode.
  6. Type ':' (to enter command mode), then 'w', then press Enter to save ("write") your changes to the file.
    • Before you press Enter you'll see :w at the bottom of your screen.
    • After you press Enter you'll see something like "hello.cpp" [New] 7L, 103C written (the number of lines and or characters may be slightly different depending on your version of the program).
  7. Type ':', then 'q', then press Enter to quit and return to your shell.

You are now ready to compile your hello world program with g++ or another compiler.