telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

Unable to map `<CR>` correctly for `actions.select_default`

Open akhilsbehl opened this issue 2 years ago • 12 comments

Description

Description

I'd like to change the default mappings to what I like. The one default I'd like to keep is to use <CR> to select default action. However, the behavior of this mapping is non-deterministic (especially given the other mappings - I suspect <Tab> and <S-Tab> are in play).

Sometimes, <CR> works as default action (less often) and at other times works as marking a selection in multi-select (more often). This happens with several builtins that allow multi-select.

However, for builtins where multiselect is not an option, such as commandhistory, <CR> works reliably.

I've had to map <C-e> as a backup mapping since <CR> doesn't work reliably.

Neovim version

NVIM v0.8.3
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Operating system and version

ArchLinux (on WSL2)

Telescope version / branch / rev

Branch - 0.1.x

checkhealth telescope


## OS Info:
{
  machine = "x86_64",
  release = "5.15.90.1-microsoft-standard-WSL2",
  sysname = "Linux",
  version = "#1 SMP Fri Jan 27 02:56:13 UTC 2023"
}

telescope: require("telescope.health").check()
========================================================================
## Checking for required plugins
  - OK: plenary installed.
  - OK: nvim-treesitter installed.

## Checking external dependencies
  - OK: rg: found ripgrep 13.0.0
  - OK: fd: found fd 8.7.0

## ===== Installed extensions =====

## Telescope Extension: `fzf`
  - OK: lib working as expected
  - OK: file_sorter correctly configured
  - OK: generic_sorter correctly configured

## Telescope Extension: `ultisnips`
  - INFO: No healthcheck provided

Steps to reproduce

  1. Start nvim with the above config
  2. :Telescope commands and press <CR>
  3. Exit neovim. Do step above again.

Repeat loop 3 - 4 times.

Expected behavior

Should always behave as select_default for single-select and multi-select contexts.

Actual behavior

<CR> will sometimes act as select_default and at other times as mark selection.

Minimal config

VC = vim.cmd
VF = vim.fn

local ensure_packer = function()
    local install_path = VF.stdpath('data') ..
        '/site/pack/packer/start/packer.nvim'
    if VF.empty(VF.glob(install_path)) > 0 then
        VF.system({
            'git', 'clone', '--depth', '1',
            'https://github.com/wbthomason/packer.nvim', install_path
        })
        VC [[packadd packer.nvim]]
        return true
    end
    return false
end

local packer_bootstrap = ensure_packer()

require('packer').startup(function(use)
    use 'wbthomason/packer.nvim' -- Package manager

    use { -- Fuzzy finder
        'nvim-telescope/telescope.nvim',
        branch = '0.1.x',
        requires = { { 'nvim-lua/plenary.nvim' } },
        config = function()
            local actions = require('telescope.actions')
            local qfix = actions.smart_send_to_qflist + actions.open_qflist
            require('telescope').setup({
                defaults = {
                    default_mappings = {
                        i = {
                            ["<C-/>"]      = actions.which_key,
                            ["<C-_>"]      = actions.which_key,
                            ["<C-e>"]      = actions.select_default,
                            ["<C-h>"]      = actions.preview_scrolling_down,
                            ["<C-j>"]      = actions.nop,
                            ["<C-l>"]      = actions.preview_scrolling_up,
                            ["<C-m>"]      = actions.toggle_selection,
                            ["<C-n>"]      = actions.results_scrolling_down,
                            ["<C-p>"]      = actions.results_scrolling_up,
                            ["<C-q>"]      = qfix,
                            ["<C-s>"]      = actions.select_horizontal,
                            ["<C-t>"]      = actions.select_tab,
                            ["<C-v>"]      = actions.select_vertical,
                            ["<C-w>"]      = actions.nop,
                            ["<C-x>"]      = actions.nop,
                            ["<Down>"]     = actions.nop,
                            ["<Esc>"]      = actions.close,
                            ["<M-q>"]      = actions.nop,
                            ["<PageDown>"] = actions.nop,
                            ["<PageUp>"]   = actions.nop,
                            ["<S-Tab>"]    = actions.move_selection_next,
                            ["<Tab>"]      = actions.move_selection_previous,
                            ["<Up>"]       = actions.nop,
                            ["<cr>"]       = actions.select_default, -- buggy
                        },
                    },
                },
            })
        end,
    }

    if packer_bootstrap then
        require('packer').sync()
    end

end)

akhilsbehl avatar Mar 07 '23 14:03 akhilsbehl

I'm unable to reproduce this issue using your provided config on neovim v0.8.3. I tried <cr> about 40+ times and it's behaving as expected. If you're getting this issue using the provided config, this feels like like a terminal emulator issue :man_shrugging:

On a related note, you probably don't want to use defaults = { default_mappings = { ... } } for settings mappings. As per docs:


                                      *telescope.defaults.default_mappings*
    default_mappings:  
        Not recommended to use except for advanced users.

        Will allow you to completely remove all of telescope's default maps
        and use your own.

        Default: nil

You can use defaults = { mappings = { ... } }.

jamestrew avatar Mar 08 '23 00:03 jamestrew

