1. Overview

Linux users need command-line text editors on a daily basis. Albeit many of us prefer Vim, Nano, Helix, GNU Emacs, and Tilde, by and large, Vim is one of the most popular and highly efficient command-line text editors of our time. It comes pre-installed on almost all Linux distributions these days.

In this tutorial, firstly, we’ll learn briefly about the modes of Vim and then dive into methods to add new blank lines while staying in normal mode.

2. Introduction to Modes of the Vim

We all have different needs, such as writing code or text lines using the Vim editor. In essence, Vim has various modes, such as Normal mode, Visual mode, Select mode, Insert mode, and Command-Line mode. As a matter of fact, each has its purpose, and users conveniently pick each based on the course of action.

To write lines inside a file using the vim editor, we’ll have to enter insert mode. From time to time, we may need to use other modes of vim editor. Generally, we use the Normal mode to run various file operations like reading, searching, and replacing. The Normal mode is also known as the Command mode.

Occasionally, we’d need to insert new lines without leaving normal mode. We can switch to insert mode, add a new line and return to normal mode. However, if there’s a way to add a new blank line without leaving normal mode, it’ll certainly save us some keystrokes and time overhead. Let’s go through some ways which achieve this purpose.

3. Using the Mapping of Keys

We can create a shortcut with a sequence of keys or commands called mapping. The mapping can be defined in the Vim configuration file called .vimrc, which will generally be located in our system’s home directory. In general, we should edit the .vimrc file with the mappings. Let’s look at some valuable mappings we can use.

3.1. Using the Keys O and O

This mapping enables us to insert a new line under the cursor by pressing the key ‘o’ and above the cursor by pressing the key capital ‘O’. This way, we don’t have to leave normal mode to insert new lines:

nnoremap o o<Esc>0"_D     
nnoremap O O<Esc>0"_D    

As a result, after inserting a new line, the cursor will be moved to the new line. However, there might be cases when we don’t want to move the cursor from the starting point after adding the new line. This can be achieved by the next method.

3.2. Adding a Line Without Changing the Cursor Position

This way, we can use the same keys, ‘o’ or ‘O’, to insert new lines and keep the cursor in the same position:

nnoremap <silent> zj o<Esc>k
nnoremap <silent> zk O<Esc>j

The next method achieves the same results with more efficacy. 

3.3. Appending Multiple Lines Without Moving the Cursor

This mapping helps us append multiple lines at a time by pressing keys ‘o’ and ‘O’ with consecutive ‘\’ (backslash) followed by a number. As a result, the exact number of lines will be appended as mentioned in the command without moving the cursor position:

nnoremap <silent> <leader>o :<C-u>call append(line("."),   repeat([""], v:count1))<CR>   
nnoremap <silent> <leader>O :<C-u>call append(line(".")-1, repeat([""], v:count1))<CR>   

3.4. Adding an Empty Line in Between

This mapping enables us to add an empty line between two lines without leaving the normal mode after:

noremap <silent> <s-space> :exe "normal a".nr2char(getchar())<CR>

We’ll have to press the key ‘Space’ followed by the key ‘Enter’. This can be done from the beginning of a line. If we’re at the end of a line, we’ll press the keys ‘shift’ and ‘space’ together, followed by the key ‘Enter’. This will add a new line next to it. 

Indeed, we can map any other key in place of ‘Enter’.**

3.5. Using the Key ‘Enter’

When we’re in Vim’s normal mode, we don’t use the key ‘Enter’ often*.* This method maps the key ‘Enter’ to insert a new blank line. On the other hand, this allows us to remove inserted lines with the use of the key capital ‘J’:

nnoremap <Enter> i<Enter><Esc>k$

This way makes exactly the opposite use of the key ‘J’, which we use to join two lines in Vim.

3.6. Using a Plugin

A Vim plugin is a template of the Vim configuration file .vimrc. Many Linux enthusiasts create open-source plugins, and here we’re going to use one of them:

function! s:BlankUp(count) abort
  put!=repeat(nr2char(10), a:count)
  ']+1
  silent! call repeat#set("\<Plug>unimpairedBlankUp", a:count)
endfunction
function! s:BlankDown(count) abort
  put =repeat(nr2char(10), a:count)
  '[-1
  silent! call repeat#set("\<Plug>unimpairedBlankDown", a:count)
endfunction
nnoremap <silent> <Plug>unimpairedBlankUp   :<C-U>call <SID>BlankUp(v:count1)<CR>
nnoremap <silent> <Plug>unimpairedBlankDown :<C-U>call <SID>BlankDown(v:count1)<CR>

The plugin vim-unimpaired  is an advanced way of adding a new line and keeping the cursor in the same place. Additionally, it uses another plugin called vim-repeat for achieving better results with the command ‘*.*‘.

It’s important to realize that all the above methods need modification in the configuration of the .vimrc file. With this in mind, let’s head over to the next section.

4. Simple and Effective Ways

Simplicity and convenience are the points often overlooked. Even though there’re multiple ways, not all are simple enough. Some need complex settings to be applicable. Given the simplicity, we’re going to see some of the most convenient ways to insert a new line in Vim without leaving the normal mode.

4.1. Using a Key Followed by the Count of Lines

In truth, one of the easiest ways to insert a new blank line without leaving normal mode would be to press the key ‘o’. Our cursor will be on the new blank line. Here onwards, we can add as many lines as we want by just pressing the key ‘.’ or the sequence of a number and the key ‘.'(dot). For instance, if we want to add 10 blank lines, we can use ’10.’.

4.2. Using the Default Options

With this method, we can press the key ‘yy’ (pressing ‘y’ twice) to yank a blank line and go on pasting it with the key ‘p’. Specifically, the key ‘p’ pastes below the cursor, and the capital ‘P’ pastes above the cursor.

4.3. Using the Register

We can save a blank line on a named vim register. To do that, we’ll go to a blank line and type ‘pyy’, which will yank the blank line and save it on the named register ‘p’. Moreover, this saved register value doesn’t change until we modify it.

Given these points, we can go anywhere pasting the value of the register by typing ‘pp’. It adds a new blank line. The second ‘p’ is for pasting, while the first ‘p’ stands for the register. Furthermore, we can use any key as a register.

5. Conclusion

To sum up, we’ve learned plenty of effective ways to insert a new line without leaving normal mode in Vim. There’s no single method that suits the need of all. Therefore, we’ve pondered over more than one.