Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Linux vim功能“;更新时间戳";“关于事件”;BufWrite“是的,我是:有待改进_Linux_Unix_Vim_File Io - Fatal编程技术网

Linux vim功能“;更新时间戳";“关于事件”;BufWrite“是的,我是:有待改进

Linux vim功能“;更新时间戳";“关于事件”;BufWrite“是的,我是:有待改进,linux,unix,vim,file-io,Linux,Unix,Vim,File Io,该用例由全世界所有(vim)开发人员共享(IMHO):我们希望在源文件中的每个写操作上更新一个标记和一个时间戳。在Barry Arthur的帮助下,我的尝试是: 每次写入时要更新的字符串示例 #2013-01-09 01:04:31.0+0100/Me vimrc代码 “如果未映射,:x不调用UpdateTimestamp() 地图:x:wq 函数!UpdateTimestamp() 让old_pos=getpos('.')) 让old_search=histget(“search”,-1)

该用例由全世界所有(vim)开发人员共享(IMHO):我们希望在源文件中的每个写操作上更新一个标记和一个时间戳。在Barry Arthur的帮助下,我的尝试是:

每次写入时要更新的字符串示例
#2013-01-09 01:04:31.0+0100/Me
vimrc代码
“如果未映射,:x不调用UpdateTimestamp()
地图:x:wq
函数!UpdateTimestamp()
让old_pos=getpos('.'))
让old_search=histget(“search”,-1)
g/^\(\\\\\\\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
exe“normal/”。旧的搜索
调用设置位置('.',旧位置)
端功能
au BufWrite*调用UpdateTimestamp()
未解决的问题
  • 更新标记时不应修改撤消历史记录(如果可能)
  • 写入时屏幕的位置会发生变化
  • 当找不到模式时,屏幕上会显示一些不需要的错误
问题 简单:如何解决这些问题?

  • RE:更新标签时不应修改历史记录
    你的意思是撤销历史记录,对吗?这与Vim的设计背道而驰。每一个更改都需要表示出来。你能做的最好的事情就是
    :撤销加入
    ,但我会发现这令人困惑

  • RE:屏幕在写入时的位置发生变化
    您需要使用
    winsaveview()
    /
    winrestview()
    ,而不仅仅是
    setpos()

  • RE:当找不到图案时,屏幕上会显示一些不需要的错误
    :substitute
    末尾传递
    /e
    标志,并将
    :silent!
    前置到
    :global

附言:我想vim.org上有一些插件可以满足你的需要。你试过其中一些吗?

  • RE:更新标签时不应修改历史记录
    你的意思是撤销历史记录,对吗?这与Vim的设计背道而驰。每一个更改都需要表示出来。你能做的最好的事情就是
    :撤销加入
    ,但我会发现这令人困惑

  • RE:屏幕在写入时的位置发生变化
    您需要使用
    winsaveview()
    /
    winrestview()
    ,而不仅仅是
    setpos()

  • RE:当找不到图案时,屏幕上会显示一些不需要的错误
    :substitute
    末尾传递
    /e
    标志,并将
    :silent!
    前置到
    :global


PS:我想vim.org上有一些插件可以满足您的需要。您尝试过其中一些吗?

您根本不需要保存/恢复位置,也不需要使用
setpos()
winrestview()
:您不能移动光标。也不需要使用技巧来保存/恢复搜索:使用
search()
函数代替
:g
setline()
代替
s/*/\=

function! UpdateTimestamp()
  " Removed “.*” from the end of the pattern because it is pointless
  " Also made it use very-magic mode because otherwise it looks bad (too many escapes)
  " Also replaced \d\{2\} with \d\d because \d\d takes less characters
  let lnr=search('\v^(\#|\/\/)\s+\d{4}\-\d\d\-\d\d\s+\d\d\:\d\d\:\d\d\.\d+\s+\+\d{4}\s+\/\s+\VMe <me@domain.tld>', 'wn')
  if lnr
    " Matchstr gets comment leader. Above regex intended to work with # or // comments, but \=expression supported only the former, this got fixed
    call setline(lnr, matchstr(getline(lnr), '^\S\+')." " . strftime('%F %H:%M:%S.0 %z') . " / Me <me@domain.tld>")
  endif
endfunction
function!UpdateTimestamp()
已从模式末尾删除“.*”,因为它没有意义
“还使它使用非常神奇的模式,因为否则它看起来很糟糕(太多的逃逸)
“还将\d\{2\}替换为\d\d,因为\d\d使用更少的字符
让lnr=search('\v^(\\\\\\\/\/)\s+\d{4}-\d\d\d\s+\d\d\:\d\d\:\d\d\。\d+\s+\+\d{4}\s+\/\s+\VMe',wn')
如果lnr
“Matchstr获取注释头。上面的正则表达式打算使用#或//注释,但是\=表达式只支持前者,这得到了修复
调用setline(lnr,matchstr(getline(lnr),“^\S\+”)。“”.strftime(“%F%H:%M:%S.0%z”)。“/Me”)
恩迪夫
端功能

。注意:您的解决方案和我的解决方案还有一个区别:这里只更新了一个时间戳。这个问题可以解决。

您根本不需要保存/恢复位置,也不需要使用
setpos()
winrestview()
:您不能移动光标。保存/恢复搜索也不需要技巧:使用
search()
函数代替
:g
setline()
代替
s/*/\=

function! UpdateTimestamp()
  " Removed “.*” from the end of the pattern because it is pointless
  " Also made it use very-magic mode because otherwise it looks bad (too many escapes)
  " Also replaced \d\{2\} with \d\d because \d\d takes less characters
  let lnr=search('\v^(\#|\/\/)\s+\d{4}\-\d\d\-\d\d\s+\d\d\:\d\d\:\d\d\.\d+\s+\+\d{4}\s+\/\s+\VMe <me@domain.tld>', 'wn')
  if lnr
    " Matchstr gets comment leader. Above regex intended to work with # or // comments, but \=expression supported only the former, this got fixed
    call setline(lnr, matchstr(getline(lnr), '^\S\+')." " . strftime('%F %H:%M:%S.0 %z') . " / Me <me@domain.tld>")
  endif
endfunction
function!UpdateTimestamp()
已从模式末尾删除“.*”,因为它没有意义
“还使它使用非常神奇的模式,因为否则它看起来很糟糕(太多的逃逸)
“还将\d\{2\}替换为\d\d,因为\d\d使用更少的字符
让lnr=search('\v^(\\\\\\\/\/)\s+\d{4}-\d\d\d\s+\d\d\:\d\d\:\d\d\。\d+\s+\+\d{4}\s+\/\s+\VMe',wn')
如果lnr
“Matchstr获取注释头。上面的正则表达式打算使用#或//注释,但是\=表达式只支持前者,这得到了修复
调用setline(lnr,matchstr(getline(lnr),“^\S\+”)。“”.strftime(“%F%H:%M:%S.0%z”)。“/Me”)
恩迪夫
端功能

。注意:您的解决方案和我的解决方案之间仍然存在一个差异:这里只更新了一个时间戳。此问题可以解决。

谢谢提示,不,我没有看到这些,但我将进行搜索。我仍然有“E486:Motif Intruvable”(模式未找到错误)使用
/e
修饰符在
/s///e
正则表达式的末尾。啊,很抱歉,您还需要
:silent!g/..
您还可以使用Vim中的
try…catch…finally…endtry
构造来处理错误。@Daan:是的,但这里我们要忽略错误。这意味着将每个命令包装在自己的try..catc中h以避免剩余的命令没有被中止。感谢提示,不,我没有看到这些,但我将进行搜索。我仍然有“E486:Motif Intruvable”(pattern not found error)使用
/e
修饰符在
/s///e
正则表达式的末尾。啊,很抱歉,您还需要
:silent!g/..
您还可以使用Vim中的
try…catch…finally…endtry
构造来处理错误。@Daan:是的,但这里我们要忽略错误。这意味着将每个命令包装在自己的try..catc中H
function! UpdateTimestamp()
  " Removed “.*” from the end of the pattern because it is pointless
  " Also made it use very-magic mode because otherwise it looks bad (too many escapes)
  " Also replaced \d\{2\} with \d\d because \d\d takes less characters
  let lnr=search('\v^(\#|\/\/)\s+\d{4}\-\d\d\-\d\d\s+\d\d\:\d\d\:\d\d\.\d+\s+\+\d{4}\s+\/\s+\VMe <me@domain.tld>', 'wn')
  if lnr
    " Matchstr gets comment leader. Above regex intended to work with # or // comments, but \=expression supported only the former, this got fixed
    call setline(lnr, matchstr(getline(lnr), '^\S\+')." " . strftime('%F %H:%M:%S.0 %z') . " / Me <me@domain.tld>")
  endif
endfunction