commit 2e8256877de2e3ebc2ac1f090705294187e6f174 Author: McAuleyPenney <134115@gmail.com> Date: Mon Oct 25 21:20:04 2021 -0700 [init]: diff --git a/lua/init.lua b/lua/init.lua new file mode 100644 index 0000000..bf2d98f --- /dev/null +++ b/lua/init.lua @@ -0,0 +1,42 @@ +-- MP +-- These sources did all the work +-- 1. https://vim.fandom.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace +-- 2. https://stackoverflow.com/questions/7495932/how-can-i-trim-blank-lines-at-the-end-of-file-in-vim + +local M = {} + + +function M.clear_spaces() + + -- get tuple of cursor position before making changes + local pos = vim.api.nvim_win_get_cursor( 0 ) + + -- delete all whitespace, see source 1 + vim.cmd[[:%s/\s\+$//e]] + + -- delete all lines at end of buffer, see source 2 + vim.cmd[[:silent! 0;/^\%(\n*.\)\@!/,$d]] + + -- get row count after line deletion + local end_row = vim.api.nvim_buf_line_count( 0 ) + + + --[[ + if the row value in the original cursor + position tuple is greater than the + line count after empty line deletion + (meaning that the cursor was inside of + the empty lines at the end of the file + when they were deleted), set the cursor + row to the last line + ]] + + if pos[1] > end_row then + pos[1] = end_row + end + + vim.api.nvim_win_set_cursor( 0, pos ) +end + + +return M diff --git a/plugin/tidy.vim b/plugin/tidy.vim new file mode 100644 index 0000000..c4b6e6c --- /dev/null +++ b/plugin/tidy.vim @@ -0,0 +1,5 @@ + +augroup Tidy + au! + au BufWritePre * lua require( "aucmd.functions" ).clear_spaces() +augroup END diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..9478d7f --- /dev/null +++ b/readme.md @@ -0,0 +1,9 @@ +#### Tidy.nvim + +I like to keep my files orderly. Tidy.nvim is a function and autocommand pair that helps me do this by removing +all empty lines from the end of the file and trailing whitespace from all lines on every save. + + +Credits: +1. https://vim.fandom.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace +2. ib. from https://stackoverflow.com/questions/7495932/how-can-i-trim-blank-lines-at-the-end-of-file-in-vim