Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将vim变量插入注释快捷方式的文本中_Vim_Variables_Comments_Autocmd - Fatal编程技术网

将vim变量插入注释快捷方式的文本中

将vim变量插入注释快捷方式的文本中,vim,variables,comments,autocmd,Vim,Variables,Comments,Autocmd,我有一个简单的目标:mapctrl-C,我想我从来没有使用过这个命令来杀死vim,它可以自动在一行的开头插入正确的字符,根据文件的文件类型注释掉该行 我想我可以使用自动命令识别文件类型,并在文件打开时将vim变量设置为正确的注释字符。所以我尝试了以下方法: " Control C, which is NEVER used. Now comments out lines! autocmd BufNewFile,BufRead *.c let CommentChar = "//" autocmd B

我有一个简单的目标:mapctrl-C,我想我从来没有使用过这个命令来杀死vim,它可以自动在一行的开头插入正确的字符,根据文件的文件类型注释掉该行

我想我可以使用自动命令识别文件类型,并在文件打开时将vim变量设置为正确的注释字符。所以我尝试了以下方法:

" Control C, which is NEVER used. Now comments out lines!
autocmd BufNewFile,BufRead *.c let CommentChar = "//"
autocmd BufNewFile,BufRead *.py let CommentChar = "#"
map <C-C> mwI:echo &CommentChar<Esc>`wll
“控件C,从未使用过。现在注释掉行!
autocmd BufNewFile,BufRead*.c let CommentChar=“//”
autocmd BufNewFile,BufRead*.py let CommentChar=“#”
地图mwI:echo和CommentChar`wll
该映射标记了我的当前位置,在插入模式下转到行的开头,在该点回显注释字符,进入命令模式,返回到设置标记,并向右移动两个字符以弥补插入的注释字符(假设为C样式注释)

斜体部分是我遇到麻烦的部分;它只是作为一个占位符来代表我想要做的事情。你能帮我弄清楚如何做到这一点吗?如果你使用strlen(CommentChar)可以获得额外的积分向右移动正确的空格数!如果您处于视觉模式,vim master将获得额外的加分,包括如何进行块样式注释

我对vim脚本编写还是相当陌生的;我的.vimrc仅有98行,因此,如果您能帮助我解释您提供的任何答案!谢谢。

您可以在这里使用

noremap <C-c> mwI<C-r>=g:CommentChar<CR><Esc>`wll

而且你不必自己定义注释字符。

我在某个时候从vim tips wiki上取下了这个,自己使用它。唯一的缺点是,出于某种原因,它在行的末尾添加了一个空格,可能是我忽略了一些小东西

" Set comment characters for common languages
autocmd FileType python,sh,bash,zsh,ruby,perl,muttrc let StartComment="#" | let EndComment=""
autocmd FileType html let StartComment="<!--" | let EndComment="-->"
autocmd FileType php,cpp,javascript let StartComment="//" | let EndComment=""
autocmd FileType c,css let StartComment="/*" | let EndComment="*/"
autocmd FileType vim let StartComment="\"" | let EndComment=""
autocmd FileType ini let StartComment=";" | let EndComment=""

" Toggle comments on a visual block
function! CommentLines()
    try
        execute ":s@^".g:StartComment." @\@g"
        execute ":s@ ".g:EndComment."$@@g"
    catch
        execute ":s@^@".g:StartComment." @g"
        execute ":s@$@ ".g:EndComment."@g"
    endtry
endfunction

" Comment conveniently
vmap <Leader>c :call CommentLines()<CR>
“为通用语言设置注释字符
autocmd文件类型python、sh、bash、zsh、ruby、perl、muttrc let StartComment=“#”| let EndComment=“”
autocmd文件类型html let StartComment=“”
autocmd文件类型php、cpp、javascript let StartComment=“/”| let EndComment=“”
autocmd文件类型c,css let StartComment=“/*”| let EndComment=“*/”
autocmd文件类型vim let StartComment=“\”\124; let EndComment=“”
autocmd文件类型ini let StartComment=“;“|让EndComment=“”
“在可视块上切换注释
功能!注释行()
尝试
执行“:s@^”.g:StartComment.@\@g”
执行“:s@”.g:EndComment.$@@g”
抓住
执行“:s@^@”.g:StartComment.@g”
执行“:s@$@”.g:EndComment.@g”
末日
端功能
“随口说说
vmap c:调用CommentLines()

寄存器非常棒:)谢谢你解释这一切。我读过关于I\u CTRL-R的文章,但没有理解它。感谢你,我让我的小脚本开始工作(但将
wll改为
w:exe“normal”。strlen(CommentChar)。“l”让光标回到正确的位置)。也感谢你,我放弃了我的脚本,现在正在使用NERDCommenter!但至少在这个过程中我学到了更多关于vim的知识。感谢StartComment和EndComment的思维方式。这对其他任务也很有用。希望我能将此更新两次!顺便说一句,末尾的空格是由于在执行前的执行行中的硬编码空格造成的在@character后面加上nd。只需将它们去掉,如果您愿意为实际的StartComment和EndComment变量添加补偿空格,我发现有一个小的改进很有用:使用b:StartComment和b:EndComment(用于初始化和使用)。这允许您在不同的缓冲区中具有不同的值。如果打开了多个使用不同注释字符的文件类型,这一点很重要。
" Set comment characters for common languages
autocmd FileType python,sh,bash,zsh,ruby,perl,muttrc let StartComment="#" | let EndComment=""
autocmd FileType html let StartComment="<!--" | let EndComment="-->"
autocmd FileType php,cpp,javascript let StartComment="//" | let EndComment=""
autocmd FileType c,css let StartComment="/*" | let EndComment="*/"
autocmd FileType vim let StartComment="\"" | let EndComment=""
autocmd FileType ini let StartComment=";" | let EndComment=""

" Toggle comments on a visual block
function! CommentLines()
    try
        execute ":s@^".g:StartComment." @\@g"
        execute ":s@ ".g:EndComment."$@@g"
    catch
        execute ":s@^@".g:StartComment." @g"
        execute ":s@$@ ".g:EndComment."@g"
    endtry
endfunction

" Comment conveniently
vmap <Leader>c :call CommentLines()<CR>