0

I want to format only a part of file and keep the rest unchanged using ALE. I imagine that its kinda possible by providing a fragment of code for example for black formatter and replacing the selected lines with the result retrieved from black.

Applying formatter for a large file could cause too many changes. Its way easier to track what have changed if only a part of the file is changed at once.

4
  • How would ALE know which part of the file to operate on? Setting formatprg is probably a better option, I think? But you need to invoke that manually. – Martin Tournoij Feb 26 at 4:02
  • @MartinTournoij I imagine that visual-line mode could be used to select the block for the formatting. – niekas Feb 26 at 7:40
  • Yeah, you could use formatprg for that, assuming that black can operate on partial files: set formatprg=black (may need flags?), select what you want to be formatted, and then gq. Many people like to map gq to q btw, which is a bit easier to type. – Martin Tournoij Feb 26 at 9:24
  • @MartinTournoij i use Q, perhaps that’s what you meant? Black does have some issues when the partial file is not syntactically correct, and it’s also noisy (needs some cleanup). And i think a stdin or - flag is required – D. Ben Knoble Feb 26 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.