DEV Community

Linwei
Linwei

Posted on

4 1

Vim 2022: Format your code in real-time !!

There are plenty of code-formatting plugins, like auto-format and neoformat, for vim. But all of them require running a certain command or using :w to trigger formating.

Is it possible to see the immediate formatted code in the real-time ? If so, life could be much easier.

Therefore, I created a little plugin vim-rt-format, which can format the current line immediately in INSERT mode as soon as you press ENTER:

picture

When you are focusing on editing, everything is done without paying extra attention. No need to stop your work to run an extra command like gq or :Neoformat.

Ready to try this out ? Firstly, +python3 feature must be enabled in your vim, check it by:

:echo has('python3')
Enter fullscreen mode Exit fullscreen mode

It will return 1 if python is supported in your current vim version. If not, install a new vim with +python3 feature, for example in ubuntu/debian:

sudo apt-get install vim-nox
Enter fullscreen mode Exit fullscreen mode

Secondly, a python module named autopep8 is required:

sudo pip install autopep8
Enter fullscreen mode Exit fullscreen mode

Then add a few code in your .vimrc:

" Install the plugin with vim-plug:
Plug 'skywind3000/vim-rt-format'

" By default, it will be triggered by `ENTER` in insert mode.
" set this to 1 to use `CTRL+ENTER` instead, and keep the  
" default `ENTER` behavior unchanged.
let g:rtf_ctrl_enter = 0

" Enable formatting when leaving insert mode
let g:rtf_on_insert_leave = 1
Enter fullscreen mode Exit fullscreen mode

And everything is ready, restart your vim, edit some files then you will have a nice day.

BTW: Currently, it supports python, lua and javascript.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
fbicknel profile image
Frank Bicknell

Here's your challenge:

Autoformat yaml in rt.

Go.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs