So, you’re using a Raspberry Pi board, and are wondering which IDE you might use to write your code. As the Raspberry Pi resources are quite limited, and you’ll often use it headless (without a screen), the number of options you have is also quite limited. In this post I’ll show you how to install and use Vim on your Raspberry Pi board.
This tutorial is working whether you run Raspbian, Noobs, Ubuntu, or almost any Linux version.
Watch this video as an additional resource to this article:
After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!
Table of Contents
Why using Vim on Raspberry Pi
First of all, I want to make sure this post is not another battle such as Vim vs another IDE. I will not try to convince you that Vim is THE best IDE. I’ll just show you why it’s great on a Raspberry Pi.
You are learning how to use Raspberry Pi to build your own projects?
Check out Raspberry Pi For Beginners and learn step by step.
Here are some advantages of using Vim on Raspberry Pi:
- When you know how to use Vim, you can install, configure, and use it everywhere. You’ll just need a few seconds to install Vim and fully configure it for your needs. Very handy, especially if you are working with multiple Raspberry Pi boards. If you flash a new image, you’ll lost your config and you don’t want to spend a lot of time re-configuring everything.
- If you run your Raspberry Pi headless – without a monitor, keyboard, etc -, using Vim will allow you to program on your Raspberry Pi with only a ssh connection, and still get a good development environment. You can develop directly from your own computer.
- Using Vim along with command line tools is very handy. Mostly you’ll make experiments on your Raspberry Pi, and having a text editor/IDE close to the terminal is a good thing.
- You can install a server image with no GUI desktop and still be able to use Vim to program on your Raspberry Pi, including a multi-tab editor, using auto-completion, syntax highlighting, etc.
First, you need to get a ssh connection to your Raspberry Pi. You can also open a terminal if you’re using a graphical desktop.
To install Vim you’ll just need to type one command line:
$ sudo apt install vim
Vim will take about 30MB of memory, which is really not a lot, when you compare with some other IDEs. And space matters on a Raspberry Pi!
You can now start to use Vim with the
vim command line tool. If you’re using Vim for the first time, type
vimtutor and you’ll get a complete tutorial with the basic commands. Vim can be hard to use at first, so you’ll need a little bit of practice. After some time, you’ll be able to work as fast as with any other text editor/IDE.
Configure Vim on Raspberry Pi
It’s really convenient to configure Vim. You’ll just need to use… Vim!
As you may already know, you have a .bashrc file in your home repository. The content of this file will be executed as soon as you create a new session – ssh, open a terminal, etc. But… What is the relation with Vim?
Vim uses something similar. There is a file called .vimrc, also on your home repository (you can create one if it doesn’t exist). As soon as you open a new Vim editor, the content of the .vimrc will be executed in Vim. So, if you understand the concept behind how .bashrc works, well it’s the same for the .vimrc.
You’ll write all your Vim configuration inside this file. That’s why it’s really convenient. If you happen to change to a new Raspberry Pi image, all you have to do is :
1. Reinstall Vim
2. Import your .vimrc
And that’s it!
Let’s open the .vimrc with
" Syntax highlight syntax enable " Tabs are spaces set expandtab " Number of spaces per tab set tabstop=4 " Search as soon as characters are entered set incsearch " Highlight search results set hlsearch
This is a pretty simple configuration to start. You don’t need to follow it, you can add or remove whatever you want from this file. As you progress with Vim, you’ll add many other options that suit your needs for programming.
To get a description of an option, directly inside Vim type
:help 'option'. To get the list of all options, type
:options. And then there is Google!
Add Vim plugins to boost your productivity
For now, you have Vim on Raspberry Pi, but you haven’t unleashed the true power of Vim on embedded systems yet.
Vim plugins allow you to use Vim just as any other IDE with rich syntax highlighting, auto-completion, folder-tree view, debugging tools, and many other nice and useful functionalities.
For managing plugins, I personally use Vundle. With Vundle, you’ll be able to install all your plugins, directly inside the .vimrc.
Get Vundle on your Raspberry Pi by typing this in your terminal:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Now, open your .vimrc and add those lines at the beginning:
set nocompatible filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' " Add your plugins below this line " All of your Plugins must be added before the following line call vundle#end() filetype plugin indent on
This is the minimum configuration to install new plugins. To get more information about Vundle, check out the Vundle github page.
Now, you can add any plugin you want to try (between the 2 comments), and use the
:PluginInstall command inside Vim to install them. Don’t forget to run this command after you’ve added a plugin.
On VimAwesome you can get a great list of plugins. This website sorts the plugins by popularity on GitHub. It also gives you for each plugin – if available – instructions to install via Vundle.
For example, one of the first plugin you’ll want is NerdTree. NerdTree gives you a complete file system browser on a tab inside Vim. Thus you can program only from one terminal on your Raspberry Pi, and switch from file to file with NerdTree inside Vim. This is a huge time saver.
To install NerdTree, add
Plugin 'scrooloose/nerdtree' inside your .vimrc and type
Another cool plugin is the YouCompleteMe plugin, which adds autocompletion. Install it by adding the line
Plugin 'valloric/youcompleteme' to your .vimrc.
Well, you get the idea! Browse the VimAwesome website to get some ideas for the main plugins you’ll need to use. Oh, and you can also customize the plugins if you want!
Here’s a recap of the .vimrc with everything we’ve added in this tutorial:
set nocompatible filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'VundleVim/Vundle.vim' " Add your plugins below this line Plugin 'scrooloose/nerdtree' Plugin 'valloric/youcompleteme' " All of your Plugins must be added before the following line call vundle#end() filetype plugin indent on " Syntax highlight syntax enable " Tabs are spaces set expandtab " Number of spaces per tab set tabstop=4 " Search as soon as characters are entered set incsearch " Highlight search results set hlsearch
Wrapping things up
Using Vim on a Raspberry Pi is a great time saver, and very convenient if you use your board headless, or you use multiple boards.
Let’s imagine you are developing an app to connect your home to the cloud, through a Raspberry Pi board. You’ll have to develop on a headless web server, a possibly headless Raspberry Pi, and maybe also your computer. Vim allows you to use the same editor with the same settings for all of them.
You’ll spend some time to understand how things work at first, but don’t give up too soon! Eventually you’ll be confident using Vim and you’ll find that it’s one of the best solution to program directly on a Raspberry Pi board.