Skip to content
Projekte

Über uns

Tools for working with Markdown in Neovim.

Entwicklerwerkzeuge

So werden diese Zahlen gemessen

What you can do

  • Keybindings over vim motions / visual selection
  • Toggle, delete, and change emphasis and code spans
  • Insert as list with configurable markers
  • Show in current window's location list

Gemessener Sternzuwachs

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
Aktualisierung verzögert

Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.

README.md

Original

markdown.nvim

Tools for working with markdown files in Neovim.

ci tests (nightly)

markdown-nvim-demo

Contents

Features

  • Inline-style
    • Keybindings over vim motions / visual selection
    • Toggle, delete, and change emphasis and code spans
  • Table of contents
    • Insert as list with configurable markers
    • Show in current window's location list
    • Omit entire sections and individual headings with an HTML tag
  • Lists
    • Insert new items
    • Auto-number ordered lists
    • Toggle task list items (GFM)
  • Links
    • Add links over vim motions / visual selection
    • Follow links under the cursor
    • Paste URLs from clipboard over visual selection as links
  • Navigation
    • Jump to the current, parent, next, and previous heading
  • nvim-treesitter module support

Planned features

  • Tables (GFM)
    • Formatting
    • Insert rows and columns

Requirements

Installation

Install markdown.nvim with your preferred plugin manager.

  • lazy.nvim

    {
      "tadmccorkle/markdown.nvim",
      ft = "markdown", -- or 'event = "VeryLazy"' or 'lazy = false' (there isn't much plugin overhead)
      opts = {
        -- configuration here or empty for defaults
      },
    }
    
  • packer.nvim

    use({
      "tadmccorkle/markdown.nvim",
      config = function()
        require("markdown").setup({
          -- configuration here or empty for defaults
        })
      end,
    })
    
  • vim-plug

    Plug 'tadmccorkle/markdown.nvim'
    
    " after plug#end()
    " provide `setup()` configuration options or leave empty for defaults
    lua require('markdown').setup()
    
  • pckr.nvim

    { "tadmccorkle/markdown.nvim",
      config = function()
        require("markdown").setup({
          -- configuration here or empty for defaults
        })
      end,
    };
    

Getting help

markdown.nvim provides help docs that can be accessed by running :help markdown.nvim.

Configuration

Detailed plugin configuration information can be found in the help doc (:h markdown.configuration).

A call to require("markdown").setup() is necessary for commands and keybindings to be registered in markdown buffers.

A table of configuration options can optionally be passed to the setup() function. Any fields in the table will overwrite the corresponding default. markdown.nvim uses the following defaults:

{
  -- Disable all keymaps by setting mappings field to 'false'.
  -- Selectively disable keymaps by setting corresponding field to 'false'.
  mappings = {
    inline_surround_toggle = "gs", -- (string|boolean) toggle inline style
    inline_surround_toggle_line = "gss", -- (string|boolean) line-wise toggle inline style
    inline_surround_delete = "ds", -- (string|boolean) delete emphasis surrounding cursor
    inline_surround_change = "cs", -- (string|boolean) change emphasis surrounding cursor
    link_add = "gl", -- (string|boolean) add link
    link_follow = "gx", -- (string|boolean) follow link
    go_curr_heading = "]c", -- (string|boolean) set cursor to current section heading
    go_parent_heading = "]p", -- (string|boolean) set cursor to parent section heading
    go_next_heading = "]]", -- (string|boolean) set cursor to next section heading
    go_prev_heading = "[[", -- (string|boolean) set cursor to previous section heading
  },
  inline_surround = {
    -- For the emphasis, strong, strikethrough, and code fields:
    -- * 'key': used to specify an inline style in toggle, delete, and change operations
    -- * 'txt': text inserted when toggling or changing to the corresponding inline style
    emphasis = {
      key = "i",
      txt = "*",
    },
    strong = {
      key = "b",
      txt = "**",
    },
    strikethrough = {
      key = "s",
      txt = "~~",
    },
    code = {
      key = "c",
      txt = "`",
    },
  },
  link = {
    paste = {
      enable = true, -- whether to convert URIs to links on paste
    },
  },
  toc = {
    -- Comment text to flag headings/sections for omission in table of contents.
    omit_heading = "toc omit heading",
    omit_section = "toc omit section",
    -- Cycling list markers to use in table of contents.
    -- Use '.' and ')' for ordered lists.
    markers = { "-" },
  },

  on_attach = nil, -- (fun(bufnr: integer)) callback when plugin attaches to a buffer

  -- Hook functions allow for overriding or extending default behavior.
  -- Called with a table of options and a fallback function with default behavior.
  -- Signature: fun(opts: table, fallback: fun())
  hooks = {
    -- Called when following links with resolved context; see "Link follow hook" below.
    follow_link = nil,
  },
}

Buffer attachment

on_attach is useful for creating additional buffer-only keymaps:

on_attach = function(bufnr)
  local map = vim.keymap.set
  local opts = { buffer = bufnr }
  map({ 'n', 'i' }, '<M-l><M-o>', '<Cmd>MDListItemBelow<CR>', opts)
  map({ 'n', 'i' }, '<M-L><M-O>', '<Cmd>MDListItemAbove<CR>', opts)
  map('n', '<M-c>', '<Cmd>MDTaskToggle<CR>', opts)
  map('x', '<M-c>', ':MDTaskToggle<CR>', opts)


  -- inline style visual mode keybindings like `<C-b>` for strong/bold and `<C-i>` for emphasis/italic:
  local function toggle(key)
    return "<Esc>gv<Cmd>lua require'markdown.inline'"
      .. ".toggle_emphasis_visual'" .. key .. "'<CR>"
  end

  vim.keymap.set("x", "<C-b>", toggle("b"), { buffer = bufnr })
  vim.keymap.set("x", "<C-i>", toggle("i"), { buffer = bufnr })
end,

Link follow hook

hooks.follow_link(opts, fallback) overrides following recognized Markdown links. Links delegated to the native gx mapping do not invoke this hook.

Option Meaning
dest Original destination text.
use_default_app Optional boolean; see the default-app <Plug> mapping below.
bufnr Source buffer number.
kind "heading", "external", or "path".
path Absolute normalized local path; only present for path links.
fragment Interpreted path fragment including #, if any.
path_type "file", "directory", "other", "missing", or "unknown"; path links only.
is_markdown Whether the path is Markdown; path links only.
line_range Parsed line hint { start_line = n, end_line = n }, if applicable.

kind describes the link type:

  • "heading": starts with #, looked up in the source document.
  • "external": URI or URL, including www. shorthand.
  • "path": local filesystem destination.

path resolution matches the fallback/default: foo//foo are cwd-relative and ./foo/../foo are source-buffer-relative.

path_type describes the result of filesystem inspection:

  • "file": a regular file; this does not imply readability.
  • "directory": a directory.
  • "other": another filesystem object, such as a FIFO, socket, or device.
  • "missing": inspection failed with ENOENT or ENOTDIR; the target cannot be found at this path.
  • "unknown": another failure, e.g., permission denied; do not treat this as "missing".

For example, open a new buffer for a missing Markdown page:

require("markdown").setup({
  hooks = {
    follow_link = function(opts, fallback)
      if opts.kind == "path" and opts.path_type == "missing" and opts.is_markdown then
        vim.cmd.edit(vim.fn.fnameescape(opts.path))
        return
      end
      fallback()
    end,
  },
})

<Plug> mappings

markdown.nvim sets up <Plug> mappings regardless of configuration. Most are used by the configuration mappings described above:

<Plug> Mapping Configuration Mapping
<Plug>(markdown_toggle_emphasis) inline_surround_toggle
<Plug>(markdown_toggle_emphasis_line) inline_surround_toggle_line
<Plug>(markdown_toggle_emphasis_visual) inline_surround_toggle
<Plug>(markdown_delete_emphasis) inline_surround_delete
<Plug>(markdown_change_emphasis) inline_surround_change
<Plug>(markdown_add_link) link_add
<Plug>(markdown_add_link_visual) link_add
<Plug>(markdown_follow_link) link_follow
<Plug>(markdown_go_current_heading) go_curr_heading
<Plug>(markdown_go_parent_heading) go_parent_heading
<Plug>(markdown_go_next_heading) go_next_heading
<Plug>(markdown_go_prev_heading) go_prev_heading

The following <Plug> mappings are not used by markdown.nvim's configuration:

<Plug> Mapping Description
<Plug>(markdown_follow_link_default_app) Like <Plug>(markdown_follow_link) but opens links to non-markdown files in the default application for the link destination. The hook follow_link is called before following the link with the option 'use_default_app' set to 'true'.

Usage

Detailed usage instructions can be found in the help doc (:h markdown.usage).

markdown.nvim is broken up into different feature categories:

Inline surround

markdown.nvim provides keymaps to toggle, delete, and change emphasis and code spans, referred to in this section as "styles". The supported styles and the default keys used to refer to them are:

Style Key
emphasis (typically rendered in italic) "i"
strong (typically rendered in bold) "b"
strikethrough "s"
code span "c"
  • Toggle

    Inline styles can be toggled over vim motions in normal and visual mode. Toggled styles are only applied to appropriate markdown elements (i.e., not blank lines, list markers, etc.). For example, a motion that includes a list marker and multiple blocks will only apply the style to inline content:

            toggle strong over five lines
    -----------------------------------------------
    paragraph block             **paragraph block**
    - list item                 - **list item**
                        ---->
    another paragraph           **another pargraph
    over two lines              over two lines**
    

    In normal mode this is done with gs{motion}{style}, where {style} is the key corresponding to the style to toggle. Like other vim motions, a [count] can be specified before and after the gs. Styles can also be toggled over the current line using gss{style}. A [count] can be specified to toggle over multiple lines.

    Before Command After
    ^some text gs2es ~~some text~~
    some t^ext gsiwb some **text**
    some *t^ext* gsiwi some text
    ***some^ text*** gssb *some text*

    ^ denotes cursor position

    Styles can be toggled in visual mode based on a visual selection using gs{style}.

    Before Command After
    ^some text$ gss ~~some text~~
    some ^text$ gsb some **text**
    some *^text$* gsi some text
    ***^some text$*** gsb *some text*

    ^ and $ denote selection start and end, respectively

    Styles can also be toggled in visual block mode.

    Before          | Command | After
    ----------------|---------|----------------
    - list ^item$ 1 |         | - list *item* 1
    - li2           |         | - li2
                    |   gsi   |
    - list ^item$ 3 |         | - list *item* 3
    - list ^item$ 4 |         | - list *item* 4
    
    `^` and `$` denote block selection start and end on each line, respectively
    
  • Delete

    Inline styles around the cursor can be deleted in normal mode using ds{style}, where {style} is the key corresponding to the style to delete. Only the style directly surrounding the cursor will be deleted.

    Before Command After
    **some^ *text*** dsb some *text*
    **some *t^ext*** dsb some *text*
    **some **t^ext**** dsb **some text**

    ^ denotes cursor position

  • Change

    Inline styles around the cursor can be changed in normal mode using cs{from}{to}, where {from} and {to} are the keys corresponding to the current style ({from}) and the new style ({to}). Only the matching {from} style directly surrounding the cursor will be changed.

    Before Command After
    **some^ *text*** csbi *some *text**
    **some *t^ext*** csbi *some *text**
    **some **t^ext**** csbs **some ~~text~~**

    ^ denotes cursor position

Table of contents

A table of contents (TOC) is created from the top-level ATX and setext headings of markdown buffers.

Heading destinations are made unique in document order with -1, -2, etc. suffixes, skipping destinations already in use. For example, Heading, Heading, Heading 1 produce #heading, #heading-1, #heading-1-1. All headings outside lists and block quotes count, even those omitted from the TOC or excluded by its max level. Link navigation uses the same destinations, including links to headings in other files. These anchors are GitHub-like, not guaranteed to match GitHub or other renderers.

The :MDInsertToc [max_level] [markers] command adds a TOC by inserting (normal mode) or replacing selected lines (visual mode). Optional arguments can be provided to set the max heading level to include and the list markers to alternate through for each heading level.

The :MDToc [max_level] [size] and :MDTocAll [max_level] [size] commands show a TOC for the current buffer in the current window's location list. :MDToc omits flagged headings and :MDTocAll includes all headings. The optional [max_level] argument specifies the max heading level to include, and the optional [size] argument specifies the desired size of the location list. These commands also forward command modifiers to :lopen.

Omit sections and headings

Headings and entire sections can be omitted from the TOC by flagging them with <!-- toc omit heading --> and <!-- toc omit section -->, respectively. The flag can either be placed directly above (i.e., on the line immediately preceding) or within the heading content. For example, the following headings would be omitted:

# heading 1 <!-- toc omit heading -->

<!-- toc omit heading -->
## heading 2

<!-- toc omit section -->
# section heading omitted
## subsection heading also omitted

Lists

Most list editing commands are intended to be invoked by custom keymaps (see notes on the on_attach field under configuration).

  • Inserting items

    Use the :MDListItemBelow and :MDListItemAbove commands to insert a new list item below and above the current cursor position, respectively. Both commands maintain the same indentation and list marker as the item under the cursor. The commands do nothing if the cursor is not within an existing list.

    When inserting an item in an ordered list, numbering is reset automatically for that list.

  • Reset numbering

    The :MDResetListNumbering command resets the numbering of all ordered lists in the current buffer (normal mode) or under the current visual selection (visual mode).

  • Toggle tasks

    The :MDTaskToggle command toggles the task(s) on the current cursor line (normal mode) or under the current visual selection (visual mode).

Links

  • Add

    Links can be added over vim motions in normal and visual mode. Links are only added when the motion is within one inline block (i.e., not over list markers, blank lines, etc.). In normal mode this is done with gl{motion} and over a visual selection with gl.

  • Follow

    Follow inline Markdown links ([text](destination)) under the cursor in normal mode with gx. Supported in-editor navigation:

    • #destination: headings in the current buffer
    • ./destination: files and directories relative to the current buffer
    • /destination: files and directories relative to the working directory
    • Other absolute path destinations are opened if they exist

    File links can include line jumps like #L56, #L56-L60 (GitHub range), or #L56-60 (GitLab range), e.g., [source](./test.lua#L56). Only uppercase L and decimal digits are supported; bare #56 is not.

    Explicit link destination URIs such as https://example.org, mailto:[email protected], and file:///path/document.pdf are passed unchanged to vim.ui.open and opened with the system default handler.

    Outside a supported Markdown link, gx delegates to the mapping that was active before the plugin attached (likely the builtin Neovim mapping). This preserves native URL/path handling and, where supported by the installed Neovim version, extmark, Tree-sitter, and LSP hyperlinks.

  • Paste URIs

    URIs such as https://example.org, mailto:[email protected], and file:///path/document.pdf, as well as www. URLs, can be pasted over a visual selection (not a visual block selection) from the system clipboard as markdown links.

Navigation

Markdown buffers can be navigated with the following keymaps:

  • ]c: go to the current section heading
  • ]p: go to the parent section heading
  • ]]: go to the next section heading
  • [[: go to the previous section heading

Runtime ftplugin keymaps for older Neovim versions (e.g., 0.10.0) can overwrite markdown.nvim ]] and [[ keymaps. These can be disabled with vim.g.no_markdown_maps = 1.

nvim-treesitter module

markdown.nvim can also be configured as an nvim-treesitter module.

[!CAUTION] This feature is only supported when using nvim-treesitter's master branch, which has been locked and remains available for backward compatibility.

Module installation

The following code snippets show how to install markdown.nvim as an nvim-treesitter module. Refer to nvim-treesitter for the appropriate way to install and manage parsers.

  • lazy.nvim

    {
      "nvim-treesitter/nvim-treesitter",
      dependencies = { "tadmccorkle/markdown.nvim" },
      config = function()
        require("nvim-treesitter.configs").setup({
          ensure_installed = { "markdown", "markdown_inline", --[[ other parsers you need ]] },
          markdown = {
            enable = true,
            -- configuration here or nothing for defaults
          },
        })
      end,
    }
    
  • packer.nvim

    use({
      "nvim-treesitter/nvim-treesitter",
      requires = { { "tadmccorkle/markdown.nvim" } },
      config = function()
        require("nvim-treesitter.configs").setup({
          ensure_installed = { "markdown", "markdown_inline", --[[ other parsers you need ]] },
          markdown = {
            enable = true,
            -- configuration here or nothing for defaults
          },
        })
      end,
    })
    
  • vim-plug

    Plug 'nvim-treesitter/nvim-treesitter'
    Plug 'tadmccorkle/markdown.nvim'
    
    " after plug#end()
    lua << EOF
    require("nvim-treesitter.configs").setup({
      ensure_installed = { "markdown", "markdown_inline", --[[ other parsers you need ]] },
      markdown = {
        enable = true,
        -- configuration here or nothing for defaults
      },
    })
    EOF
    
  • pckr.nvim

    { "nvim-treesitter/nvim-treesitter",
      requires = { "tadmccorkle/markdown.nvim" },
      config = function()
        require("nvim-treesitter.configs").setup({
          ensure_installed = { "markdown", "markdown_inline", --[[ other parsers you need ]] },
          markdown = {
            enable = true,
            -- configuration here or nothing for defaults
          },
        })
      end,
    };
    

Module configuration

When markdown.nvim is configured as an nvim-treesitter module, configuration options are passed to the require("nvim-treesitter.configs").setup() function. All configuration options are the same as described in the configuration section.

local configs = require("nvim-treesitter.configs")

configs.setup({
  ensure_installed = { "markdown", "markdown_inline", --[[ other parsers you need ]] },
  markdown = {
    enable = true, -- must be specified to enable markdown.nvim as a module
    -- configuration here or nothing for defaults
  },
})

Ähnliche Projekte entdecken

Wird geladen…