Last active
December 24, 2022 13:35
-
-
Save yardenlaif/45ceddc322dd8afc6e4a0435b4985f2f to your computer and use it in GitHub Desktop.
My implementation for surround in visual mode.
This file contains 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
-- Surround | |
function Surround_with(start_surround, end_surround) | |
local vstart = vim.fn.getpos("'<") | |
local vend = vim.fn.getpos("'>") | |
local start_column = vstart[3] - 1 | |
local start_line = vstart[2] | |
local end_column = vend[3] - 1 | |
local end_line = vend[2] | |
-- Skip whitespaces at the start of lines | |
if start_column == 0 then | |
vim.api.nvim_win_set_cursor(0, { start_line, start_column }) | |
local char = vim.api.nvim_get_current_line():sub(start_column + 1, start_column + 1) | |
while char == " " or char == "\t" do | |
start_column = start_column + 1 | |
char = vim.api.nvim_get_current_line():sub(start_column + 1, start_column + 1) | |
end | |
end | |
vim.api.nvim_win_set_cursor(0, { end_line, end_column }) | |
vim.api.nvim_put({ end_surround }, "c", true, true) | |
vim.api.nvim_win_set_cursor(0, { start_line, start_column }) | |
vim.api.nvim_put({ start_surround }, "c", false, true) | |
end | |
function Surround_with_input() | |
local start_input = vim.fn.input("Surround with> ", "") | |
local end_input = start_input | |
if string.find(end_input, "<") == 1 and string.find(end_input, ">") == string.len(end_input) then | |
end_input = "</" .. string.sub(end_input, 2) | |
end | |
if string.len(end_input) == 1 then | |
if end_input == "<" then | |
end_input = ">" | |
end | |
if end_input == "(" then | |
end_input = ")" | |
end | |
if end_input == "{" then | |
end_input = "}" | |
end | |
if end_input == "[" then | |
end_input = "]" | |
end | |
end | |
Surround_with(start_input, end_input) | |
end | |
vim.keymap.set("v", "(", ":lua Surround_with('(', ')')<CR>", opts) | |
vim.keymap.set("x", ")", ":lua Surround_with('( ', ' )')<CR>", opts) | |
vim.keymap.set("x", "[", ":lua Surround_with('[', ']')<CR>", opts) | |
vim.keymap.set("x", "]", ":lua Surround_with('[ ', ' ]')<CR>", opts) | |
vim.keymap.set("x", '"', ":lua Surround_with('\"', '\"')<CR>", opts) | |
vim.keymap.set("x", "'", ":lua Surround_with(''', ''')<CR>", opts) | |
vim.keymap.set("x", "{", ":lua Surround_with('{', '}')<CR>", opts) | |
vim.keymap.set("x", "}", ":lua Surround_with('{ ', ' }')<CR>", opts) | |
vim.keymap.set("x", "S", ":lua Surround_with_input()<CR>", opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't really get used to the keymappings of the surround plugin for vim/neovim, so I wrote this small implementation with everything I need. The keymappings are:
<input>
<input>
hello<input>
<
) the selection will be surrounded with opening and closing tags (for example hello -> <h1>hello</h1>)