Skip to content

Instantly share code, notes, and snippets.

@jesseleite
Created December 21, 2023 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesseleite/a61c73757ea51b6ced1de6b61c568546 to your computer and use it in GitHub Desktop.
Save jesseleite/a61c73757ea51b6ced1de6b61c568546 to your computer and use it in GitHub Desktop.
Toggle surrounding quote style mapping in Neovim (probably not perfect lol!)
local toggle_surrounding_quote_style = function ()
local current_line = vim.fn.line('.')
local next_single_quote = vim.fn.searchpos("'", 'cn')
local next_double_quote = vim.fn.searchpos('"', 'cn')
if next_single_quote[1] ~= current_line then
next_single_quote = false
end
if next_double_quote[1] ~= current_line then
next_double_quote = false
end
if next_single_quote == false and next_double_quote == false then
print('Could not find quotes on current line!')
elseif next_single_quote == false and next_double_quote ~= false then
vim.cmd.normal([[macs"'`a]])
elseif next_single_quote ~= false and next_double_quote == false then
vim.cmd.normal([[macs'"`a]])
elseif next_single_quote[2] > next_double_quote[2] then
vim.cmd.normal([[macs"'`a]])
else
vim.cmd.normal([[macs'"`a]])
end
end
vim.keymap.set('n', "<Leader>'", toggle_surrounding_quote_style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment