-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add trim_file
command and trim_file_on_save
configuration option
#1481
Comments
This would maybe need an additional config option to not do it for heavy files, since trimming could be quite long on files of 100k lines ? Even if there is no trimming to be done, each line must be checked individually (though that should be parallel once the initial reading is done). |
Add-on: maybe a default option + a per language one since markdown uses "two spaces at end of line" as code for "insert visible line break here" but I don't know of any other commonly used language that does something like this |
It's worth noting that Currently, we rely on the LSP implementation to format on document save, which will often include trimming whitespace from the end of lines. If the LSP is not present or doesn't implement formatting, then you are currently stuck doing it manually. There is discussion of what to do about this elsewhere but I'm on mobile, may find it later and comment. In the meantime, you can select the entire document ( |
#1396 would somewhat alleviate this issue. |
As for applying some operation to all the files in a long session, macros can probably help automate that. |
Here's a macro that should trim trailing whitespace from the end of all lines in the current buffer, save it, then close it. If you yank the macro into the
|
I don't like this solution much; it forces users to have a default language server on every new install of helix and to wait for it to spin up to quickly trim a file that failed an |
Using the current version from the # ~/.config/helix/config.toml
# <...>
[keys.normal]
C-s = [
"save_selection",
"select_all",
# Delete trailing whitespace from end of each line
":pipe sed 's/[ \t]*$//'",
# Delete trailing blank lines (including whitespace) from end of the buffer
":pipe awk '/^\\s*$/ {b=b $0 \"\\n\"; next;} {printf \"%s\",b; b=\"\"; print;}'",
"collapse_selection",
"jump_backward",
"commit_undo_checkpoint",
":write",
]
# <...> Current issues:
References: |
As noted by @DannyJJK, this has some other consequences. Use the |
I'm not sure if that's a good idea, the documentation for this says:
|
thx, i try to keep the file has new line on end of file, it work.
I want know is there some other way to do that? |
FWIW, if you use formatter = { command = "sed", args = ["s/[ \t]*$//"] } |
FYI, if you don't want to exit file after trimming white spaces, but you want to keep editing the files, you may find the following command to be helpful as it keeps the original cursor position: "space" = { "m" = [ "save_selection", "select_all", ":pipe sed 's/[[:space:]]*$//'", "collapse_selection", "jump_backward", "flip_selections" ] } The trick here is you need to use |
Describe your feature request
I know there is an
trim_selection
command but using it breaks the current selection, which is a not very helpful. It's also not very practical to do it for each opened file when ending a somewhat long session.The text was updated successfully, but these errors were encountered: