Skip to content

Instantly share code, notes, and snippets.

@LudoPinelli
Last active December 28, 2021 11:40
Show Gist options
  • Save LudoPinelli/ab0f076daddbff019e0489f76146657e to your computer and use it in GitHub Desktop.
Save LudoPinelli/ab0f076daddbff019e0489f76146657e to your computer and use it in GitHub Desktop.
Neovim + toggleterm plugin utility snippet for Rust: launching cargo watch automaticaly on current project
local cargo_watch = Terminal:new({
-- launch cargo watch with all the necessary options to just have the project running:
cmd = "cargo watch -q -c -x 'run -q'",
dir = "git_dir",
-- open the terminal under the file (use "vertical" to open it on the right)
direction = "horizontal",
-- automaticaly quit the process when the terminal is closed:
close_on_exit = true,
on_open = function(term)
-- when the terminal is launched the cursor is sent back into the code (use ':wincmd h' if terminal on the right):
vim.cmd(":wincmd k")
-- to be able to also quit with 'q' in the terminal (in normal mode)
vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "<cmd>close<CR>", { noremap = true, silent = true })
end,
})
--[[
function to bind with a keymap or in which-key or whatever, e.g.:
require("file.where.this.snippet.is")
vim.api.nvim_set_keymap('n', '<Leader>tw', "<Cmd>lua cargo_watch_toggle()<CR>", {noremap = true})
]]--
function cargo_watch_toggle()
cargo_watch:toggle()
end
--[[
Keymapping examples
--]]
-- regular kaymapping
require("file.where.the.snippet.is")
vim.api.nvim_set_keymap('n', '<Leader>tw', "<Cmd>lua cargo_watch_toggle()<CR>", {noremap = true})
-- or in Witch-key:
require("file.where.the.snippet.is")
local mappings = {
-- ...
t = {
name = "  Terminal",
-- ...
w = { "<Cmd>lua cargo_watch_toggle()<CR>", " Cargo Watch" },
},
}
which_key.register(mappings, opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment