[~] BACK



~~ VIM (II) ~~

** BASICS, SPLITS, TABS **

[VIM] | [20211206] | [0] | [0x17]




Part 2 of "Learn Vim with the NERDBUDE".
I explained how the basic structure and operation of Vim works in PART I. The second part is about NORMAL mode, which is basically the basis for everything (except text/code entry). Last time I explained how we open a simple file, write text, save and close the file again. That may be sufficient for simple notes or similar. But now Vim can also be used in a much more complex way. NORMAL mode serves as the basis for everything. Let's take a quick step back. NORMAL mode is reached by pressing "ESC". The status line then shows NORMAL.
Here, various commands and key combinations can be used to edit text or code and to control functions in Vim.

KEYS

There are two ways of using NORMAL mode. Firstly, keys and key combinations and secondly, commands. Key combinations can simply be pressed as usual and the commands usually start with ":" followed by the desired command. But enough of the theory now, after all we want to use Vim and not just read about it.

Let's start with the basic keys and key combinations that make navigating the cursor in Vim much easier. We can of course use HJKL until we get to the place we want, but that takes a lot more time than with key combinations.
There are also keys and key combinations for editing text. Basically, it can be said that Vim commands and key combinations follow the following two patterns:

[command][number]text object
[number][command]text object

It looks complicated at first, but once we look at an example it becomes clearer:

2dd

The "2" defines the number, "d" stands for delete and the second "d" tells Vim to delete the entire line. "2dd" therefore deletes two lines from the file.
The keys and combinations that are frequently used (at least by me) are the following:

x - deletes a single character under the cursor
dd - deletes an entire line
2dd - deletes two lines
yy - copies an entire line
2yy - copies 2 lines 
p - paste 
ZZ - save and exit 

To facilitate cursor navigation, the following keys can be used:


b / w - previous / next word
ge / e - previous / next end of word
G - Go to last line
gg - Go to first line
3G - Go to line "3" 

This allows you to edit basic text and code files conveniently and efficiently. Next up are the commands that Vim has to offer.

COMMANDS

In addition to the keys or key combinations, Vim also has commands that can be used. Commands are also entered in NORMAL mode and usually start with a ":".
The basic commands are as follows:


:q - Close
:wq - Save and close
:q! - Force close without saving
:w - Save
:qa - Close all files
:qa! - Close all files without saving
:e /path/to/file - Open file

We can now close Vim and save files. Vim can of course not only open and edit one file. Vim can of course open multiple files. There are two ways to open files: tabs or in split view.

SPLITS / TABS

If you want to work on files that are related to each other in parallel, it is a good idea to see the files side by side. Vim also has split views for this. Split horizontally as well as split vertically.
The following commands can be used to split a Vim instance:


:split - splits horizontally
:vsplit - splits vertically

Vim is now split and we can use ":e" to open and edit files in the individual splits.
Since Vim runs in the terminal, the question now arises as to how to switch between the splits.


CTRL + W - switches to the next split window

Now we might not only want to split vertically or horizontally, but have a colorful mix of both. This also works under Vim, of course. This requires key combinations again.

HORIZONTAL TO VERTICAL
CTRL + W
SHIFT + H


VERTICAL TO HORIZONTAL
CTRL + W
SHIFT + K


"CTRL + W" activates the Window Command in this case, which is used to edit the window.
But if we If you don't have to edit files side by side, but tabs are basically sufficient, then this is of course also possible.

TABS

Everyone should be familiar with tabs from their favorite internet browser. Files that are opened in parallel but not visible at the same time.
In Vim, tabs can be created with the following command:

:tabnew


This command creates a new tab without a name. However, you can also specify when starting Vim that several files should be opened in tabs in one Vim instance. To do this, use the following command in the command line:

TERMINAL
vim -p file1.md file2.md


The "-p" option specifies that the following files (file1.md, file2.md) should be opened in tabs.
After this command, Vim opens with all the specified files. But how do you switch between the tabs? It's just as easy, using two key combinations

gt - Next Tab
gT - Previous Tab


Or we can jump directly to the 5th tab using the key combination as described above:

5gt - Jumps to the 5th tab


There are two ways to close a tab. One is the well-known ":q" or you can use the command ":tabclose".

PRACTICE

With the basics of NORMAL mode, we are now able to use Vim like a modern editor with tabs, split windows, we can open, close and save files and also move around in the files. So we are getting closer to a full-fledged replacement for an IDE, for example. To conclude, as with Part I of this "written learning process", a small example:


vim -p file1.md file2.md - we open 2 files in tabs
2gt - we go to tab no. 2
i - we activate the INSERT mode
Hack the Planet - our text
ESC - exit the INSERT mode
yy - we copy the line of text...
p - ... and paste it again
:w - save the whole thing
:q - and close Vim

[~] BACK