2013-07-02 92 views
5

我喜欢vim中的'.命令。从:help '.防止Vim记住更改

'. `. 

[跳转到]其中最后做出更改的位置。该位置在变化开始的位置或附近。

好的。但这是我的问题:我使用autocmd函数在我的文件头中添加“最后修改”行。所以,在每次写入之后,'.都不会让我的“真正”最后更改,而是我的文件头。我目前的解决方案是尝试记住用ma标记我当前的编辑点,以便我可以'a返回到它。尽管我有时会忘记,甚至当我记得时,这又是一对情侣的按键。

我的理想解决方案是某种命令,告诉vim不要记住动作。我可以在autocmd函数跳过之前发送此命令,写入最后一个修改过的行,然后在autocmd函数完成后取消它。这样,与'.相关的位置就不会改变。不过,我愿意接受任何其他更高效的选择。

如果你想看到它,这里是autocmd:w

function! UpdateHeader() 
    let b:winview = winsaveview() 

    " This is where I'd put the command to ignore future movements 

    "The periods concatenate all the arguments into one command. 
    "Silent! suppresses errors, usually 'pattern not found' 
    "The 1,6g means search only lines 1 thru 6 
    "Search for File Name: followed by anything 
    "'s'ubstitute 
    "Substitute in 'File Name: ' and the results of the expand command, on the 
    "current filename 
    execute "silent! 1," . 6 . "g/File Name:.*/s//File Name: " . expand("%") 
    execute "silent! 1," . 6 . "g/Last Modified:.*/s//Last Modified: " . strftime("%d-%m-%Y") 

    " This is where I'd put the command to start remembering movements again 

    call winrestview(b:winview) 
endfunction 

回答

4

您可以在您的autocmd中使用:keepjumps {command}

参见:help :keepjumps

+0

完美。谢谢。 – ravron

+0

任何想要实现这个解决方案的人都会注意到:*文本*实际修改发生时需要'keepjumps'命令* - 即':g'命令运行的':s'命令:'execute'沉默!1,“。 6。 “g /文件名:。*/keepjumps s //文件名:”。扩展( “%”)'。 – ravron

+0

还有一点需要注意:在前面的注释中写入的命令确实会阻止设置“。”。但是,它仍然增加了跳转列表。为了防止那*,在'silent!'之后再次添加'keepjumps'。 – ravron

1

可能有更漂亮的方法来做到这一点,但如何简单地保存位置在另一个标记?例如:

" This is where I'd put the command to ignore future movements 
" go to the mark and label it as z 
`.mz 

" This is where I'd put the command to start remembering movements again 
" return to your mark and create a fake edit there to reset the most recent edit mark 
`zi <Esc>x 
+0

有趣。我也考虑过这种解决方案。事实上,这个标记甚至不是必需的,因为'winsaveview()'和后面的'winrestview()'保存并恢复了光标位置。更重要的是“假”编辑,强制'.'标记回到正确的位置。这应该可行,但是现在,我会坚持一个更“漂亮”的解决方案。如果没有出现,你会得到支票。 +1不管。 – ravron

3

尝试:lockmarks <command>在您的autocmd。对此的帮助说'.是不会被命令改变的东西之一。

+0

我选择了''keepjumps' over':lockmarks'作为答案,因为该命令在范围上似乎更加有限,但仍能达到所述目标。谢谢,不过。 – ravron

+0

@Riley这很有道理。感谢您提出这个问题;我学到了一些东西。 –