Last active
August 17, 2022 16:05
-
-
Save ii14/c5d6c39c1bc7b8553afe76db3350c043 to your computer and use it in GitHub Desktop.
pipe.lua
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
-- :! replacement that supports char-wise selection | |
-- from visual mode :Pipe <command> | |
vim.api.nvim_create_user_command('Pipe', function(ctx) | |
local ms = vim.api.nvim_buf_get_mark(0, '<') | |
local me = vim.api.nvim_buf_get_mark(0, '>') | |
local mt = vim.fn.visualmode() | |
if mt == '\22' then | |
error 'blockwise selection not supported' | |
end | |
local lines = vim.api.nvim_buf_get_lines(0, ms[1] - 1, me[1], true) | |
local input = vim.deepcopy(lines) | |
if mt == 'v' then | |
input[#input] = input[#input]:sub(1, me[2] + 1) | |
input[1] = input[1]:sub(ms[2] + 1) | |
end | |
local output = vim.fn.systemlist(ctx.args, input) | |
if #output == 0 then | |
output = {''} | |
end | |
if mt == 'v' then | |
output[#output] = output[#output] .. lines[#lines]:sub(me[2] + 2) | |
output[1] = lines[#lines]:sub(1, ms[2]) .. output[1] | |
end | |
vim.api.nvim_buf_set_lines(0, ms[1] - 1, me[1], true, output) | |
end, { range = true, nargs = '+', complete = 'shellcmd' }) | |
-- :w !... replacement that supports char-wise selection | |
-- from visual mode :Pipew <command> | |
vim.api.nvim_create_user_command('Pipew', function(ctx) | |
local ms = vim.api.nvim_buf_get_mark(0, '<') | |
local me = vim.api.nvim_buf_get_mark(0, '>') | |
local mt = vim.fn.visualmode() | |
if mt == '\22' then | |
error 'blockwise selection not supported' | |
end | |
local lines = vim.api.nvim_buf_get_lines(0, ms[1] - 1, me[1], true) | |
if mt == 'v' then | |
lines[#lines] = lines[#lines]:sub(1, me[2] + 1) | |
lines[1] = lines[1]:sub(ms[2] + 1) | |
end | |
print(vim.fn.system(ctx.args, lines)) | |
end, { range = true, nargs = '+', complete = 'shellcmd' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment