Skip to content

Instantly share code, notes, and snippets.

@Elv13
Created April 8, 2019 21:55
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 Elv13/e9caf5399a0ccf41cda2f8cada9a9501 to your computer and use it in GitHub Desktop.
Save Elv13/e9caf5399a0ccf41cda2f8cada9a9501 to your computer and use it in GitHub Desktop.
-- Get a string from the command prompt.
local function prompt(message)
return vim.api.nvim_call_function("input", {message})
end
--- Bring sanity back to search, no idiotic magic by default
local function search()
local res = prompt("Search: "):gsub("/", "\\/")
vim.api.nvim_input("<cmd>/\\V"..res.."/<cr><esc>n")
end
--- Search and replace without idotic magic.
-- no more dozen of backslashes per minute...
local function replace()
local str = prompt("Search: "):gsub("/", "\\/")
local rep = prompt("Replace " ..str.." with: "):gsub("/", "\\/")
vim.api.nvim_input("<cmd>%s/\\V"..str.."/"..rep.."/gc<cr>")
end
-- CTRL+O to save (insert mode)
keymap.insert["<C-s>"] = cmd ":w"
keymap.normal["<C-s>"] = cmd ":w"
-- CTRL+W to search (insert mode)
keymap.insert["<C-w>"] = search
keymap.normal["<C-w>"] = search
keymap.insert["<C-f>"] = search
-- map CTRL+R to search and replace
keymap.insert["<C-r>"] = replace
keymap.normal["<C-r>"] = replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment