Blog about technologies

It was mentioned before that VIM is a great editor and some even use it as the main IDE for development in various languages.
I can’t say my eyes are red enough to get rid of my graphical IDEs in favor of terminal, but VIM is still a great tool to use in ssh sessions.
And actually it is much more powerful than you can expect from terminal editor. Hush, emacs people, with all my respect to you, I’m just saying, not declaring a holy war.

However VIM doesn’t reveal its secrets to someone who is not curious enough. There are lot of plain-text doc files and look, they didn’t even mention on their front page how to exit it!

So, as far as you learnt how to exit VIM, I’d like to introduce few cool features which are available for more than a decade and probably you haven’t heard about many of them.

Make it shine

Firstly, there are some old weird distros where you’re getting some weird symbols on the screen while trying to use arrows or <del> button. Are you facing such problems? Let’s fix this before going on.

:set nocompatible – Fix problems with arrows:set backspace=eol,start,indent – Allow backspace to work over the end of the line, start of INSERT or autoindent.:syntax on – Enables syntax highlighting for various file-formats:set number / set nonumber – Enable / disable line numbers

Copy and Paste

That could be obvious that copying and pasting today is done with cursor selection. But wait, you’re gonna need these commands in order to use features mentioned below conveniently. So just read this and keep in mind.v – switch to visual selection mode.y – yank the selected fragment, it is an equivalent to CTRL+C / CTRL+Insertp – paste the selected fragment, it is an equivalent to CTRL+V / Shift+Insertc – cut the selected fragment, it is an equivalent to CTRL+X / Shift+Delete

File selector

Need to open another file?e. – opens interactive file selector

Windows

This is my favorite feature of VIM. Forget about opening second SSH session to edit second file simultaneously. Use VIM’s windows and tabs, or even both if you wish.

Tabs

Vim is not limited to one screen only. Sometimes there is a need to keep a dozen of files open and quickly switch between them. So VIM supports tabs which in turn can have windows. Great!

Vim has various commands for managing tabs, however they are quite complicated to remember. So let’s try to stick with hotkeys:

CTRL + w T – move current window to a separate tabgt – switch to next tabgT – switch to previous tab[N]gt – switch to tab #[N]. So 4gt would switch to 4th tab.CTRL + w c – still works for closing tabs

:tabe command opens new empty tab

Comparing files

Now as you got familiar with windows, he-he you are not even close to it, you can use another great feature called diff. You remember the pain of comparing configs of different versions of program? Maybe you have seen some nice diff viewers in modern IDEs. Now let’s see the one VIM has.

Open two files in separate vim windows and enter command :diffthis, VIM will highlight the differences and would even follow your cursor in the opposite window.

You can quickly open diff viewer from terminal with vimdiff file1 file2 command.

Commands for managing diffs:

do – Get changes from other window into the current windowdp – Put the changes from current window into the other window.]c – Jump to the next change.[c – Jump to the previous change.za – toggle fold/unfoldzo – unfold block under the cursorzc – fold block under the cursor

Searching and replacing text

To search and replace(substitute) text in VIM you need to remember few commands:

/expr – Search for expression. Where expression can be just string or regular expression/ – Search for the next occurrence of previously entered expression.:s/search/replace/g – replace search with replace within current string:%s/search/replace/g – replace search with replace in the whole file:%s/search/replace/gc – replace search with replace in the whole file but ask for confirmation before each replacing:%s/sEaRch/replace/gi – replace search with replace in the whole file in case-insensitive mode

This article covers just a small part of VIM features. Check the VIM manual for the rest and make yourself comfortable with terminal.