Fundamentals 3 min read

Instant Code Formatting in Vim with the vim-rt-format Plugin

This guide shows how to install and configure the vim-rt-format plugin so that Vim automatically formats the current line on pressing ENTER in INSERT mode, covering prerequisite Python3 support, required Python modules, and essential .vimrc settings.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Instant Code Formatting in Vim with the vim-rt-format Plugin

Overview

The vim-rt-format plugin provides real‑time code formatting in Vim. When you press ENTER while in INSERT mode, the current line is automatically reformatted according to the language‑specific formatter, eliminating the need to run manual commands such as gq or :Neoformat.

vim-rt-format demo
vim-rt-format demo

Prerequisites

vim-rt-format requires Vim compiled with +python3. Verify the feature with: :echo has('python3') If the command returns 1, your Vim supports Python 3. Otherwise, install a version that includes the feature, for example on Ubuntu/Debian: sudo apt-get install vim-nox The plugin also depends on the Python formatter autopep8. Install it system‑wide:

sudo pip install autopep8

Installation

Using vim-plug, add the following lines to your .vimrc:

Plug 'skywind3000/vim-rt-format'

" Format on ENTER in INSERT mode (default)
let g:rtf_ctrl_enter = 0   " Set to 1 to use CTRL+ENTER instead

" Also format when leaving INSERT mode
let g:rtf_on_insert_leave = 1

After updating the plugin list ( :PlugInstall) restart Vim.

Configuration and Usage

Enable the plugin for the current buffer: :RTFormatEnable Now, while editing a file, press ENTER in INSERT mode and the line will be reformatted instantly. The same behaviour can be triggered when exiting INSERT mode if g:rtf_on_insert_leave is set to 1.

Supported Languages

Python

Lua

Ruby

JavaScript

Reference

GitHub repository:

https://github.com/skywind3000/vim-rt-format
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pluginVimCode Formattingpython3autopep8vim-rt-format
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.