简体   繁体   English

配置Vim为代码行插入文本

[英]Configure Vim to insert text for Lines of Code

I'm using Vim for all program editing and I have a standard header I use at the top of all my source code files. 我正在使用Vim进行所有程序编辑,并且在所有源代码文件的顶部都使用了标准标头。 I have a .vimrc file set up to update certain fields in this header (like Last Modified) when I save any changes using :w 当我使用:w保存任何更改时,我设置了一个.vimrc文件来更新此标头中的某些字段(例如Last Modified)。

My question is, how do I put in a function to count lines of code, following the basic rule that only non-blank lines are counted? 我的问题是,如何遵循仅计数非空白行的基本规则,如何放入一个函数来计算代码行?

I know within an open vim buffer, I can use 我知道在打开的vim缓冲区中,我可以使用

:%s/\n//gn 

to count all lines, and 计算所有行,以及

:%s/\n\n//gn 

to count blank lines (basically count how many times two newlines appear in a row, indicating a blank line). 计算空白行(基本上是计算一行中出现两个换行符的次数,表示有一个空白行)。 But how do I put this in my .vimrc file? 但是,如何将其放入我的.vimrc文件中?

Here's the code fragment from my .vimrc that updates the header fields: 这是我的.vimrc中的代码片段,用于更新标头字段:

function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([20, line("$")])
    keepjumps exe '1,' . n . 's#^\(.\{,10}Last Modified:\).*#\1' .
          \ strftime(' %a %b %d, %Y  %I:%M%p') . '#e'
    keepjumps exe '1,' . n . 's#^\(.\{,10}Filename:\).*#\1' .
          \ ' ' . @% . '#e'
    keepjumps exe '1,' . n . 's#^\(.\{,10}LOC:\).*#\1' .
          \ ' ' . '' . '#e'
    call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun

Also, I would just like to add, I know there are numerous other ways to do this (like using wc --lines from the shell) but I'm interested in learning how to really configure my editor (so call it a learning exercise). 另外,我想补充一下,我知道还有很多其他方法可以做到这一点(例如从外壳中使用wc --lines),但是我有兴趣学习如何真正配置编辑器(因此称之为学习练习) )。

You actually should not want to use :s here: 您实际上不应该在这里使用:s

function! CountNonEmpty()
    return len(filter(getline(1, line('$')), '!empty(v:val)'))
endfunction

By the way, I would have used getline+map+setline to implement your header updater: 顺便说一句,我会使用getline + map + setline来实现您的标头更新程序:

function! LastModified()
    if &modified
        " If number of buffer lines is < 20, then getline(1, 20)"
        " will return only existing lines without any errors "
        call setline(1, map(getline(1, 20), 'substitute(substitute(substitute(v:val, '.
        \'"^\\v(.{,10}Last Modified:).*", "\\1 ".strftime("%s %b %d, %Y  %I:%M%p"), ""),'.
        \'"^\\v(.{,10}Filename:).*",      "\\1 ".escape(@%, "&\\~"), ""),'.
        \'"^\\v(.{,10}LOC:).*",           "\\1 ", "")'))
    endif
endfunction

This might help: 这可能会有所帮助:

function! CountNonEmpty()
  redir => g:nonblank
  silent %s/^.\+$/&/n
  redir END
  return substitute(g:nonblank, '\n\s*\(\d\+\)\D.*$', '\1', '')
endfunction
  • :redir => Stores the output of the following ex commands into the given variable. :redir =>将以下ex命令的输出存储到给定变量中。 See :help :redir 参见:help :redir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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