VIM editor -raspberry pi

Photo of author

By Jackson Taylor

Home » Electronics Topics » Raspberry Pi » VIM editor -raspberry pi

As an Amazon Associate we earn from qualifying purchases.

Vim is a fast, keyboard-driven text editor that runs inside the terminal. On a Raspberry Pi it is handy because you can edit scripts and configuration files without a desktop, even over a remote connection. This guide shows how to install Vim, understand its modes, and create, edit, save and quit a file. The video below walks through the same basics.

Why use Vim on a Raspberry Pi?

Many Raspberry Pi projects are headless, meaning the Pi has no monitor or keyboard attached. In that case you usually connect to your Pi over SSH and work entirely in a terminal. A terminal editor is the natural tool for that job.

Vim is a good choice for a few reasons:

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • It works over SSH with no graphical interface.
  • It starts quickly and uses little memory, which suits small boards.
  • It has syntax highlighting for Python, shell scripts and many other languages.
  • Once you know the basics, you can move around and change text without touching the mouse.

If you only need a quick edit, nano is simpler to learn. Vim takes a little more practice, but it pays off if you edit files often.

Installing Vim

Raspberry Pi OS usually comes with a minimal version of Vim (often reached with the vi command). The full version adds features such as better syntax highlighting. To install it, update the package list first and then install the package:

sudo apt update
sudo apt-get install vim

When the install finishes, type vim in the terminal to check that it opens. To leave it again, press Esc, type :q and press Enter.

Understanding Vim’s modes

The biggest surprise for new users is that Vim is modal. The keys you press do different things depending on the current mode. Two modes cover most beginner needs.

Insert mode (edit mode)

Insert mode is where you type text, like in any other editor. Press i to enter this mode. The word INSERT appears at the bottom of the screen so you know where you are.

See also
simple PWM - DSPIC30F2010

Command mode

Press the Esc key to go back to command mode. Here the keys are commands rather than text. You use this mode to move around, delete lines, search, save and quit. Vim’s own documentation calls this “normal mode”, but many beginners know it as command mode.

If Vim seems to be doing strange things when you type, you are probably in command mode. Press i to start typing, or Esc to get back.

Creating and editing a file

To open a file in Vim, type vim followed by the file name. If the file does not exist, Vim creates it when you save. As an example, here is how to create a Python file:

vim print.py
  1. Press i to enter insert mode.
  2. Type your code, for example print(“Hello from my Raspberry Pi”).
  3. Press Esc to return to command mode.

Once the file is saved you can run it with python3 print.py. If you want more ideas to try next, see these Python projects for your Pi.

Saving and quitting

First make sure you are in command mode by pressing Esc. Then type one of the commands below and press Enter.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
:wq

This writes (saves) the file and quits. Other useful variations are:

  • :w saves the file and keeps it open.
  • :q quits if there are no unsaved changes.
  • :q! quits and throws away any changes you made.
  • :wq or ZZ saves and quits.

Basic commands worth learning

You can get a lot done with a short list of commands. Type these in command mode:

Moving around

  • h, j, k, l move left, down, up and right. The arrow keys also work.
  • jumps to the start of a line and $ to the end.
  • gg goes to the first line of the file and G to the last.

Editing text

  • a starts inserting after the cursor, and o opens a new line below.
  • x deletes the character under the cursor.
  • dd deletes (cuts) the current line.
  • yy copies the current line and p pastes it below.
  • u undoes the last change and Ctrl+r redoes it.
See also
Raspberry Pi Digital Output: Turn an LED On and Off

Searching

  • Type /word and press Enter to search forward. Press n to jump to the next match.

Editing system files

Some files on the Pi, such as those under /etc, belong to the system and need administrator rights to change. Open them with sudo:

sudo vim /path/to/file

Take care here. A mistake in a system file can stop a service or the whole Pi from working properly, so make a copy of the file before you change it. This comes up, for example, when you set up WiFi on the Pi by editing configuration files.

A few helpful settings

Vim reads its settings from a file called .vimrc in your home folder. You can create it with vim ~/.vimrc and add a few lines that make editing code easier:

syntax on
set number
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab

These turn on syntax colouring and line numbers, keep the indentation of the previous line, and use four spaces instead of tab characters. That last point matters for Python, which is sensitive to indentation.

Getting files onto the Pi

You do not have to write everything on the Pi itself. If you prefer to write code on your PC, you can transfer files from Windows to the Pi and then use Vim for quick fixes on the board.

Practising Vim

Vim comes with a built-in interactive lesson. Type vimtutor in the terminal and it opens a practice file that teaches the essentials in about half an hour. It is the quickest way to make the commands feel natural. If the command is not found, the full Vim package may not be installed yet.

FAQ

How do I exit Vim on a Raspberry Pi?

Press Esc to make sure you are in command mode, then type :q and press Enter. If you have unsaved changes and want to discard them, use :q!. To save and quit together, use :wq.

Why can’t I type anything in Vim?

Vim opens in command mode, where keys are commands rather than text. Press i to switch to insert mode, then type. Press Esc when you are done.

See also
FTP -SENDING FILE BETWEEN WINDOWS AND RASPBERRY PI

Is Vim already installed on Raspberry Pi OS?

A minimal version is often included, which you can start with vi. To get the full version, run sudo apt install vim.

Should I use Vim or nano?

Nano is easier for a first quick edit because its shortcuts are shown on screen. Vim is better if you edit code regularly and want faster ways to move around and change text. Both work well over SSH.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works