Git Product home page Git Product logo

command_center.nvim's Introduction

command_center.nvim

Create and manage keymaps and commands in a more organized manner, and search them quickly through Telescope.

Demo

demo

Change log

Table of Contents

Install

This plugin requires Telescope to be installed.

vim-plug

Plug "nvim-telescope/telescope.nvim"
Plug "FeiyouG/command_center.nvim"

Packer

use {
  "FeiyouG/command_center.nvim",
  requires = { "nvim-telescope/telescope.nvim" }
}

Use

Setup

First, you will need to load this plugin as a Telescope extension. Put this somewhere in your config:

require("telescope").load_extension("command_center")

Then, you can open command-center by calling :Telescope command_center. I also use the following keybinding:

vim.cmd "nnoremap <leader>fc <CMD>Telescope command_center<CR>"

And, of course, the above keybindings can also be created in command-center-way. Keep reading the following sections.

Add commands

command_center.add

The function command_center.add(commands, opts) does two things:

  1. Set the keymaps (if any)
  2. Add the commands to command_center

You can find an example below:

local command_center = require("command_center")
local noremap = {noremap = true}
local silent_noremap = {noremap = true, silent = true}

command_center.add({
  {
    desc = "Search inside current buffer",
    cmd = "<CMD>Telescope current_buffer_fuzzy_find<CR>",
    keys = { "n", "<leader>fl", noremap },
  },  {
    -- If no descirption is specified, cmd is used to replace descirption by default
    -- You can change this behavior in setup()
    cmd = "<CMD>Telescope find_files<CR>",
    keys = { "n", "<leader>ff", noremap },
  }, {
    -- If no keys are specified, no keymaps will be displayed nor set
    desc = "Find hidden files",
    cmd = "<CMD>Telescope find_files hidden=true<CR>",
  }, {
    -- You can specify multiple keys for the same cmd ...
    desc = "Show document symbols",
    cmd = "<CMD>Telescope lsp_document_symbols<CR>",
    keys = {
      {"n", "<leader>ss", noremap},
      {"n", "<leader>ssd", noremap},
    },
  }, {
    -- ... and for different modes
    desc = "Show function signaure (hover)",
    cmd = "<CMD>lua vim.lsp.buf.hover()<CR>",
    keys = {
      {"n", "K", silent_noremap },
      {"i", "<C-k>", silent_noremap },
    }
  }, {
    -- You can pass in a key sequences as if you would type them in nvim
    desc = "My favorite key sequence",
    cmd = "A  -- Add a comment at the end of a line",
    keys = {"n", "<leader>Ac", noremap}
  }, {
    -- You can also pass in a lua functions as cmd
    -- NOTE: binding lua funciton to a keymap requires nvim 0.7 and above
    desc = "Run lua function",
    cmd = function() print("ANONYMOUS LUA FUNCTION") end,
    keys = {"n", "<leader>alf", noremap},
  }, {
    -- If no cmd is specified, then this entry will be ignored
    desc = "lsp run linter",
    keys = {"n", "<leader>sf", noremap},
  }
})

NOTE:

  • If you are on neovim 0.6, then you can add a Lua function as a cmd and execute it inside command_center. However, you are not able to set the keymaps for the lua function.

If you have above snippet in your config, command-center will create your specified keybindings automatically. And calling :Telescope command_center will open a prompt like this:

demo1

command_center.mode

command_center.add() will add and set the keymaps for you by default. You can use command_center.mode to override this behavior.

mode = {
  ADD = 1,      -- only add the commands to command_center
  SET = 2,      -- only set the keymaps
  AND_SET = 3,  -- add the commands and set the keymaps
}

An example usage of command_center.mode:

local command_center = require("command_center")

-- Set the keymaps for commands only
-- This allows you to use command_center just as a convenient
-- and organized way to manage your keymaps
command_center.add({
  {
    desc = "Find files",
    cmd = "<CMR>telescope find_files<CR>",
    keys = { "n", "<leader>ff", { noremap = true } },
  }, {
    -- If keys is not specified, then this enery is ignored
    -- since there is no keymaps to set
    desc = "Search inside current buffer",
    cmd = "<CMD>Telescope current_buffer_fuzzy_find<CR>",
  }
}, {
  mode = command_center.mode.SET
})


-- Only add the commands to command_center
-- This is helpful if you already registered the keymap somewhere else
-- and want to avoid set the exact keymap twice
command_center.add({
  {
    -- If keys are specified,
    -- then they will still show up in command_center but won't be registered
    desc = "Find hidden files",
    cmd = "<CMD>Telescope find_files hidden=true<CR>",
    keys = { "n", "<leader>f.f", noremap },
  }, {
    desc = "Show document symbols",
    cmd = "<CMD>Telescope lsp_document_symbols<CR>",
  }, {
    -- The mode can be even further overridden within each item
    desc = "LSP cdoe actions",
    cmd = "<CMD>Telescope lsp_code_actions<CR>",
    keybinginds = { "n", "<leader>sa", noremap },
    mode = command_center.mode.ADD_SET,
  }
}, {
  mode = command_center.mode.ADD
})

