Neovim for Beginners — Refactoring

Code refactoring using Neovim.

alpha2phi
7 min readApr 23, 2022

--

Neovim for Beginners — Refactoring

Code refactoring is one key feature of using an IDE. Natively Vim as a text editor does not have any built-in support for code refactoring.

With Neovim we start to have the built-in LSP support that gives us basic code refactoring features like find-references, rename, code actions, etc., using semantic whole-project analysis. In this article, we are going to

  • Use Neovim to search and replace a pattern in multiple buffers.
  • Explore plugins to help in search and replace.
  • Explore LSP code refactoring features.
  • Explore plugins for code refactoring.

This article is part of the Neovim for Beginners series.

The Neovim configuration files can be found in this repository.

Search and Replace in Multiple Buffers

There are multiple ways to search and replace a pattern in multiple buffers. We can use the commands listed below to search and replace a pattern in multiple buffers.

  • :argdo — Execute the command for each file in the argument list
  • :bufdo — Execute the command in each buffer in the buffer list
  • :tabdo — Execute the command in each tab page
  • :windo — Execute the command in each window
  • :cdo — Execute the command in each valid entry in the quickfix list
  • :cfdo — Execute the command in each file in the quickfix list

We will use a simple Python project for demonstration purposes. The project layout is shown below.

Scenario 1 — Use Argument List

Let’s start with the :argdo command.

Add files to the Argument List

  • :args **/*.py — Add all the python files in the current folder and below to the arguments list. We can use :argadd to add additional files.
  • Now use :args to list all the files in the argument list. Alternatively, we can use :sall or :vert sall to view the files in splits.

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.