How I use Vim as DevOps Engineer

Written by Georgi Stefkoff

Vim for DevOps?

Of course vim in not one of the requirements in order to be good DevOps, but I choose it over nano, emacs or any other terminal editors. Why? Read below to see its power.

Why it is better than others?

It is not better than others, because in one way or another, someone could finds nano (for example) as a best editor for him/her. In my experience, I've started with vim and almost everything I did was with vim.

If you prefer some other terminal editor, probably this blog post is not for you, or you can stay and see how I use it.

Here are some key features that I got used to vim and I think it is the best one:

  • a lot of short codes that you can use in order to speed-up the process of writing/editing files
  • very customisable software that you can find plugins for almost everything. You can even write your own.
  • ability to split your screen by multiple layouts and edit multiple files at one screen, without need to open and close every time
  • as the second point, it is customisable and it can be configured per user, so one of the users of the system could use one key mapping, but another user can use the default one, without overlapping or even knowing of the existing of each other.
  • built-in terminal, so you can execute some code, while editing the file, without the need to close and re-open the file
  • many, many more

I just can't remember what I like in vim, because there are a lot things in it. Even there is a huge database with plugins that the boring terminal editor could become as a fully-featured IDE with auto-complete and AI assistance.

In this blog post, I will not cover how you should install vim or configuring any plugins. Probably, your Linux distributing (or even UNIX) has it already.

The following sections describes the most used features by me, in order to speed-up the process of writing/editing files.

How to open?

Opening a file for editing with vim is simple as follows:

vim /path/to/file.ext

With this command, you will open the file in Normal mode. You can also, open the entire folder:

vim /path/to/folder

in this way, vim will list you the files inside this folder so you can hit Enter on the particular file that you want to edit.

Creating the files is the same as editing:

vim new_file_name.ext

With this command, once you made the modifications, vim will save the file with the name that you provided (new_file_name.ext in my case).

Normal mode?

Vim has three modes: Normal, Inserting ,Visual and Command mode

  1. Normal mode: Normal mode is the default one, or once you hit Esc key. In this mode, you can navigate throw the content, but once you click some key on the keyboard, probably you will hit some command.
  2. Inserting mode - this mode is where you will want to insert you content. You enter in this mode by hitting i key or Insert. Insert mode has its opposite Replace mode. The difference between them is that in Insert mode, you are adding a character before the cursor, but in Replace mode, you are changing the character that the cursor is over it. Note: Depending on the shortcuts that you are using, you may enter in Insert mode also
  3. Visual Model - this mode is used for visual selection of the content so you can copy, cut, replace and any other operations over the visually selected code. You will enter in this mode by pressing v button.
  4. Command mode - this is where the magic happen. Command mode in vim allows you to enter specific commands that can do various things (like exiting). In command mode also, saving is happening. You enter in command mode by pressing : (colon). Once you there, you have to write the command and press Enter. You can exit from the Command mode, by pressing Esc key

How to exit?

This is probably the first question of a beginner that is using vim. It is really simple and you have several ways:

  1. Quit from not modified files: You have to enter in Command mode and the writer q and hit Enter:

:q<Enter>

In this way, you will exit from vim and close the program. Note that, I've mentioned "not modified files". This is intentionally, because if the file is modified, after it has been opened, vim will deny the closing, without saving or discarding the changes.

  1. Discard the changes: you can exit and discard all of the changes by writing q! in the Command mode

:q!<Enter>

In this way, vim will discard all changes and will close the program.

Extra You can close all opened instances by adding a after q command, like:

:qa!<Etner>

Will discard all modified opened files and exit.

  1. Save the file and exit: You can save you file and exit from vim by writing w after the q command, like:

:wq<Enter>

In this way, vim will save everything and will exit.

Extra You can use w command just to save your progress, like :w<Enter> and still stay with the opened file. After saving, you will enter in Normal mode

Enough boring stuff for now. I will make the entire tutorial for vim. You can read more about this in details, if you enter the following command: :h<Enter>

Editing multiple files?

Yes. You can edit multiple files in one screen in vim. My favourite command for this is vertical split. In vertical split, you can open the same file, or any other file and the screen will be split in vertical (you will have two editors).

:vs|vsplit [file]<Enter>

You can use both vs or vsplit in order to split the screen. If you do not provide a file name as an argument, you will split the current file in two. You can also use :sp|split [file]<Enter> in order to split the file (or files) horizontally.

