how to use vim on a raspberry pi

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 used to use it before I started to find it too buggy. 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’re 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

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.

Previously, Vim was the default text editor 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.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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.

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

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.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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

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 /etc/hosts

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

Small fix

Depending on your version and system configuration, Vim may 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 have found 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 in the community).

Update : On Raspberry Pi OS Bookworm, it seems to work fine by default, so no need to worry about that.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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.

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).

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

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

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Conclusion

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 community if you have other questions about it.

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

Whenever you’re ready, here are other ways I can help you:

The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.

Master your Raspberry Pi in 30 days: 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.

The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

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

Similar Posts