Above snippet will only set the keymaps for "Find files" and "LSP code actions", but not for others. The resulted command_center prompt will look like this:

demo2

Filter

You can filter the commands upon invoking :Telescope command_center.

Currently, you can filter either by mode or category. You can find some examples below:

  • Show only commands that has keymaps that work in normal mode
:Telescope command_center mode=n
  • Show only commands that in "git" category

    :Telescope command_center category=git
    

    To make this work, you have to first set the category when you add a command. For example:

    command_center.add({
      {
        description = "Open git diffview",
        cmd = "<CMD>DiffviewOpen<CR>",
        keybindings = { "n", "<leader>gd", noremap },
        category = "git",
      }, {
        description = "Close current git diffview",
        cmd = "<CMD>DiffviewClose<CR>",
        keybindings = { "n", "<leader>gc", noremap },
        category = "git",
      }, {
        description = "Toggle markdown preview",
        cmd = "<CMD>MarkdownPreviewToggle<CR>",
        keybindings = { "n", "<leader>mp", noremap },
        category = "markdown",
      }
    })
    
    -- Or you can set up the category for multiple commands at once
    command_center.add({
      {
        description = "Open git diffview",
        cmd = "<CMD>DiffviewOpen<CR>",
        keybindings = { "n", "<leader>gd", noremap },
      }, {
        description = "Close current git diffview",
        cmd = "<CMD>DiffviewClose<CR>",
        keybindings = { "n", "<leader>gc", noremap },
      }, {
        -- category set in a smaller scope takes precedence
        description = "Toggle markdown preview",
        cmd = "<CMD>MarkdownPreviewToggle<CR>",
        keybindings = { "n", "<leader>mp", noremap },
        category = "markdown",
      }
    }, {
      mode = command_center.mode.ADD_ONLY,
      category = "git"
    })
    
  • Or both

:Telescope command_center mode=n category=markdown

command_center.remove()

command_center.remove(commands, opts)

You can also remove commands from command_center, with the following limitations:

  1. You need to pass in commands with the exact same desc, cmd, and keys in order to remove it from command_center.

Furthermore, you can find an example usage in the wiki page.

command_center.converter

The functions in command_center.converter can be used to convert commands used by command_center to/from the conventions used by another plugin/functions.

Current available converters are:

  • command_center.converter.to_nvim_set_keymap(commands)
  • command_center.converter.to_hydra_heads(commands)

You can find some example usage of converters in wiki page.

Configure

The following is the default configuration for command_center:

{
  -- Specify what components are shown in telescope prompt;
  -- Order matters, and components may repeat
  components = {
    command_center.component.DESC,
    command_center.component.KEYS,
    command_center.component.CMD,
    command_center.component.CATEGORY,
  },

  -- Spcify by what components that search results are ordered;
  -- Order does not matter
  sort_by = {
    command_center.component.DESC,
    command_center.component.KEYS,
    command_center.component.CMD,
    command_center.component.CATEGORY,
  },

  -- Change the separator used to separate each component
  separator = " ",

  -- When set to false,
  -- The description compoenent will be empty if it is not specified
  auto_replace_desc_with_cmd = true,

  -- Default title to Telescope prompt
  prompt_title = "Command Center",
}

Example configuration

Below is my personal configuration for command_center. You can use it as a reference.

local telescope = require("telescope")
local command_center = require("command_center")
local noremap = { noremap = true }

command_center.add({
  {
    desc = "Open command_center",
    cmd = "<CMD>Telescope command_center<CR>",
    keys = {
      {"n", "<Leader>fc", noremap},
      {"v", "<Leader>fc", noremap},

      -- If ever hesitate when using telescope start with <leader>f,
      -- also open command center
      {"n", "<Leader>f", noremap},
      {"v", "<Leader>f", noremap},
    },
  }
}, command_center.mode.REGISTER_ONLY)

telescope.setup {
  extensions = {
    command_center = {
      components = {
        command_center.component.DESC,
        command_center.component.KYES,
      },
      sort_by = {
        command_center.component.DESC,
        command_center.component.KEYS,
      },
      auto_replace_desc_with_cmd = false,
    }
  }
}

telescope.load_extension('command_center')

Related Projects

command_center.nvim's People

Contributors

d12bb avatar feiyoug avatar lawrence-laz avatar mattdibi avatar zane- avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.