Introduction

As an experienced vim user, I really thought I knew all there was to know in vim. Sure, there are always improvements and optimizations to be made, but I thought that I already had all the tools I needed to be an efficient software developer. Vim had blown my mind with new tricks before, so I thought that there was nothing left that would surprise me in the same way.

I was wrong.

In this article, you will learn 7 exciting new things that vim can do for you.

Prerequisites

For this article, I expect that you have at least a beginner’s fluency with vim, which means that simple commands such as basic movement, inserting, and searching are already understood. If you aren’t at this level yet, you can open the terminal and run:

sudo apt-get install vim

Then, you can go through the vim tutorials by running:

vimtutor

1. Deleting without register overwrite: Copy, delete, paste workflow

By default, vim commands that delete text, such as “d”, “c”, and “x”, overwrite the register. This allows you to write “dd” to perform a cut, then write “p” to perform a paste. While this functionality can be useful, a missing functionality in vim is the ability to delete text WITHOUT overwriting the register.

For example, say you want to delete some text and replace it with the text currently in your register. The problem is that when you run the delete command, you lose the information in your register! This is a very common text editing scenario, so the manual use of other registers is not a good solution.

I found that the best way to fix this issue is the use the following in your .vimrc:

" Shortcut to use blackhole register by default
nnoremap d "_d
vnoremap d "_d
nnoremap D "_D
vnoremap D "_D
nnoremap c "_c
vnoremap c "_c
nnoremap C "_C
vnoremap C "_C
nnoremap x "_x
vnoremap x "_x
nnoremap X "_X
vnoremap X "_X
" Change <leader> to be comma
let mapleader = ","
let g:mapleader = ","
" Shortcut to use clipboard with <leader>
nnoremap <leader>d d
vnoremap <leader>d d
nnoremap <leader>D D
vnoremap <leader>D D
nnoremap <leader>c c
vnoremap <leader>c c
nnoremap <leader>C C
vnoremap <leader>C C
nnoremap <leader>x x
vnoremap <leader>x x
nnoremap <leader>X X
vnoremap <leader>X X

This makes the “d”, “c”, and “x” commands delete WITHOUT overwriting the register, and makes “,d”, “,c”, and “,x” commands delete WITH overwriting the register.

This can fundamentally change how you edit code, as it opens up a “copy, delete, paste” workflow.

Image for post

Figure 1: In this example, I want to create a new variable PUBLISH_RATE = 1.0 / MAIN_LOOP_PERIOD_SECONDS, copy the text PUBLISH_RATE, and then replace the argument in rospy.Rate() to PUBLISH_RATE.

2. Navigating with speed and accuracy

Everyone who learns how to use vim knows how to use “hjkl” for small navigation steps and “gg” and “G” to jump to the top and bottom of the file. While these are both very useful, we need the ability to find a balance between these fine and coarse movement methods. To increase your navigation speed and accuracy, use the following:

  • “SHIFT + L” (low) to jump the cursor to the bottom of the current screen without moving the screen
  • “SHIFT + M” (middle) to jump the cursor to the center of the current screen without moving the screen
  • “SHIFT + H” (high) to jump the cursor to the top of the current screen without moving the screen
  • “CTRL + U” (up) to move the cursor and screen half a page up
  • “CTRL + D” (down) to move the cursor and screen half a page down

#text-editor #vim #text #code #software-development

7 Surprising Vim Tricks That Will Save You Hours
1.15 GEEK