NERDBUDE

[ code. keyboards. terminals. cyber. ]

[HOWTO] [WHOAMI] [PODCAST] [FRANKENSWITCHES] [GERMAN]

VIM II



Basic Commands, Split-View, Tabs

Part 2 of "Learning Vim with the NERDBUDE".
How the basic structure and operation of Vim works I explained in PART I. The second part is about the NORMAL mode which is the base for everything (except text/code input). Last time I explained how to open a simple file, write text, save and close the file. This may be sufficient for simple notes or similar. But now Vim can be used much more complex. The basis for everything is the NORMAL mode. A short 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 on the one hand and on the other hand to control functions in Vim.

KEYS

In NORMAL mode there are two ways of using it. One is keys and key combinations and the other is commands. Key combinations can be simply pressed as usual and commands are usually started with ":" followed by the desired command. Now but enough of the theory finally 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 where we want to go, but this takes much more time than using keyboard shortcuts.
. There are also keys and key combinations to edit text. Basically it can be said that Vim commands and keyboard shortcuts follow the following two schemes:

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

Looks complicated at first, but once we look at an example it becomes more understandable:
2dd

The "2" defines the number, "d" stands for Delete and the second "d" instructs Vim to delete the complete line. Thus "2dd" deletes two lines from the file.
The keys and combinations that are often used (at least for me) are as follows:

x  - delete single character
dd - delete line
2dd - delete two lines
yy - copy a line
2yy  - copy two lines 
p  - paste 
ZZ  - save and quit 

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


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

With this basic text and code files can be edited conveniently and efficiently. Continue with the commands that Vim holds ready.

COMMANDS

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


:q - Quit
:wq - Save and quit
:q! - Quit without saving
:w - Save
:qa - closes all files
:qa! - close all files without save
:e /path/to/file - open file

With this we can close Vim and save files. Vim can of course not only open and edit one file. Vim can open multiple files. There are two ways to open files: Tabs or in Split-View.

SPLITS / TABS

. If you want to work in parallel on files that are related to each other, it is useful to see the files side by side. For this there are also split views in Vim. Horizontally split as well as vertically split.
To split a Vim instance, the following commands can be used:


:split - split horizontal
:vsplit - split vertical

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


STRG + W - next split window

Now we might not want to split only vertically or horizontally but have a colorful mix of both. Works of course also under Vim. For this it needs again key combinations.

HORIZONTAL TO VERTICAL
STRG + W
SHIFT + H



VERTICAL TO HORIZONTAL
STRG + W
SHIFT + K


"CTRL + W" activates in this case the Window Command, which serves to edit the window.
But if we do not need to edit files side by side, but tabs are sufficient in principle, of course, this also works.

TABS

Tabs should know everyone from his favorite Internet browser. Parallel open but not simultaneously visible files.
In Vim, tabs can be created with the following command:

:tabnew


This command creates a new tab without a name. However, it is also possible to specify at the start of Vim that several files are to be opened in tabs in a Vim instance. To do this, use the following command in the command line:
.
TERMINAL
vim -p file1.md file2.md


The option "-p" specifies that the following files (file1.md, file2.md) should be opened in tabs. After this command Vim opens with all specified files. But how to switch between the tabs? This is just as simple and with two key combinations
.
gt - Next Tab
gT - Previous Tab


Or we can jump directly to the 5th tab using the above scheme with the key combination:
.
5gt - to the 5th tab


There are two ways to close a tab. On the one hand the well-known ":q" or one uses the command ":tabclose".

PRAXIS

. With the basics to the 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 in the files. So we are getting closer to a full replacement for an IDE for example. As a conclusion as already with Part I this "written learning process" a small example:


vim -p file1.md file2.md - Open 2 files within tabs
2gt - to tab nr. 2
i - activate INSERT Mode
Hack the Planet - our Text
ESC - exit the INSERT Mode
yy - copy a line of text...
p - ... and paste it
:w - save
:q - quit







//EOF