Created
December 22, 2024 19:37
-
-
Save amekusa/c8258fda63a112837923342b374588e1 to your computer and use it in GitHub Desktop.
A neovim function that smartly indents the current line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- A neovim function that smartly indents the current line | |
| -- By github.com/amekusa | |
| -- Tested with neovim v0.10 | |
| -- | |
| -- # What it does | |
| -- If the current line is empty, | |
| -- copies the indentations of the previous or the next line. (The longer one is chosen) | |
| -- | |
| -- If the current line is not empty, | |
| -- simpy shift it to right. | |
| -- | |
| function smart_indent() | |
| local _ | |
| local line = vim.api.nvim_get_current_line() | |
| if line == '' then -- empty line | |
| vim.cmd.startinsert() | |
| local lnum = vim.api.nvim_win_get_cursor(0)[1] | |
| local lnum_max = vim.api.nvim_buf_line_count(0) | |
| -- get indents in the prev line | |
| local indents_prev = '' | |
| if lnum > 1 then | |
| _,_,indents_prev = vim.api.nvim_buf_get_lines(0, lnum - 2, lnum - 1, true)[1]:find([[^(%s*)]]) | |
| end | |
| -- get indents in the next line | |
| local indents_next = '' | |
| if lnum < lnum_max then | |
| _,_,indents_next = vim.api.nvim_buf_get_lines(0, lnum, lnum + 1, true)[1]:find([[^(%s*)]]) | |
| end | |
| -- use longer one | |
| local len_prev = indents_prev:len() | |
| local len_next = indents_next:len() | |
| if len_prev > 0 or len_next > 0 then | |
| if len_prev > len_next | |
| then vim.api.nvim_set_current_line(indents_prev) | |
| else vim.api.nvim_set_current_line(indents_next) | |
| end | |
| vim.api.nvim_win_set_cursor(0, {lnum, 1000}) -- set cursor to EoL | |
| else | |
| vim.api.nvim_feedkeys(fn.key('<Tab>'), 'n', false) -- just type <Tab> | |
| end | |
| else | |
| vim.cmd('>') -- shift the current line to right | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A neovim function that smartly indents the current line
By github.com/amekusa
Tested with neovim v0.10
What it does
If the current line is empty,
copies the indentations of the previous or the next line. (The longer one is chosen)
If the current line is not empty,
simpy shift it to right.