Once you split the file (or open another one), your cursor will be active on the opened window. This means that, all of the navigations, commands and so on will works on that window and not in the previous one. In order to navigate between the opened windows, you can use the key combination of Ctl+w - [arrow-keys] (pressing Ctl+w, releasing and then pressing the arrow keys to navigate between the windows.

Built-in terminal

Imaging that you are wring a C program or in any other language that requires compilation, before executing. After each modifications that you made, you may need to check the compilations and fix the errors (if any). If you need to close and open the file, again and again, you will be pissed very soon. Adding second monitor is not the correct way.

Vim has a great feature to execute a commands. This is done by entering in Command mode, then writing ! followed by the command. Here is an example of listing the content of the current working directory:

:!ls -la<Enter>

After hitting Enter you will see a complete different screen (all active windows will be closed and you will see the output of the entered command. You will be prompted to hit some key, in order to return to the editor.

I've use this feature maybe every time when I use vim. Like I said for compiling or debugging, finding some files that I need to edit, and many other commands that I can use it. Note that, I've use this feature for commands that are in one-line. This means that if I need to use series of commands in order to do something, most of the time, I exit from vim and to my job in the normal terminal.

Going to a specific line

If you need to go to a specific line in the file, is more than easy: :[line number]<Enter>. An example is: :55<Enter> which will jump to the 55th line.

Replacing single character

Sometimes, I have to open a file in order to change a single character (typo for example). In vim you do not have to enter in Ineset mode first, then changing the character and then exit from Insert mode. You can simply move the cursor over the target character, hit the r key and then hit the replaced character. In this way, you immediately atelly change a character, without moveing throw the different modes.

Replacing a word

Replacing a word is simple as replacing a character. You have to go to the first character of the word, hit cw. In this way, the entire word, starting from the cursor will be deleting and you enter in Insert mode. You can then write the correct word and hit Esc key to exit from Insert mode.

Replacing until start or end

If you need to change a peace of text from one point until the end of the line, you can hit c$. In this way, from where is the cursor, until the end of the line will be deleted and you will enter in Insert mode. If you need to edit it from the point to the begging, then you have to hit c0. In this way, everything that is between the cursor and the start of the line will be deleted and you will enter in Insert mode.

Navigating through the words

This is simple as you hit w key. Once pressed, the cursor will go to the next word at the first character. This is for moving forward word-by-word. If you need to move backward, you can press b key. Then the cursor will move back to the previous word at the first character.

From the last section it can be seen that, you can go to the end of the line by hitting $ and go to the begging of the line with 0.

Deleting a word

You can delete an word by hitting dw. In this way, starting from the cursor, until the end of the current word, everything will be deleted and you will enter in Insert mode. You can delete multiple words by dNw for example: :d2w<Enter> will delete the first two words from the cursor.

You can also delete everything until the start or the begging of the line by d0 or d$ accordingly.

Deleting a line

You do not have to use the previous approaches to delete a line. You can simple do a dd and the line will be removed.

Moving to the begging and end of the file

You can go to the very first character by hitting gg. In order to move the the end of the file, you have to press G. Note on the uppercase G. You have to actually press Shift+g key.

If you need to delete the entire content of the file, you can combine some commands that we met in order to do this:

  1. Go the the begging of the file - gg
  2. Press d
  3. Go to the end of the file: G

In this way, you will delete everything that is from the begging to the end of the file.

Copy-paste

You can have the copy-paste function with the commands and it can be active to all of the opened windows in vim, *Note that, this will not copy the content to the buffer of the machine, but just in vim memory. From every delete command that you made, it works like cut. This means that the content will stay in the buffer memory and you can paste it somewhere else, after deletion.

Copy

You can copy the content with the combination of y command. y commands copy the cuurect character at the cursor, but combined with other commands it can do more:

  • copy entire word: yw
  • copy entire line: yy
  • copy entire content: gg+y+G

Pasting

Pasting is done by just pressing the p key. As I said above, once you delete something, you can paste it anywhere (until you delete another it will stay in the buffer).

Note that, the pasting will occur at the cursor position

Undo

If you need to do some undo(s), you can hit u key. You can hit as many us you need.

Searching

Searching is one of the most important features for the file editors. You can search in vim by pressing / first and followed by the search pattern, like:

`/search text`

This will search for search text in the entire file.

If there are more than one search results, you can go to the next occurrence by hitting n key, or go back to the previous by hitting the N key.

and many, many more features, that I can't even remember remember

Conclusion

These are just a quick pull from my memory that I can remember while working with vim and one that I use most of the time. Of course there are many more like replacing, complex navigation, mouse behaviour, etc. If you learn this basic usage, you will not need more than that in order to do some magic in the cloud.

Comments

  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.