2011-04-26 18 views
10

我正在使用Mac和MacVim 7.3。如何在vim中將符號鏈接擴展爲文件名的完整路徑?

我有一個符號鏈接〜/ some/symlink,它是一個真實文件〜/ some/actual_file的鏈接。當我「vim〜/ some/symlink」時,它會在vim中用vim的狀態行顯示文件名稱是〜/ some/symlink,這是有道理的。

如何從vim中獲取「real」文件的完整路徑〜/ some/actual_file?我想要vim status行說〜/ some/actual_file而不是〜/ some/symlink。

我預計VIM功能解析()會工作,因爲它幫助說明(粘貼以下),但解決(「〜/一些/符號鏈接」)返回〜/一些/符號鏈接,所以這是沒有任何幫助。

我錯過了什麼?謝謝!

resolve({filename})     *resolve()* *E655* 
     On MS-Windows, when {filename} is a shortcut (a .lnk file), 
     returns the path the shortcut points to in a simplified form. 
     On Unix, repeat resolving symbolic links in all path 
     components of {filename} and return the simplified result. 
     To cope with link cycles, resolving of symbolic links is 
     stopped after 100 iterations. 
     On other systems, return the simplified {filename}. 
+0

使用vim,您可以執行命令並使用system(「cmd」)獲取輸出。有了這個,migth可以調用「readlink -m」來獲得真正的路徑。 – Lynch 2011-04-26 18:22:14

回答

22

你的問題是「〜」,它不是文件名的一部分,而是你的主目錄的縮寫。您可以使用expand(),然後使用resolve()。例如:

:echo resolve(expand("~/some/symlink")) 

expand()也將擴大般的環境變量的東西(如:$HOME$VIMRUNTIME)。

+0

完美的答案,謝謝勞倫斯! – svec 2011-04-26 18:42:36

+3

@svec如果你不想'expand()'擴展環境變量,並且使用'fnamemodify('〜/ some/symlink',':p')'。 – ZyX 2011-04-26 20:02:30

+0

要獲取當前文件的完整路徑,請將'%/'替換爲'〜/ some/symlink'。將它綁定到一個引導鍵組合,當你忘記正在編輯的文件時,它會很方便。(-_-) – 2017-02-07 13:23:57

相關問題