DEV Community

Dima Portenko
Dima Portenko

Posted on

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 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
}


Enter fullscreen mode Exit fullscreen mode

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"
    }
  }
}


Enter fullscreen mode Exit fullscreen mode
  • 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 })

Enter fullscreen mode Exit fullscreen mode




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}"
}
}
Enter fullscreen mode Exit fullscreen mode




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!

Top comments (0)