Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to prevent autocomplete if Esc is pressed? #438

Closed
sainttttt opened this issue Mar 1, 2024 · 2 comments
Closed

Is there a way to prevent autocomplete if Esc is pressed? #438

sainttttt opened this issue Mar 1, 2024 · 2 comments
Labels
enhancement New feature or request wontfix This will not be worked on

Comments

@sainttttt
Copy link

Hi! I'd like it so that if I press Esc (leaving insert mode) without entering another subsequent character, the autocomplete won't trigger. So for example just entering (<Esc> would end up with ( or ('<Esc> would give me (', but entering ('h<Esc> would give me ('h'). Would this be possible at all? I sort of have this functionality with some basic vim mappings as follows:

imap [<esc> <c-v>[<esc>
imap ['<esc> <c-v>[<c-v>'<esc>
imap ["<esc> <c-v>[<c-v>"<esc>
imap ']<esc> <c-v>'<c-v>]<esc>
imap "]<esc> <c-v>"<c-v>]<esc>

imap {<esc> <c-v>{<esc>
imap {'<esc> <c-v>{<c-v>'<esc>
imap {"<esc> <c-v>{<c-v>"<esc>
imap '}<esc> <c-v>'<c-v>}<esc>
imap "}<esc> <c-v>"<c-v>}<esc>

imap (<esc> <c-v>(<esc>
imap ('<esc> <c-v>(<c-v>'<esc>
imap ("<esc> <c-v>(<c-v>"<esc>
imap ')<esc> <c-v>'<c-v>)<esc>
imap ")<esc> <c-v>"<c-v>)<esc>

imap '<esc> <c-v>'<esc>
imap "<esc> <c-v>"<esc>

imap "" <c-v>"<c-v>"

But the main issue being is that for multiple char imappings, it doesn't show all the characters on the screen as you type, but just one at a time, which is a bit jarring. Any help would be appreciated.

Thanks!

@sainttttt sainttttt added the enhancement New feature or request label Mar 1, 2024
@Sam-programs
Copy link
Contributor

There isn't really a nice way to prevent the autocomplete from happening for Esc, but you could instead make Esc delete end pairs if the pairs are empty.

-- lua
local is_pair = {
    ['{}'] = true,
    ['[]'] = true,
    ['\'\''] = true,
    ['\"\"'] = true,
    ['[\'\']'] = true,
    ['[\"\"]'] = true,
    ['{\'\'}'] = true,
    ['{\"\"}'] = true,
}
vim.keymap.set('i', '<esc>', function()
    local line = vim.api.nvim_get_current_line()
    local col = vim.api.nvim_win_get_cursor(0)[2] + 1
    -- {""}
    --   |
    -- The cursor is |
    if is_pair[line:sub(col - 2, col + 1)] then
        -- del deletes the character on the cursor
        return '<del><del><esc>'
    end
    -- {}
    --  |
    if is_pair[line:sub(col - 1, col)] then
        return '<del><esc>'
    end
    return '<esc>'
end, { expr = true })

Copy link

stale bot commented May 1, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 1, 2024
@stale stale bot closed this as completed May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants