vim - VIM mapping not fully repeating
问题描述
I've added the following lines to my vimrc in order to quickly comment lines of code:
augroup cmnts
autocmd FileType c, cpp, javascript nnoremap <buffer> <leader>c
I//<esc>j
autocmd FileType python nnoremap <buffer> <leader>c I#<esc>j
augroup END
My expectation was that when I repeat the mapping with a number, that number of lines would be commented, but instead it just adds the comment character multiple times.
For example in Python, When I type Hc Hc Hc (H is my leader key) it comments three lines, but when I type 3Hc I get ### at the start of my current line.
解决方案
要进行计数,您将切换到使用:normal
Ex命令。对于 ruby/python,这将如下所示:
nnoremap <leader>c :normal I# <CR>
此外,vim 知道大多数语言和文件类型的注释字符串。因此,您不必明确告诉每种语言使用什么作为注释字符串,而是使用如下内容:
nnoremap <leader>c :call CommentLine()<CR>
function! CommentLine()
let comment_character = split(&commentstring, '%s')
exec 'normal I' . comment_character[0] . ' '
endfunction
推荐阅读
- python - Scrapy/Selenium - driver.get_cookies() 返回错误的 cookie(对于与 start_urls 不同的 url)
- flutter - 为什么缺少 awsconfiguration.json 文件?
- python - 如何运行具有特定持续时间的函数?
- javascript - 同一页面中两个元素之间的链接不起作用
- python - 连接到 Python websockets 时出现 Ktor MalformedInputException
- python - 从 Google 云存储桶运行 Eleuther AI GPT 模型
- smooth-scrolling - 加载巨大列表时 Flutter ListView.separated 帧丢失
- c# - docker api 和 web 应用程序容器的问题
- php - jetstream 不更新功能(在 config/jetstream.php 上)
- python - 为什么函数会出错?不和谐.py