Supercharge Your Neovim Workflow with project-cli-commands.nvim

If you’re a Neovim enthusiast looking for a way to streamline your workflow by quickly running terminal commands within your current project, then look no further. Introducing project-cli-commands.nvim, a powerful plugin that integrates with Telescope …


This content originally appeared on DEV Community and was authored by Dima Portenko

If you're a Neovim enthusiast looking for a way to streamline your workflow by quickly running terminal commands within your current project, then look no further. Introducing project-cli-commands.nvim, a powerful plugin that integrates with Telescope and ToggleTerm to provide a seamless experience for executing project-specific CLI commands.

Image description

What is project-cli-commands.nvim?

project-cli-commands.nvim is a Neovim plugin that allows you to run terminal commands effortlessly from within your editor. Whether you need to list files, run tests, or execute any custom script, this plugin has got you covered. It offers features like running commands with input, copying commands to clipboard, and injecting the current buffer path into commands.

Installation

To get started, add the following configuration to your Neovim setup:

{
  "dimaportenko/project-cli-commands.nvim",

  dependencies = {
    "akinsho/toggleterm.nvim",
    "nvim-telescope/telescope.nvim",
  },

  config = function()
    local OpenActions = require('project_cli_commands.open_actions')
    local RunActions = require('project_cli_commands.actions')

    local config = {
      running_telescope_mapping = {
        ['<C-c>'] = RunActions.exit_terminal,
        ['<C-f>'] = RunActions.open_float,
        ['<C-v>'] = RunActions.open_vertical,
        ['<C-h>'] = RunActions.open_horizontal,
      },
      open_telescope_mapping = {
        { mode = 'i', key = '<CR>',  action = OpenActions.execute_script_vertical },
        { mode = 'n', key = '<CR>',  action = OpenActions.execute_script_vertical },
        { mode = 'i', key = '<C-h>', action = OpenActions.execute_script },
        { mode = 'n', key = '<C-h>', action = OpenActions.execute_script },
        { mode = 'i', key = '<C-i>', action = OpenActions.execute_script_with_input },
        { mode = 'n', key = '<C-i>', action = OpenActions.execute_script_with_input },
        { mode = 'i', key = '<C-c>', action = OpenActions.copy_command_clipboard },
        { mode = 'n', key = '<C-c>', action = OpenActions.copy_command_clipboard },
        { mode = 'i', key = '<C-f>', action = OpenActions.execute_script_float },
        { mode = 'n', key = '<C-f>', action = OpenActions.execute_script_float },
        { mode = 'i', key = '<C-v>', action = OpenActions.execute_script_vertical },
        { mode = 'n', key = '<C-v>', action = OpenActions.execute_script_vertical },
      }
    }

    require('project_cli_commands').setup(config)
  end
}

Usage

Commands Configuration

Configuration for project-cli-commands.nvim is stored in a .nvim/config.json file. If the file does not exist, the plugin will prompt you to create one when you run Telescope project_cli_commands open.

Example config.json:

{
  "env": ".env",
  "commands": {
    "ls:la": "ls -tls",
    "current:ls": "ls -la ${currentBuffer}",
    "print:env": "echo $EXPO_TOKEN",
    "print:env:local": {
      "cmd": "echo $EXPO_TOKEN",
      "env": ".env.local",
      "after": "Telescope find_files"
    }
  }
}
  • env (optional): Path to the environment file to load before running the command.
  • commands: List of terminal commands with optional configurations.

Telescope Commands

  • Telescope project_cli_commands open: Opens Telescope with a list of commands from config.json.
  • Telescope project_cli_commands running: Opens Telescope with a list of running commands, allowing you to toggle or stop them.

Keymap

Add these key mappings to your Neovim configuration for quick access:

vim.api.nvim_set_keymap("n", "<leader>p", ":Telescope project_cli_commands open<cr>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<leader>;", ":Telescope project_cli_commands running<cr>", { noremap = true, silent = true })

Features

Open Terminal with Command

You can open a terminal in float, vertical, or horizontal mode to run your commands.

Run Command with Input

This feature allows you to add extra arguments to your terminal command dynamically.

Copy Command to Clipboard

By pressing Ctrl+c, you can copy the command to your clipboard.

Run Command After

Run a Neovim command after the terminal command finishes execution.

Environment Variables

Load environment variables from a file before running a command.

Inject Current Buffer Path to Command

For example, to run tests for the current buffer:

{
  "commands": {
    "test:current": "jest ${currentBuffer}"
  }
}

List of Running Commands

Use Telescope project_cli_commands running to manage your running commands, including showing/hiding terminals or stopping commands.

Conclusion

project-cli-commands.nvim is a must-have plugin for Neovim users who want to boost their productivity by seamlessly integrating terminal commands into their workflow. Give it a try and see how it can transform your development experience!


This content originally appeared on DEV Community and was authored by Dima Portenko


Print Share Comment Cite Upload Translate Updates
APA

Dima Portenko | Sciencx (2024-07-21T13:23:43+00:00) Supercharge Your Neovim Workflow with project-cli-commands.nvim. Retrieved from https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/

MLA
" » Supercharge Your Neovim Workflow with project-cli-commands.nvim." Dima Portenko | Sciencx - Sunday July 21, 2024, https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/
HARVARD
Dima Portenko | Sciencx Sunday July 21, 2024 » Supercharge Your Neovim Workflow with project-cli-commands.nvim., viewed ,<https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/>
VANCOUVER
Dima Portenko | Sciencx - » Supercharge Your Neovim Workflow with project-cli-commands.nvim. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/
CHICAGO
" » Supercharge Your Neovim Workflow with project-cli-commands.nvim." Dima Portenko | Sciencx - Accessed . https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/
IEEE
" » Supercharge Your Neovim Workflow with project-cli-commands.nvim." Dima Portenko | Sciencx [Online]. Available: https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/. [Accessed: ]
rf:citation
» Supercharge Your Neovim Workflow with project-cli-commands.nvim | Dima Portenko | Sciencx | https://www.scien.cx/2024/07/21/supercharge-your-neovim-workflow-with-project-cli-commands-nvim/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.