---
title: "ターミナルがダサいとモテない。NeovimでMarkdown-ToC編 | grasys blog"
url: "https://blog.grasys.io/post/yusukeh/terminal-neovim-markdown-toc"
description: "markdown tocがauto updateしたら便利 Photo by frame harirak on Unsplash 恵比寿でIT企業をやっているとモテると聞いて創業しましたが早いことありがたいことに10年目に突入した長谷川です。 まだモテる成果は出ていません、、、これからです！（もうめんどくさいのでカウントやめました😆） Neovimでmarkdown表示しながらでmermaidも表示はこちらを見ていただくとして・・・ …"
---

# ターミナルがダサいとモテない。NeovimでMarkdown-ToC編

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[yusukeh](/authors/yusukeh/)
-   公開日：2024年11月22日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#シリーズ: ターミナル環境](/tags/シリーズ-ターミナル環境/)[#Neovim](/tags/neovim/)[#Markdown](/tags/markdown/)[#AstroNvim](/tags/astronvim/)

![ターミナルがダサいとモテない。NeovimでMarkdown-ToC編](/_astro/ogp.DjvEZ7qV_ZAmSmc.webp)

Photo by [frame harirak](https://unsplash.com/@framemily?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)

markdown-tocがauto updateしたら便利

恵比寿でIT企業をやっているとモテると聞いて創業しましたが早いことありがたいことに10年目に突入した長谷川です。

まだモテる成果は出ていません、、、これからです！（もうめんどくさいのでカウントやめました😆）

Neovimでmarkdown表示しながらでmermaidも表示はこちらを見ていただくとして・・・

## ターミナルがダサいとモテない。mermaidを表示するぞ編（NeoVim+AstroNvim v4）

### あなたのNeoVimはmermaidを表示できますか？

medium.com

今回はMarkdown-ToCです。

Markdownを書くときToCが自動で更新されると便利です。

このpluginを使います。

## GitHub - hedyhli/markdown-toc.nvim: Generate and auto-update table of contents list for markdown

### Generate and auto-update table of contents list for markdown - hedyhli/markdown-toc.nvim

github.com

## Install

ほぼ上記のRepositoryのREADMEのままですが😅

### ~/.config/nvim/lua/plugins/my-markdown-toc-nvim.lua

Plain textcontent\_copy

```
return {
  "hedyhli/markdown-toc.nvim",
  ft = "markdown", -- Lazy load on markdown filetype
  cmd = { "Mtoc" }, -- Or, lazy load on "Mtoc" command
  opts = {
    -- Your configuration here (optional)
    headings = {
      -- Include headings before the ToC (or current line for `:Mtoc insert`)
      before_toc = false,
      -- Either list of lua patterns (regex),
      -- or a function that takes a heading title and returns boolean (true means
      -- to EXCLUDE heading).
      exclude = {},
      -- The first capture is for heading level ('###') and second is for the heading
      -- title.
      pattern = "^(#+)%s+(.+)$",
    },

    -- Config relating to the style and format of the ToC
    toc_list = {
      -- string or list of strings (for cycling)
      -- If cycle_markers = false and markers is a list, only the first is used.
      -- You can set to '1.' to use a automatically numbered list for ToC (if
      -- your markdown render supports it).
      markers = "*",
      cycle_markers = false,
      -- Example config for cycling markers:
      ----- markers = {'*', '+', '-'},
      ----- cycle_markers = true,

      -- Integer or a function that returns an integer.
      -- If function, it is called every time the ToC is regenerated. This allows the use
      -- of retrieving buffer-local settings like shiftwidth.
      indent_size = 2,

      -- Remove the ${indent} below, or set indent_size=0 to have the whole ToC
      -- be a flattened list.
      item_format_string = "${indent}${marker} [${name}](#${link})",

      ---Formatter for a single ToC list item.
      -- `item_info` has fields `name`, `link`, `marker`, `indent`, To change the
      -- format of each heading item but keep the same field substitution syntax,
      -- simply change `item_format_string`.
      ---@param item_info table Information for current heading item.
      ---@param fmtstr string from `item_format_string` config
      ---@return string formatted_item
      item_formatter = function(item_info, fmtstr)
        local s = fmtstr:gsub([[${(%w-)}]], function(key) return item_info[key] or ("${" .. key .. "}") end)
        return s
      end,
    },

    -- Table or boolean. Set to true to use these defaults, set to false to disable completely.
    -- Fences are needed for the update/remove commands.
    fences = {
      enabled = true,
      -- These fence texts are wrapped within "<!-- % -->", where the '%' is
      -- substituted with the text.
      start_text = "mtoc-start",
      end_text = "mtoc-end",
      -- An empty line is inserted on top and below the ToC list before the being
      -- wrapped with the fence texts, same as vim-markdown-toc.
    },

    -- Set auto_update=true to use the following defaults.
    -- Set to false to disable completely.
    -- Fields events and pattern are used unprocessed for creating autocmds.
    auto_update = {
      enabled = true,
      -- This allows the ToC to be refreshed silently on save for any markdown file.
      -- The refresh operation uses `Mtoc update` and does NOT create the ToC if
      -- it does not exist.
      events = { "BufWritePre" },
      pattern = "*.{md,mdown,mkd,mkdn,markdown,mdwn}",
    },
  },
}
```

## Usage

以下のようなサンプルのREADME.mdを作ります。

Plain textcontent\_copy

```
# README

## ToC

## Requirements

## Usage

### Sub Commands

#### aaa

#### bbb

#### ccc
```

![ToCの下あたりにカーソルを合わせて・・・の画面](/_astro/image-02.Ba5Kw1r7_2glyku.webp)

ToCの下あたりにカーソルを合わせて・・・

おもむろに以下のコマンドをNeovimに叩き込むと

![おもむろに以下のコマンドをNeovimに叩き込むとの画面](/_astro/image-03.BKsHPXII_ZbjPKL.webp)

Plain textcontent\_copy

```
:Mtoc i
```

ToCが自動生成されて挿入されます😆

![ToCが自動生成されて挿入されます😆の画面](/_astro/image-04.DSP4uGIZ_2r3bGE.webp)

## Auto Update

![Auto Updateの画面（1）](/_astro/image-05.kWKP817O_ZQ18ql.webp)

こんな感じで新しいセクションを追加してNormalモードに抜けて保存すると・・・

![Auto Updateの画面（2）](/_astro/image-06.BSs0-iAJ_19GCPB.webp)

ToCに新しくConfigが追加されます。

先ほど書きましたが以下の設定が必要です。

Plain textcontent\_copy

```
    -- Table or boolean. Set to true to use these defaults, set to false to disable completely.
    -- Fences are needed for the update/remove commands.
    fences = {
      enabled = true,
      -- These fence texts are wrapped within "<!-- % -->", where the '%' is
      -- substituted with the text.
      start_text = "mtoc-start",
      end_text = "mtoc-end",
      -- An empty line is inserted on top and below the ToC list before the being
      -- wrapped with the fence texts, same as vim-markdown-toc.
    },

    -- Set auto_update=true to use the following defaults.
    -- Set to false to disable completely.
    -- Fields events and pattern are used unprocessed for creating autocmds.
    auto_update = {
      enabled = true,
      -- This allows the ToC to be refreshed silently on save for any markdown file.
      -- The refresh operation uses `Mtoc update` and does NOT create the ToC if
      -- it does not exist.
      events = { "BufWritePre" },
      pattern = "*.{md,mdown,mkd,mkdn,markdown,mdwn}",
    },
```

これでとってもREADME.mdが書きやすくなりました。

これなしではもう書けない😂

ターミナル環境について他の記事も書いているので、よろしければこちらもご覧ください！  
[『ターミナルがダサいとモテない』シリーズ一覧](https://blog.grasys.io/tags/series-terminal-update)

転載：[ターミナルがダサいとモテない。NeovimでMarkdown-ToC編](https://medium.com/@yusuke_h/%E3%82%BF%E3%83%BC%E3%83%9F%E3%83%8A%E3%83%AB%E3%81%8C%E3%83%80%E3%82%B5%E3%81%84%E3%81%A8%E3%83%A2%E3%83%86%E3%81%AA%E3%81%84-neovim%E3%81%A7markdown-toc%E7%B7%A8-3e87f2aabf59)

この記事は [Medium（@yusuke\_h）](https://medium.com/@yusuke_h) からの転載です。

## この記事を書いた人

[![](/_astro/yusukeh.BvUd2yKE_1mQ6Fp.webp)](/authors/yusukeh/)

### [yusukeh](/authors/yusukeh/)

Fly high. Think deep. Move fast. Go far.🦅

-   [X](https://x.com/yusukeh "X")
-   [GitHub](https://github.com/yusukeh "GitHub")
-   [Medium](https://medium.com/@yusukeh "Medium")
-   [Facebook](https://www.facebook.com/yusuke.exzm "Facebook")
-   [Instagram](https://www.instagram.com/yusukehasegawa/ "Instagram")
-   [LinkedIn](https://www.linkedin.com/in/hasegawa-yusuke-091b4164/ "LinkedIn")

[プロフィールと記事一覧](/authors/yusukeh/)