Cookbook - mfussenegger/nvim-dap Wiki

Configuration Cookbook

This page is for various configuration snippets that might be useful.

Pick a process

Some debug adapters support attaching to a running process. If you want to have a "pick pid" dialog, you can use the pick_process utils function in your configuration. For example, in a configuration using the lldb-vscode debug adapter, it can be used like this:

dap.configurations.cpp = {
    {
      -- If you get an "Operation not permitted" error using this, try disabling YAMA:
      --  echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
      name = "Attach to process",
      type = 'cpp',  -- Adjust this to match your adapter name (`dap.adapters.<name>`)
      request = 'attach',
      pid = require('dap.utils').pick_process,
      args = {},
    },
}

See :help dap-configuration for more information about nvim-dap configuration.

Map K to hover while session is active.

local dap = require('dap')
local api = vim.api
local keymap_restore = {}
dap.listeners.after['event_initialized']['me'] = function()
  for _, buf in pairs(api.nvim_list_bufs()) do
    local keymaps = api.nvim_buf_get_keymap(buf, 'n')
    for _, keymap in pairs(keymaps) do
      if keymap.lhs == "K" then
        table.insert(keymap_restore, keymap)
        api.nvim_buf_del_keymap(buf, 'n', 'K')
      end
    end
  end
  api.nvim_set_keymap(
    'n', 'K', '<Cmd>lua require("dap.ui.variables").hover()<CR>', { silent = true })
end

dap.listeners.after['event_terminated']['me'] = function()
  for _, keymap in pairs(keymap_restore) do
    api.nvim_buf_set_keymap(
      keymap.buffer,
      keymap.mode,
      keymap.lhs,
      keymap.rhs,
      { silent = keymap.silent == 1 }
    )
  end
  keymap_restore = {}
end

Reload dap.configuration before starting a debug session

If you configure the dap.configurations table in a Lua module and load that module via a require call, the module gets cached. If you then modify the module with the configurations the changes won't be picked up automatically.

There are two options to force load the changes:

1. Read the file via :luafile:

You could add something like autocmd BufWritePost ~/your_dap_config_file :luafile % in init.vim.

2. Clear the Lua package cache and reload the configuration module:

Assuming the following conditions:

local M = {}
local dap =  require'dap'

function M.reload_continue()
  package.loaded['dap_config'] = nil
  require('dap_config')
  dap.continue()
end

local opts = { noremap=false, silent=true }

-- <Leader>ec to continue
vim.api.nvim_buf_set_keymap( 0, 'n', '<Leader>ec',
       '<cmd>lua require"dap".continue()<CR>', opts)

-- <Leader>eC to reload and then continue
vim.api.nvim_buf_set_keymap(0, 'n', '<Leader>eC',
    '<cmd>lua require"dap_setup".reload_continue()<CR>', opts)

return M