vimrc 脚本获取当前文件名

Register % contains the name of the current file, and register # contains the name of the alternate file. These registers allow the name of the current or alternate files to be displayed or inserted. The name, or full path, or directory containing the file can be used.

For example, in directory /abc the command vim def/my.txt would edit file /abc/def/my.txt. The following commands could be entered to display the information shown.

:echo expand('%:t')     my.txt  name of file ('tail')
:echo expand('%:p')     /abc/def/my.txt full path
:echo expand('%:p:h')   /abc/def    directory containing file ('head')
:echo expand('%:p:h:t') def First get the full path with :p (/abc/def/my.txt), then get the head of that with :h (/abc/def), then get the tail of that with :t (def)
:echo expand('%:r')     my  name of file less one extension ('root')
:echo expand('%:e')     txt name of file's extension ('extension')```
For more info run :help expand

If all that is wanted is to display the name of the current file, type Ctrl-G (or press 1 then Ctrl-G for the full path).

When using @%, the name is displayed relative to the current directory.

In insert mode, type Ctrl-R then % to insert the name of the current file.

In command mode (after typing a colon), type Ctrl-R then % to insert the name of the current file. The inserted name can then be edited to create a similar name.

In normal mode, type "%p to put the name of the current file after the cursor (or "%P to insert the name before the cursor).

The following commands insert lines consisting of the full path of the current and alternate files into the buffer:

:put =expand('%:p')
:put =expand('#:p')


#打赏
如果这篇文章解决了您的问题,让我买根烟抽抽。

![支付宝.jpg](http://upload-images.jianshu.io/upload_images/4765606-b13a42b44fd975a7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/340)


![微信.jpg](http://upload-images.jianshu.io/upload_images/4765606-3ad5592b17125067.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/340)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容