diff options
| author | Robert Günzler <r@gnzler.io> | 2021-07-29 23:39:08 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2021-07-29 23:40:32 +0200 |
| commit | 3079e935c793a9aa1831fd165e28636eb7a87dae (patch) | |
| tree | e832ac6a08ad70c14c01b1eac32ac1b5eec3549f /.vim/lua/misc.lua | |
| parent | cefbce656c511a3583744ed7735aa71249b73500 (diff) | |
vim: cleanup config
Diffstat (limited to '.vim/lua/misc.lua')
| -rw-r--r-- | .vim/lua/misc.lua | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/.vim/lua/misc.lua b/.vim/lua/misc.lua index 4f2b9dc..0067efb 100644 --- a/.vim/lua/misc.lua +++ b/.vim/lua/misc.lua @@ -31,4 +31,30 @@ function M.open_under_cursor(cmd) end) end +function get_visual_selection() + pos1 = vim.fn.getpos("'<") + ln1, col1 = pos1[2], pos1[3] + pos2 = vim.fn.getpos("'>") + ln2, col2 = pos2[2], pos2[3] + lines = vim.fn.getline(ln1, ln2) + if vim.opt.selection:get() == 'inclusive' then + lines[#lines] = lines[#lines]:sub(1,col2) + else + lines[#lines] = lines[#lines]:sub(1,col2-1) + end + lines[1] = lines[1]:sub(col1) + return pos1, pos2, lines +end + +function M.sort_line() + local fn = vim.fn + pos1, pos2, lines = get_visual_selection() + for i=1, #lines do + local ln = pos1[2] + (i-1) + local line = fn.join(fn.sort(fn.split(lines[i], " "))) + line = fn.substitute(fn.getline(ln), lines[i], line, "") + vim.fn.setline(ln, line) + end +end + return M |