Neovim

:normal command

Automating repetitive edits in Neovim with the :normal command.

April 01, 2025

The :normal (short: :norm) command can be used to execute normal commands on a given range of lines.

For instance this command :%norm I// can be used to comment out every single line in the given file.

Tipp

Control characters like "esc" or "enter" can be used by pressing Ctrl-V (on windows ctrl-q) followed by the key (esc, enter, ...).

Here is a more advanced example to transform code, that you can find in a typical angular constructor, into the structure required for unit testing.

'<,'>norm df: aprovideAutoSpy(^[ea)
'<,'>Shows that we are working on a subset of lines norm df:Switch to normal mode and delete everything until ":" aprovideAutoSpy(Go into insert mode and write "provideAutoSpy(" ^[This will be displayed for ctrl-v followed by esc ea)Go to the end of the word, switch to insert mode and add ")"
describe('AppComponent', () => {
  let component: AppComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        private _artifactService: ArtifactService,
        private _environmentService: EnvironmentService,
        private _i18nextPipe: I18NextPipe,
        private _loadingService: LoadingService,
        private _messageService: MessageService,
        private _router: Router,
        private _tabBarService: TabBarService,
        private _templateService: TemplateService
      ],
    });

    component = TestBed.inject(AppComponent);
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});
describe('AppComponent', () => {
  let component: AppComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        provideAutoSpy(ArtifactService),
        provideAutoSpy(EnvironmentService),
        provideAutoSpy(I18NextPipe),
        provideAutoSpy(LoadingService),
        provideAutoSpy(MessageService),
        provideAutoSpy(Router),
        provideAutoSpy(TabBarService),
        provideAutoSpy(TemplateService)
      ],
    });

    component = TestBed.inject(AppComponent);
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

This is the actual result. In the first image, we can see the code from an angular constructor pasted into the providers array. In the second image, we see the result of the command.

Share