vim脚本提取文件名
简介
创建文件时,希望直接生成文件标题
快捷键
- vimrc支持函数expand,:h expand可以查看帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22: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')