Skip to main content
Basic Programming In C
Basic Programming In C
Easy 1h

Principle

To launch the editor, simply type vim in a terminal. Unlike regular text editors, vim has 4 operating modes: interactive mode, insertion mode, visual mode, and command mode:

  • interactive mode is the default mode. It is the mode you are in when you launch vim or when you are in another mode and press the ESC key. In this mode, you can navigate through a file, copy-paste, delete text, replace text…
  • visual mode allows you to easily select words, lines, blocks of text and perform operations on them. This mode is activated from interactive mode by pressing v.
  • insertion mode is the mode that allows you to write text. From interactive mode, simply press the i key to enter insertion mode.
  • command mode is the mode that allows executing vim commands or system commands. From interactive mode, press the : key then type the command you want to execute.

First Manipulations

Difficulty: Easy
  1. Launch vim in a terminal
  2. Press i to enter insertion mode
  3. Type a minimal C program
  4. To save, press ESC (to exit insertion mode) then type :w test.c to save what you have written (: allows entering command mode, w is the save/write command and test.c is the filename)
  5. Still in interactive mode, navigate through the file with the keys h (left), j (down), k (up), l (right). You can also use the arrow keys.
  6. To quit vim, simply type :q or :q! if you want to force closing (by default vim does not quit if there are unsaved changes).

It is possible to save and quit with the command :wq

Besides the keys h, j, k, l, others are useful such as:

  • 0 which automatically goes to the beginning of the current line
  • $ which automatically goes to the end of the current line
  • gg which goes to the first line
  • G which goes to the last line
  • xG where x is a number goes to line x

Cut / Delete

  • To delete a character, type x or yx where y is the number of characters to delete
  • To delete a line, type dd or xdd where x is the number of lines to delete
  • To delete a word, type dw (delete word) or xdw where x is the number of words to delete
  • To delete from the beginning of the line to the cursor, type d0
  • To delete from the cursor to the end of the line, type d$

Copy

  • Same principle as for cutting, except the command is yy to copy a line, yw to copy a word, y0 to copy…

Paste

  • To paste text, type p or xp where x is the number of times to paste. Note that pasting happens immediately after where the cursor is; there is no line break.

Replace

  • To replace a single letter, place the cursor on the character to replace and type rs where s is the new letter to insert.
  • To replace text, it is possible to use the syntax :s/old/new which replaces the first occurrence of old with new; variants exist:
    • :s/old/new/g: replaces all occurrences on the line where the cursor is
    • :#,#s/old/new/g: replaces all occurrences on lines # to # of the file
    • :%s/old/new/g: replaces all occurrences throughout the entire file

Undoing Changes

  • To undo a change, type u or xu where x is the number of times to go back

Searching for a Word

  • To search for a word after where the cursor is, type /word. To go to the next occurrence, type n or N for the previous occurrence
  • To search for a word before where the cursor is, type ?word, the rest is the same

Launching an External Command

  • Simply type the command name preceded by :! such as :!ls to display the content of the current directory

Opening Another File

  • In vim simply type :e filename to open the file named filename. You can then navigate between files with :e #x where x designates the file number.

The Configuration File

Difficulty: Rx

There are many options in vim that make it more pleasant to use (syntax highlighting, line numbers, aliases…). Here are a few that you can write in a .vimrc file that you will place at the root of your home directory (this saves you from having to enable the options every time you open vim).

# Example .vimrc configuration
# You can customize vim with these options
  • set nocp disables compatibility mode
  • syn on enables syntax highlighting
  • set syntax =on enables syntax highlighting
  • filetype indent plugin on indentation based on file type
  • set nu displays line numbers
  • set showmatch highlights missing braces/parentheses
  • set tabstop =4 tab size in spaces
  • set shiftwidth =4 tab size in spaces
  • set softtabstop =4 tab size in spaces
  • set expandtab converts tabs to spaces
  • set cursorline highlights the line where the cursor is
  • iab #i #include example alias: simply type #i then tab to have it replaced by #include