How to Install & Use Vim on a Raspberry Pi (Text Editor)


Vim is a well-known text editor on Linux, that can be used on a Raspberry Pi (instead of Nano, for example). I was used to it before I started finding it too buggy, and then it was removed on recent Debian-like distributions. But it’s still a decent alternative to other free text editors, and today I’ll show you how to install it on your Raspberry Pi.

Vim is no longer installed by default on Raspberry Pi OS and most Debian-based distributions. It can be installed with the graphical package manager or apt directly: sudo apt install vim.

In this tutorial, I will start with a brief introduction about Vim for those who don’t know it really well and jump to installation. At the end of the article, I’ll give you a few tips to get started if you are not familiar with it.

If you are looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge, where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Introduction to Vim

Grab my cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!

I already gave you most of the information, so I won’t repeat it here, I’ll just add a bit of context.

Why is Vim no longer on Pi?

I just want to start with a quick reminder about Vim/Vi on Raspberry Pi and on Linux systems in general.
This text editor was installed by default on almost every Linux operating system. So, I learned with it and had rarely used nano before using Raspberry Pi (and most of you are in the same case if I understand your feedback correctly).

I started having some issues on Debian 9 (I think) when Vim would randomly add a “g” at the beginning of the file. There is a workaround, but it started to annoy me every time I installed a new server.
Then in Debian 10 and most other Linux operating systems, vim was removed, replaced by nano. It was a shock for me and many users.

Raspberry Pi Bootcamp
Sale: 10% off today.
Take it to the next level.
I'm here to help you get started on Raspberry Pi.
Learn all the skills you need in the correct order.

Even if the reason for developers choosing to replace it is unclear, the most probable reason is that it’s too complicated for beginners.
The Linux community does many things to attract new users, and if they can’t edit and save a file in a terminal, it’s a major downside to keeping them on that system.

Vim vs Vi

Personally, I never used Vi as I don’t like the default behavior.
But it’s probably also a habit issue, and I know that some of you may prefer to use it.
Just to address this question first, Vi is also available on Raspberry Pi, if you really want to use it.

By the way, Vim is a fork of Vi, so there are many common points between both.
The official Vim website explains the major improvement compared to vi:

  • Syntax highlighting was not available on Vi, it’s included on Vim.
  • Vi is only available on Unix systems, Vim added the support to many other systems.
  • You have access to unlimited undo to cancel any error you made. On Vi, you can restore only the last change.
  • Etc.

Finally, you have to know that it’s possible to configure Vim to act like Vi if it’s only a habit problem.

Vim vs Nano

There are really no common points between Vim and Nano. Yes, you can do almost the same thing with both, but the interface is absolutely different.
Nano may be easier to start with because you can edit directly as on the edit visual text editor, and the shortcuts are listed at the bottom of the page.

Apart from that, I won’t go into more detail on this comparison. If you are here, you probably prefer Vim over Nano. There are pros and cons on both sides, so test them and choose one.

VimNano
Mode based editor (Navigation, Insert, Command)Modeless (WYSIWYG)
Difficult to master, even for advanced usersEasy to use for beginners
Powerful with complex editsLimited features
Differences between Vim and Nano

Nano is now here by default, even if not perfect it’s probably a bit easier to understand. And it’s probably a good idea to learn to use it, but not mandatory (for now).
If you are also lost in it, you can read my complete tutorial about Nano here.

Install Vim on Raspberry Pi OS

We’ll see here how to install it on Raspberry Pi OS, but it’s available on most operating systems (including Windows and macOS).
Some apps also exist on smartphones!

Vim installation command

Without further ado, the command to install Vim from a terminal is simply:
sudo apt install vim

Press Y or Enter to continue. After a few seconds, Vim is ready to use.

If you are on a desktop, you can find it in the “Add / Remove Program” tool and install it as usual.

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

Open a file

Master your Raspberry Pi in 30 days
Sale: 10% off today.
Download the eBook.
Uncover the secrets of the Raspberry Pi in a 30 days challenge.
Learn useful Linux skills and practice multiples projects.

Once installed, the syntax to open a file is:
vim <filename>
vim <path>/<filename>

Don’t forget to use sudo to edit a file if you don’t have permission.

