Vim, the workflow changer! and its config file

DTS Solutions
6 min readDec 21, 2020

--

In the DTS blog we have talked about solutions and how to get them work, the last time we had the opportunity to share a Serverless Environment set up to build and deploy serverless applications. But what about the coding part itself? Applications need to be written with a tool that let the programmer write some code, that’s called a text editor!

That’s why, this time, we would like to tell you a little about the coding workflow we use!

When we are introduced to programming, normally they introduce us a piece of software called Integrated Development Environment (IDE). That is, a software with all the necessary tools to write applications in a certain programming language or languages, but sometimes it has more tools than the ones we actually need or use.Vim, the workflow changer! and its config file

Then we get to know a little less bloated tools like Visual Studio Code, which is less than an IDE, but still a suitable tool to write applications with a variety of programming languages while being a lot more lightweight.

But here at DTS Solutions, some of our programmers like to go a step further, and they love to use Vim, a powerful and extensible, terminal based text editor that can improve your coding workflow making you faster at typing and moving between lines and files.

Introducing Vim

Vim is a widely-known text editor for two reasons:

  • Powerful features for editing text and navigate between files with only a few keystrokes, you never touch the mouse!
  • Be hard to adapt at the beginning due to its learning curve

Despite the second reason above, once you are at least comfortable with it, there is no looking back and you’ll start to fly! That may require a little bit of time to get used to it depending on how much time you practice with it, but it pays well.

There are a ton of tutorials on the internet about how to start learning vim, they give you the fundamentals on how you can get started, and how to start to practice, it’s pretty the same for every user that wants to learn vim.

On the other hand, there is a little detailed information on how to customize vim and make useful changes to its config file that you can take advantage of. That’s why we want to focus on those users that are already using vim and share them some lines of configuration they could find useful and may not be using right now. So lets get started!

The vimrc file

When you run vim, it searches for personalized configuration located in a file called vimrc, which is hidden in your home directory (/home/user/.vimrc) by default in UNIX-based systems. It will contain all the commands that you want to execute every time you start a vim instance.

By default vim has all the necessary stuff to work out of the box, but as it’s a very special kind of text editor, you’ll probably want to customize some of its default behaviour, so right ahead we are introducing some useful set of commands that will surely improve your vim experience:

set nocompatible

This line is seen in almost every vimrc file. It prevents compatibility with Vi, an older text editor which Vim was born from, but we don’t want to use the Vi features, we want to focus on the new features Vim came with, without having to worry about compatibility.

set history=1000

You can put any number, the default is 20. It tells to vim to remember the last 1000 commands you entered in that vim instance. Maybe you copied a command and don’t remember which one is, you can go back in history until you find it.

set noswapfile
set nobackup

When you are editing a file, and exit vim without saving those changes, a swapfile is created so you can recover those changes the next time you edit that file, this also prevents you of editing the same file within two vim instances. While you may find this useful or not, a lot of new vim users are annoyed by this behaviour and don’t want to know about swap files and backup files which behave in a similar way.

set autoindent

When inserting a new line, it detects the current indentation level and puts you in the right place of indentation.

syntax on

Enable syntax highlighting for different kinds of file extensions. This is a core requirement in every text editor.

filetype plugin indent on

This is very common in every vimrc file, it allows us to use different plugins to extend the features of vim, we’ll talk about plugins on a next blog.

set undofile
set undodir=/home/user/.vim/undodir

Undo history normally persists within a vim instance, once you exit that instance and re-open the file to edit it you can’t undo to changes made in a previous session. With this command you allow vim to remember the undo history in the specified dir, with this you can always go back to a previous editing state in a file.

set number

Sets the line number for every line in a file.

set relativenumber

While the line you are positioned at is highlighted with the line number, the rest of the lines are numbered relatively to the current one, this allows you to quickly identify the number you have to put in combination with a keystroke to reach a certain line. For example if you wanted to copy up to four lines below the current line you just type 4yj, but sometimes it’s not that easy to know how many lines below or above you want to copy and this feature helps you to know it.

set cursorline
set cursorcolumn

Highlights the line and column where the cursor is positioned, this is just visual feedback and you may find it useful or distracting.

set nowrap

When the window is not wide enough, lines tend to wrap and when we are editing code we could find this annoying since we are not editing normal text but code! we would probably want to scroll horizontally instead.

set splitbelow
set splitright

The split window feature of vim by default splits the windows to the left when vertically and above when horizontally, we could find ourselves looking to the wrong side with this behaviour when splitting a window. With this command we set the splits to be to the right and below when splitting vertically and horizontally respectively.

set hidden

When you have more than one buffer open, you want to switch between those buffers to edit the files or just to take a look at them, and by default vim doesn’t let you do this without saving before trying to switch between buffers. This line allows you to do it without the need of saving changes.

set wildmenu

An enhanced menu shown when an autocompletion is triggered while writing a command.

map <C-K> :bnext<CR>
map <C-J> :bprev<CR>

This are not configuration commands but useful mappings for switching buffers, Ctrl+J to go to the previous buffer and Ctrl+K to go to the next one, you can adjust it to the keys you are most comfortable with.

And as we said, these are just a few lines of config you can find useful to improve your vim experience. As you may have noticed, we didn’t put any config related to number of tabs or spaces for indentation, for this we consider better to use a plugin like editorconfig to make indentation consistent between programmers working in a same project. We’ll be talking about plugins in a next opportunity, by the way!

What are your thoughts on Vim? Do you think it’s worth learning? We do!

We’ll see you next time, happy coding with Vim!

--

--