Back to main

Back to lecture 3

VI basic

This document is a short introduction to vim, a powerful text-based editor

You should consider using vim because

  • it is available virtually on every unix/linux/osx sytem
  • it does not need a GUI to work (you can just connect via ssh to your favourite server and use it straight away)
  • it's very quick in displaying and editing your text
  • it has a powerful set of commands for advanced editing.

If you are programming or manipulating datasets, good chances are you will love it.

You might have heard that learning to use vim is hard. It is not. Just follow these initial steps to start being productive with vim.

Some history

Vi (pronounced as distinct letters, /ˌviːˈaɪ/) is a screen-oriented text editor originally created for the Unix operating system 1.

Vim (vi improved) is a free and open-source, screen-based text editor program. It is an improved clone of Bill Joy's vi 2.

Vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session) 1.

Starting vim

To start vim, you can proceed in one of the following ways from :

> vim

# or

> vim sample.txt # to open a new or an existing file

# or

> vim ./    # to browse the local files. You can use any other target
            # directory rather the one where you are (./)

[!TIP] To open a file: vim file_name

Vim modes

  • vim has two different main operation modes, the COMMAND and the INSERT mode (other modes will be discussed later)
  • in the COMMAND mode, what you type will not be inserted as a text, but will be interpreted as a command
  • in the INSERT mode, what you type will be inserted as text
  • you can recognise that you are in INSERT mode when the text **--INSERT--** appears in the bottom line of your editor
  • you can recognise that you are in COMMAND mode when the bottom line of your editor is empty, or has anything else written than **--INSERT--**
  • to switch to INSERT mode, you can press i (you learned your first command!)

[!CAUTION] vim starts in COMMAND mode – don't forget to press i to insert some text!

Insert mode

To activate the insert mode, just type i. Now, by pressing any key on the keyboard, you can write a character.

To exit the insert mode, press ESC.

Command mode

Edit

In the command mode, it is possible to edit the text by pressing some key combination:

Copy line: yy

Paste line: p

Delete line: dd

[!TIP] Deleting a line with dd actually cuts it. This means you can paste that line by simply pressing p.

Save and quit

In the command mode, just type the following commands:

Save: :w

Quit: :q

Save and quit: :x

Select text

You can either select entire lines, few characters or text-blocks:

Select lines: Maiusc + V

VI advanced

Here you can find some advanced topics, like:

  • Open more tabs
  • Find/replace text

References


  1. https://en.wikipedia.org/wiki/Vi_(text_editor) ↩2

  2. https://en.wikipedia.org/wiki/Vim_(text_editor)