简体   繁体   中英

Why can't I change a coc/neoclide key binding in Vim 8.1?

I love having a hotkey to format all of the code I'm using. I'd prefer having it on the lower case "f" button. I tried imitating the code given in the default config (the one which creates a new "Format" command) to bind it to "f", but it doesn't work. Here's the relevant except from my .vimrc:

" Formatting selected code.
"original code commented out
"xmap <leader>f  <Plug>(coc-format-selected)
"nmap <leader>f  <Plug>(coc-format-selected)

"these bindings do not work
xmap <leader>f  :call CocActionAsync('format')
nmap <leader>f  :call CocActionAsync('format')
nnoremap <leader>f  :call CocActionAsync('format') 

"This does however work.... strangely:
" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocActionAsync('format')

As a side note, what counts as "selected" for neoclide? I tried using visual selection, but that doesn't seem to work. Here's the link to the original config: https://github.com/neoclide/coc.nvim#example-vim-configuration

Many thanks!

So the answer is actually quite simple. Two problems were being generated: The first one comes from emulation, which basically disabled most of the useful function keys for vim, including CTRL, the START key and others... that made debugging unnecessarily hard. The second one was me confusing the keybinding syntax. The only thing that seemed to work was to use the existing :Format command and apply it so shift-f:

nmap F :Format<CR>

That's all. I can't imagine that this solution would've taken more than .01% brainpower of anyone experienced with the software. My initial attempts at fixing the problem failed due to the aforementioned emulation problems - I had posted the original not working code in order not to make things too confusing. Even though the problem is solved now, there is a certain bitterness remaining. Assuming people aren't using software "properly" just because they are learning or do not have your proficiency level seems a very inadequate thing to do. I'd bet that most programmers started by learning from small examples instead of reading a 2000-page documentation first. If you don't want to help, remain silent. I always try to help with what little knowledge I have in, say, C# or TS, or Latin, Spanish or Czech for that matter. Have a good night.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM