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.
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.
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 autopep8Installation
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 = 1After 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-formatSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
