قالب وردپرس درنا توس
Home / Tips and Tricks / Define a great Vim profile with .vimrc – CloudSavvy IT

Define a great Vim profile with .vimrc – CloudSavvy IT



Vim is an advanced text editor for Linux and Unix operating systems. Recently rated the # 1 worldwide Linux editor, Vim is open-source and free. This article explains how to create a great .vimrc profile.

What’s Vim

First, if you are new to Vim: welcome! You have just embarked on the amazing journey of getting to know one of the world̵

7;s best and most useful editors.

Bram Moolenaar created Vim, the beloved Linux terminal-based editor, in 1991. Vim stands for we have improved, and vi was the classic old-school text editor for Unix, developed in 1976. Vim’s popularity has skyrocketed, and the editor was recently named the # 1 worldwide Linux editor by a major Linux forum. Vim is also generally preinstalled as the default editor in many Linux distributions.

Vim allows users to edit files in two sets of modes, the Edit mode and the order mode, if you like, which probably makes navigation in Vim a challenge for new users. Users are probably used to being in edit mode in most other popular text editors (OpenOffice writer etc), but not command mode. And command mode is the default mode in which Vim will start.

For example, when you open a file with Vim (with a command on your terminal prompt such as vi my_story.txt), enter your Vim in command mode. You can now type the letter i (a vi command) to enter edit (or specifically in this case insert) mode (-- INSERT -- usually appears on the last line of the terminal to highlight the mode you are in) and start typing your test. When you’re done, press ESC (the escape key) to return to command mode.

You can then go back to edit mode by, for example, binding a (another vi command) to return to edit mode (a stands for in this case to add and the cursor jumps one character to the right when you enter edit mode with a instead of iWhen you are done editing (which provides full editing capabilities, such as on cursor up/down cursor left/right etc.) you can press the ESC key to return to command mode.

When you are done editing the file, make sure you are in command mode (press the ESC key again to make sure) and type the key sequence :wq!This will write (w) the file and exit (qIf you’ve never used Vim before, well done! This was your first Vim session 😉

The Vim editor is one of the few editors with such a sharp initial learning curve. However, after the initial learning curve, knowledge of editor commands and usage continues to grow with a person over time in a more or less linear format. I write all my scripts, articles and data crackers in Vim and only use it in conjunction with AutoKey to easily insert my template and HTML tags where needed.

If you want to learn more about AutoKey, read our AutoKey: How to Automatically Replace Characters with Predefined Text in Linux article.

Defining a great .vimrc profile

If you’ve used Vim in various Linux distributions before, you may have noticed how some distributions seem to fail when setting up a correct Vim profile, leading to somewhat strange behavior when using Vim.

One of the things you may see (especially on Mint 19 and some older Ubuntu versions) is that when you press cursor right you have to press another key for the cursor to actually jump, especially around highlighted / colored syntax (in shell scripts for example). Another quirk is the strange behavior of mouse clicks or when using tabs.

If you see any of these or similar quirks, it’s really time to set up a great default .vimrc profile. The .vimrc file / profile contains all the settings that Vim will read and use every time it starts. Over the years of using Vim, and especially around the time I registered a bug for one of these odd behavior items, I created a short .vimrc profile that nullifies these issues.

The .vimrc file will and should be stored in your home directory. Let’s create the file with vim self. Note that you can generally use vi to start vimFirst we open it .vimrc file in our home directory (which in Linux is represented by the tildeie ~, symbol) by running:

vi ~/.vimrc

At the command line. This would be the Vim editor. Now press the i key to enter edit / insert mode. You see one -- INSERT -- on the last line of your terminal (displayed in most cases) you will know that you are in insert mode. Then copy and paste the text block below and right click your mouse in the terminal window where Vim is running. Then click To stick

set nocompatible
colo torte
syntax on
set tabstop     =2
set softtabstop =2
set shiftwidth  =2
set expandtab

You should now see a screen similar to the following:

A solid base .vimrc profile template

If you generally use four spaces instead of two (for your coding work), just replace them all =2 by =4

Now press ESC to exit edit mode, then type the key sequence seen earlier: :wq!This will write the file ~/.vimrc and leave it Vim editor. If you want to verify the contents of the file, you can run cat ~/.vimrc on the command line.

Next time you enter Vim, all these settings are automatically loaded by Vim from your ~/.vimrc File.

In addition to setting a nice color scheme using the command colo torte, we also enable syntax highlighting with syntax onWe’ve set up some better options for handling the width of tabs (generally we indicate that we want two or four spaces as tabs described above).

Keep in mind that thetorte color scheme does not appear to be available on Linux Mint 20.1, although it is available on some previous Mint releases. If this applies to your system, you will receive a message to the same effect. Just hit enter and edit~/.vimrc to delete this line.

The set expandtab option specifically ensures that when we press tab instead it is automatically translated to spaces. You can delete this line if you don’t like it (tip: use Vim to do this and you can press dd on a line, while in edit mode, to delete the whole line, even without entering edit mode).

However, the most important option is the one on the first line. The set nocompatible ensures that a number of things – as described above – work much better and without unclear behavior.

There is some debate online about the perceived need not to set this option, as this setting is supposed to be automatically enabled once a .vimrc file is used, but this setting ensures that this setting is active and that there are no negative update to set it up [again]In other words, there is a potential benefit to defining it hard coded / static while there is no downside to it.

Shut down

This article placed Vim and Vi in a historical context and described their popularity. After this we gave a little introduction to the editing and command modes in Vim and looked at creating a ~/.vimrc settings file for Vim that will ensure a much improved Vim user experience by setting tab widths, space settings, syntax highlighting, a color scheme and lastly and most importantly avoiding buggy / unclear Vim behavior on various Linux distributions.

To enjoy!


Source link