2013-07-02 90 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這很有道理。感謝您提出這個問題;我學到了一些東西。 –