Vim n.n.n.n.n.n.


reading time 3 min

What does “n.n.n.n.” do in Vim?

n repeats the last search.

. repeats the last change.

So, let’s say we have this file and we want to extract out just the names.

1
2
3
4
5
6
7
  "ACTIVEDIRECTORY_PS": {
  "AXFRDDNS": {
  "AZURE_DNS": {
  "BIND": {
  "CLOUDFLAREAPI": {
  "CLOUDNS": {
  "HEXONET": {

We are going to move the cursor to the first : by typing /:<RETURN>, then press D to delete to the end of line. Then we want to repeat that over and over.

To repeat those two steps we could type /:<RETURN> again, then D again, 6 more times. However, we could use n to repeat the search and . to repeat the change. That is, we type n. over and over until the task is done. i.e. n.n.n.n.n.n.

As a result we end up with this list cleaned up as

1
2
3
4
5
6
7
  "ACTIVEDIRECTORY_PS"
  "AXFRDDNS"
  "AZURE_DNS"
  "BIND"
  "CLOUDFLAREAPI"
  "CLOUDNS"
  "HEXONET"

There are other ways to do it, but pressing n and . and n and . is kind of fun.

We might also want to remove the whitespace at the front of each line. In that case we do something similar. We move to the first line and type 0 to get to the first column (the 0th column), then 2x to delete the 2 chars (the 2 whitespaces).

FYI: You can prefix most vim commands with a number to repeat it that many times. In this case 2x will do x (delete the char under the cursor) twice.

Now we want to repeat this sequence of j2x. We can type j to move to the next line, then . to repeat the change (delete 2 chars).

The result should look like this:

1
2
3
4
5
6
7
"ACTIVEDIRECTORY_PS"
"AXFRDDNS"
"AZURE_DNS"
"BIND"
"CLOUDFLAREAPI"
"CLOUDNS"
"HEXONET"

In this case we didn’t use n because j is a perfectly fine way to get to the next line.

Instead of . you could have typed x twice. However I find it useful to get into a rhythm of “move dot move do move dot”. Whether the move is n or j, if you do this often enough you’ll develop muscle memory and it will become second nature.

Two keystrokes (j.) is a 33% savings over three (j2x) but most importantly it is less brainwork because it ends in . which is always in the same place on the keyboard.

As an exercise, try removing the double-quotes from each line. There are multiple ways to do it. Try using some combination of a move to the next double-quote and . to repeat the change of moving it.




Tom Limoncelli

Tom Limoncelli

Recent Posts


  1. HBO’s Integration Test Email
  2. Vim n.n.n.n.n.n.
  3. Commonly misused business metaphors
  4. VMware WorkspaceONE Notifications are a crime against productivity
  5. Numberwang (all videos)

Archive


Categories


Tags


I agree that this website may store my data to personalize my journey in accordance with their Terms & conditions

Powered by Hugo | Theme - YesThatTheme © 2017 - 2021 Tom Limoncelli