Thanks for looking into this.

I'm using this on WSL2 with Windows Terminal so may be it is a terminal issue. I'll see if can check with something else and report back.

BTW, when you say you tried 40 times, did you try in the same vim session or did you try to close and reopen vim? Asking because this issue tends to happen randomly but not always.

Lastly, thanks for your side note, I used default_mappings specifically to rule out default maps may have been causing this problem.

akhilsbehl avatar Mar 17 '23 10:03 akhilsbehl

I was re-opening vim each time. Maybe try :imap <CR> with Telescope open and see if you occasionally see multiple or strange mappings being attached to <CR>.

jamestrew avatar Mar 17 '23 17:03 jamestrew

Please, specify which pickers you are using. E.g. recent_files, buffers etc...

hinell avatar Apr 21 '23 14:04 hinell

These are the ones I use:

git_files
find_files
buffers
help_tags
oldfiles
commands
colorscheme
tags
command_history
search_history
marks
quickfix
quickfixhistory
loclist
jumplist
vim_options
registers
keymaps
highlights
resume
pickers
pickers
ultisnips

akhilsbehl avatar Apr 21 '23 21:04 akhilsbehl

@akhilsbehl I mean, when you encounter the issue, not the liste of ALL pickers.

hinell avatar Apr 22 '23 04:04 hinell

Thanks for looking into this and sorry for the late reply.

The pickers where I can confirm that this happens:

git_files
find_files
buffers
help_tags
commands
colorscheme
marks
quickfix
quickfixhistory
jumplist
vim_options
registers
keymaps
highlights
pickers
ultisnips

The pickers where I can confirm this does not happen:

command_history
search_history

The pickers that I could not test due to my setup:

tags
loclist

akhilsbehl avatar Apr 24 '23 21:04 akhilsbehl

#2506 should explain why you didn't have any issues with command_history and search_history up to this point, though unfortunately it looks like after that fix those pickers won't work for you either anymore... But at least I think that means that you aren't facing a terminal emulator issue, because if you were ever sending some key other than <CR>, those pickers certainly wouldn't have worked.

So, indeed, you seem to be facing an issue of <CR> not consistently triggering actions.select_default. Though I'm not sure what could cause this other than the mapping being overridden elsewhere in your config.

rish987 avatar May 10 '23 12:05 rish987

Oh wow, that's an interesting find. It helped me debug that the issue is with 'telescope-fzf-native.nvim' plugin which is silently remapping that key.

It doesn't say any such thing in its documentation (double checked) and I could not find any key map configuration in their code either.

Simple fix was to have the fzf extension load first and then load telescope config which remaps to my preferred keys.

I think we can mark this as closed. Thanks so much for the help.

akhilsbehl avatar May 10 '23 20:05 akhilsbehl

Looking back at my bug report, I could swear I tested without the fzf plugin but I probably wasn't completely disciplined with clearing the compiled Lua code when testing with config changes because of which the issue was appearing sometimes and sometimes not.

I'll close this issue now.

akhilsbehl avatar May 10 '23 20:05 akhilsbehl

Spoke too soon. The bug is still there and is still stochastic. The CR mapping keeps changing from session to session between marking an option or acting on an option. For the first time I saw it change within the same session too.

I'm not even able to debug because I don't know how to reproduce deterministic behavior. :(

And I have no idea how to even debug anymore.

akhilsbehl avatar May 10 '23 22:05 akhilsbehl

Hi, I've hit the same issue. Unfortunately, it's still there 😞 I also don't like when plugins overload mappings behind the scene and want only those that I explicitly configured. I don't know how to fix the real issue, but have worked around it by using false value for all default mappings (except <CR>). Obviously, it's far from ideal, but you can use something like this:

--- Create a table that explicitly tells Telescope not to override any of its default mappings.
local disabled_mappings = {}
for mode, mappings in pairs(require("telescope.mappings").default_mappings) do
    disabled_mappings[mode] = {}
    for key, _ in pairs(mappings) do
        --- FIXME: Assigning "actions.select_default" to <CR> doesn't work, but original mapping does.
        ---        https://github.com/nvim-telescope/telescope.nvim/issues/2417
        if key ~= "<CR>" then
            disabled_mappings[mode][key] = false
        end
    end
end

--- Create a table with desired mappings.
local desired_mappings = {
    ["i"] = {
        --- ["<CR>"] = actions.select_default, --- This one is kept from default Telescope mappings.
        ---
    },
    ["n"] = {
        --- ["<CR>"] = actions.select_default, --- This one is kept from default Telescope mappings.
        ---
    }
}

require("telescope").setup({
    defaults = {
        --- Use desired mappings along with remaining indicators for Telescope not to override other stuff.
        mappings = vim.tbl_deep_extend("force", disabled_mappings, desired_mappings),
    },
})

Edit It serves me right for trying cleaner approaches while writing a comment 😅 I have updated the comment with the code that actually works consistently for me. Nevertheless, this finding might help someone. Even assigning the original require("telescope.mappings").default_mappings["i"]["<CR>"] results in same issue.

Iskustvo avatar Jun 06 '25 00:06 Iskustvo