For example:
sudo vim /var/log/syslog

As you can see, the syntax highlighting is enabled directly, so the file is easy to read.

Small fix

As you can see on the previous screenshot, by default Vim will replace the first letter of the file with a “g”.
It’s not a big deal if you only read log files, but when you save a configuration file, it will save it too and break the service.
This was my first problem with Vim.

I had a configuration file to fix this problem, but it doesn’t work on the latest versions.
The only fix I find is to:

  • Create a .vimrc file with your preferences (with nano…):
    nano ~/.vimrc
  • Paste these lines:
    set term=builtin_ansi
    set mouse=r
    syntax on
    set background=dark
  • Save and exit (CTRL+O, CTRL+X).

Then reboot your Pi or start a new SSH session.
It should be ok!

I don’t understand why they try to destroy Vim this way, but I really understand why most operating systems don’t include it anymore.
If someone knows what the reason is behind this, I would love to know (please leave a comment at the end).

Vim usage tips for beginners

In this part, I will give you a few shortcuts to know to use Vim correctly. It will be particularly useful if you are trying it for the first time.

Edition mode

On Vim, you can’t write directly in the file when you open it, there are several modes:

  • i – switch to insertion mode, where you can write what you type.
  • ESC – exit the insertion mode and switch to read-only mode, where you can use shortcuts and commands to do anything else.
  • A – jump to the end of a line and switch to edit mode.

There are other shortcuts, but they aren’t really useful when you start. My goal is to keep this as simple as possible.

Save and quit

Let’s jump to the basics to survive with vim. Once in a file, use these commands to save (and exit):

  • :w – Save the file without exiting.
  • :q – Quit (only works if you didn’t change anything).
  • :wq – Save and exit.
  • :q! – Exit without saving, even if you made changes.
  • :w !sudo tee % – Vim lets you open a file even if you don’t have the right to edit it, but you’ll get an error on save. This command allows you to save using sudo, without opening and editing the file again.

I didn’t know the last one, but it can be really useful, especially on Raspberry Pi when you change configuration files with pi.

Master Python on Raspberry Pi
Sale: 10% off today.
Get the eBook.
Do more with your Raspberry Pi, learn the useful concepts and take the shortcuts.
You miss half of the fun of using a Raspberry Pi if you don’t know anything about Python.

Shortcuts in this section are where most novices fail with vim, and probably why it’s no longer included on Debian.
So, if you are in this case, take the time to remember these commands, before reading more advanced tips.

Search and replace

Even if it’s a bit hidden, Vim also includes search and replace functions, like most other editors:

  • /string – search for a specific string in the file.
  • n – jump to the next occurrence of the string (after the previous command).
  • N – same thing as above but backward.
  • :%s/find/replace/g – Find and replace command.
  • :%s/find/replace/gc – Same thing with confirmation for each occurrence.

Copy, cut, and paste

Vim is not visual, but copy & paste are still possible:

  • yy – Copy the current line.
  • dd – Cut the current line (you can also use it to delete the line).
  • p – Paste the line at the cursor position.

Don’t forget to exit the insertion mode before using these shortcuts.
You can add a number before yy and dd to copy or cut several lines, for example:
5yy
This command copies the 5 lines after your cursor.

Going further

If you wonder if a specific shortcut exists, you will find many websites and cheat sheets available on the Internet.
For example, you can check this one on Devhints.

For information, you can find all these commands (and more) by running:
vimtutor
It explains everything you need to know about it (in your language).

Reminder: Remember that all my Patreon supporters get access to this website without ads, early access to my videos and much more. You can become part of this community for as little as $3 per month & get all the benefits immediately.

Conclusion

Get My Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

That’s it, you know how to install Vim on Raspberry Pi, fix the “bugs” and use the basic shortcuts.
Let me know in the comments if you have other questions about it.

And as always, please share this post on your favorite social network if you liked it!

Raspberry Pi Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and show your support, you can also join the Patreon community. I share behind-the-scenes content there and give you early access to my content. You’ll also get a shoutout when you join.
More details here.

Need help building something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.


This tutorial doesn't work anymore? Report the issue here, so that I can update it!

0 0 votes
Article Rating

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Recent Posts

Master your Raspberry Pi - 10% off