0

How to convert this vimscript code into Lazyvim Lua code?

 function! BreakHere() s/^\(\s*\)\(.\{-}\)\(\s*\)\(\%#\)\(\s*\)\(.*\)/\1\2\r\1\4\6 call histdel("/", -1) endfunction nnoremap K :<C-u>call BreakHere()<CR> 
3
  • Why do you need to convert it? Commented Dec 8, 2024 at 15:49
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the solutions using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Dec 9, 2024 at 6:53
  • the idea splitting line help of keymap capital k (shift + k), opposite of capital j (joining line) works well vimscript help above vim code. but not in neovim lua (Lazyvim). is possible ?? Commented Dec 10, 2024 at 7:55

1 Answer 1

1

I would do:

vim.keymap.set('n', 'K', function() vim.cmd([[s/^\(\s*\)\(.\{-}\)\(\s*\)\(\%#\)\(\s*\)\(.*\)/\1\2\r\1\4\6]]) vim.fn.histdel('/', -1) end, {}) 

Remark:

  • To map keys you can use the vim.keymap.set api
  • [[...]] is the Lua notation for raw string (the equivalent of r".." string in Python).
  • To access the Vim builtin function you can use the vim.fn collection

Remark: for LazyVim the mapping code standard location is ~/.config/nvim/lua/config/keymaps.lua on Linux and ~/AppData/Local/nvim/lua/config/keymaps.lua on Windows.

Remark: Here is a variation of the expression that avoid breaking within the words:

vim.keymap.set('n', 'K', function() vim.cmd([[s/\v^(\s*)(.{-})(>\s*)(\S*%#)(\s*)(.*)/\1\2\r\1\4\6]]) vim.fn.histdel('/', -1) end, {}) 

Remark: If autoindent is set you could also do approximate with:

vim.keymap.set('n', 'K', 'EBi<CR><Esc>EB', {}) 
2
  • thank help convert lua script with zero error. But it doesn't work well, The idea is Splitting line capital k (shift+k) opposite of capital J (joining line), In vim script work quiet well. function! BreakHere() s/^\(\s*\)\(.\{-}\)\(\s*\)\(\%#\)\(\s*\)\(.*\)/\1\2\r\1\4\6 call histdel("/", -1) endfunction nnoremap K :<C-u>call BreakHere()<CR> Commented Dec 10, 2024 at 7:37
  • Could you give us an example that works with the Vim Substitution and not with the mapping? It would help us reproducing the problem :-) Commented Dec 10, 2024 at 7:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.