Skip to main content
added 71 characters in body
Source Link
SergioAraujo
  • 1.2k
  • 11
  • 13
-- keep cursor position when joinig lines
-- undo keeps cursor position (at least line)
local opts = { noremap=true, silent=true }

buf_set_keymap('n', 'J', ':lua preserve(":join")<cr>', opts)
buf_set_keymap('n', 'u', ':lua preserve(":undo")<cr>', opts)
buf_set_keymap('n', '<c-s>', [[:lua preserve("%s/\\s\\+$//ge")<cr>]])
-- keep cursor position when joinig lines
-- undo keeps cursor position (at least line)
local opts = { noremap=true, silent=true }

buf_set_keymap('n', 'J', ':lua preserve(":join")<cr>', opts)
buf_set_keymap('n', 'u', ':lua preserve(":undo")<cr>', opts)
-- keep cursor position when joinig lines
-- undo keeps cursor position (at least line)
local opts = { noremap=true, silent=true }

buf_set_keymap('n', 'J', ':lua preserve(":join")<cr>', opts)
buf_set_keymap('n', 'u', ':lua preserve(":undo")<cr>', opts)
buf_set_keymap('n', '<c-s>', [[:lua preserve("%s/\\s\\+$//ge")<cr>]])
added 632 characters in body
Source Link
SergioAraujo
  • 1.2k
  • 11
  • 13
-- keep cursor position when joinig lines
-- undo keeps cursor position (at least line)
local opts = { noremap=true, silent=true }

buf_set_keymap('n', 'J', ':lua preserve(":join")<cr>', opts)
buf_set_keymap('n', 'u', ':lua preserve(":undo")<cr>', opts)

With all these things your macros could become horrible, so I have an alias to open vim with not plugins or autocommands:

alias vinone="nvim -u NONE -U NONE -N -i NONE -c 'nnoremap <Space> /'"

I am mentioning this because sometimes we fix one thing and cause other problems. Read also about noautocmd

:sil! argdo :noautocmd norm! @a
-- keep cursor position when joinig lines
-- undo keeps cursor position (at least line)
local opts = { noremap=true, silent=true }

buf_set_keymap('n', 'J', ':lua preserve(":join")<cr>', opts)
buf_set_keymap('n', 'u', ':lua preserve(":undo")<cr>', opts)

With all these things your macros could become horrible, so I have an alias to open vim with not plugins or autocommands:

alias vinone="nvim -u NONE -U NONE -N -i NONE -c 'nnoremap <Space> /'"

I am mentioning this because sometimes we fix one thing and cause other problems. Read also about noautocmd

:sil! argdo :noautocmd norm! @a
Some explanations about the added functions
Source Link
SergioAraujo
  • 1.2k
  • 11
  • 13
function _G.preserve(cmd)
    local cmd = string.format('keepjumps keeppatterns execute %q', cmd)
    -- local original_cursor = vim.fn.winsaveview()
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    vim.api.nvim_command(cmd)
    local lastline = vim.fn.line('$')
    -- vim.fn.winrestview(original_cursor)
    if line > lastline then
        line = lastline
    end
    vim.api.nvim_win_set_cursor({0}, {line , col})
end

The function win_get_cursor gives us more precise information. you could also create marks like @maxim-kim suggested.

As we can use this function to squeeze blank lines we are using an if statement to ensure the line will neve be beond the buffer last line.

Here a function to squeeze blank lines using the above function, in this case I placed the function at ~/.config/nvim/lua/tools.lua

local M = {}

M.squeeze_blank_lines = function()
    -- references: https://vi.stackexchange.com/posts/26304/revisions
    local old_query = vim.fn.getreg('/')    -- save search register
    preserve('sil! 1,.s/^\\n\\{2,}/\\r/gn') -- set current search count number
    local result = vim.fn.searchcount({maxcount = 1000, timeout = 500}).current
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    preserve('sil! keepp keepj %s/^\\n\\{2,}/\\r/ge')
    preserve('sil! keepp keepj %s/\\v($\\n\\s*)+%$/\\r/e')
    if result > 0 then
        vim.api.nvim_win_set_cursor({0}, {(line - result), col})
    end
    vim.fn.setreg('/', old_query)           -- restore search register
end

return M

Some more features that make use of the "preserve"

vim.cmd([[cnoreab cls Cls]])
vim.cmd([[command! Cls lua preserve('%s/\\s\\+$//ge')]])
vim.cmd([[command! Reindent lua preserve("sil keepj normal! gg=G")]])

Now you can use cls to remove trailing spaces, Reindent, and many other things you came up with.

function _G.preserve(cmd)
    local cmd = string.format('keepjumps keeppatterns execute %q', cmd)
    -- local original_cursor = vim.fn.winsaveview()
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    vim.api.nvim_command(cmd)
    local lastline = vim.fn.line('$')
    -- vim.fn.winrestview(original_cursor)
    if line > lastline then
        line = lastline
    end
    vim.api.nvim_win_set_cursor({0}, {line , col})
end

The function win_get_cursor gives us more precise information. you could also create marks like @maxim-kim suggested.

function _G.preserve(cmd)
    local cmd = string.format('keepjumps keeppatterns execute %q', cmd)
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    vim.api.nvim_command(cmd)
    local lastline = vim.fn.line('$')
    if line > lastline then
        line = lastline
    end
    vim.api.nvim_win_set_cursor({0}, {line , col})
end

The function win_get_cursor gives us more precise information. you could also create marks like @maxim-kim suggested.

As we can use this function to squeeze blank lines we are using an if statement to ensure the line will neve be beond the buffer last line.

Here a function to squeeze blank lines using the above function, in this case I placed the function at ~/.config/nvim/lua/tools.lua

local M = {}

M.squeeze_blank_lines = function()
    -- references: https://vi.stackexchange.com/posts/26304/revisions
    local old_query = vim.fn.getreg('/')    -- save search register
    preserve('sil! 1,.s/^\\n\\{2,}/\\r/gn') -- set current search count number
    local result = vim.fn.searchcount({maxcount = 1000, timeout = 500}).current
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    preserve('sil! keepp keepj %s/^\\n\\{2,}/\\r/ge')
    preserve('sil! keepp keepj %s/\\v($\\n\\s*)+%$/\\r/e')
    if result > 0 then
        vim.api.nvim_win_set_cursor({0}, {(line - result), col})
    end
    vim.fn.setreg('/', old_query)           -- restore search register
end

return M

Some more features that make use of the "preserve"

vim.cmd([[cnoreab cls Cls]])
vim.cmd([[command! Cls lua preserve('%s/\\s\\+$//ge')]])
vim.cmd([[command! Reindent lua preserve("sil keepj normal! gg=G")]])

Now you can use cls to remove trailing spaces, Reindent, and many other things you came up with.

Source Link
SergioAraujo
  • 1.2k
  • 11
  • 13
Loading