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

how to use vim on a raspberry pi

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

How to use Vim on a Raspberry Pi?
Vim is a free text editor that can be used on a Raspberry Pi.
The package is available in the Raspberry Pi OS repository, and the installation is straightforward.
The usage is a bit complex for beginners, but it’s a powerful tool.

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

Vim presentation

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

Why is Vim not longer on Pi?

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

I start having issue on Debian 9 (I think), when Vim will randomly add a “g” at the beginning of the file. There is a workaround, but it starts to annoy me each time I install 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.

Even if the reason for Linux choosing to replace it is unclear, the most probable reason is probably 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 big problem to keep them.

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 is 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 is 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 because you can edit directly as on edit visual text editor, and the shortcuts are listed at the bottom of the page.

Apart from that, I will not get into more details on this comparison. If you are here you probably prefer Vim over Nano. There are pros and cons on both side, so test them and choose one 🙂

So, 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 for information it’s available on most operating systems (including Windows and macOS).
Some apps also exist on smartphone!

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 desktop, you can find it in the “Add / Remove Program” tool and install it as usual.

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 the 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 may see on the last 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 broke the service.
It was my first problem with Vim.

I had a configuration file to fix this problem, but it doesn’t work on the last 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 doesn’t include it anymore…
If someone knows what is the real reason for doing 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 try 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 command to do anything else.
  • A – jump to the end of a line and switch to edit mode.

There are other shortcuts, but not really useful when you start. My goal is to keep this simple (as much 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 work if you didn’t change anything).
  • :wq – Save and exit.
  • :q! – Exit without saving, even if you made changes.
  • :w !sudo tee % – Vim let you open a file even if you don’t have right to edit it, but you’ll get an error on save. This command allow 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 backwards/
  • :%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, example:
5yy
This command copy the 5 lines after your cursor

Going further

If you wonder if a specific shortcut exists, you will find many websites and cheat sheet available on 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)

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *