在VIM的命令模式中 :reg 可以打印出当前所有寄存器中保存的值,我们可以看到寄存器 % 保存的就是当前正在编辑的文件名了,那么我们如何获得不包含扩展名的文件名呢?
获得这样的文件名有什么用呢?显然任何功能都会有其用武之地
比如,我现在想编写一段VIM脚本,内容如下:
map <F9> :call CompileRunGpp()<CR>
func! CompileRunGpp()
exec "w"
exec "!g++ % -o %.exe"
exec "!%"
endfunc
它的功能就是将F9键映射到一个函数,而函数中一条条执行的指令就相当于我们在VIM的命令模式下一条条键入的命令,该函数完成的功能就是1) 保存当前文本 2) 编译 3) 运行
但是这里有个问题, g++ % -o %.exe
假如我当前正在编辑的文件为main.cpp 该命令相当于 g++ main.cpp -o main.cpp.exe
这显然不是我想要的结果,在生成.exe文件时我希望析取出文件命中不包含扩展名的部分
言归正传,在好一番baidu + google后,找到下面一篇文章,正好可以满足要求
见:vi improved-Entering Filenames
未能找到原文作者给出的这个参考来自何处
但通过搜索整个VIM帮助文档,发现完整的文件修饰符参考可以通过在VIM的命令模式下键入
:help filename-modifiers获得
% 当前文件名
光标所在处文件名
一些文件名相关的修饰符:
:p 将文件名转为绝对路径
:~ 将绝对路径转为缩写的版本
:. 在可能的情况下把路径转为当前目录的相对路径
:h 取出目录路径
:t 取出单独的文件名
:r 取出单独的、不含扩展名的文件名
:e 取出文件扩展名
可以用:echo expand(“%:r”)测试看看。
原文链接:Vim: 文件名相关注记
下面,我们就可以将我们原来的脚本改成:
map <F9> :call CompileRunGpp()<CR>
func! CompileRunGpp()
exec "w"
exec "!g++ % -o %:r.exe"
exec "!%:r"
endfunc
另附我的Vim配置
"start a comment
"some personal settings
set number "display line number
filetype on "check file type
"set background=dark "用在windows下颜色很难看
syntax on "syntax highlight
"下面两行在进行编写代码时,在格式对齐上很有用;
"第一行,vim使用自动对齐,也就是把当前行的对起格式应用到下一行;
"第二行,依据上面的对齐格式,智能的选择对起方式,对于类似C语言编写上很有用
set autoindent
set smartindent
"第一行设置tab键为4个空格,第二行设置当行之间交错时使用4个空格
set tabstop=4
set shiftwidth=4
"设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
set showmatch
"去除vim的GUI版本中的toolbar
set guioptions-=T
"在编辑过程中,在右下角显示光标位置的状态行
set ruler
"查询时非常方便,如要查找book单词,当输入到/b时,会自动找到第一
"个b开头的单词,当输入到/bo时,会自动找到第一个bo开头的单词,依
"次类推,进行查找时,使用此设置会快速找到答案,当你找要匹配的单词
"时,别忘记回车
set incsearch
"高亮当前行
set cursorline
"设置折叠方法为根据语法折叠
"set foldmethod=syntax
"配色方案, 位于vim/vim70/colors中
colorscheme torte
"搜索时不区分大小写
set ignorecase
"useful vim settings for python
set tabstop=4 " 一个tab 4个空格宽 <abbr. ts>
set shiftwidth=4 " 用<和>移动代码时的空格宽度 <abbr. sw>
set smarttab " <abbr. sta>
set expandtab " 扩展tab为空格 <abbr. et>
set softtabstop=4 " <BackSpace>时,视4个空格为一个<tab> <abbr. sts>
set autoindent " 保持上一行的缩进 <abbr. ai>
set directory=$VIMRUNTIME/../swap " where to put swap file
set backupdir=$VIMRUNTIME/../backup
"去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
"在Insert Mode 设置缩写,插入当前日期
iab xdate <c-r>=strftime("%Y/%m/%d %H:%M:%S")<CR><ESC>
" C++的编译和运行 [当然,你的机器上要有g++, 且其路径包含在PATH中]
map <F7> :call Compile()<CR>
func! Compile()
exec "w"
exec "!g++ % -o %:r.exe"
endfunc
map <F8> :call Run()<CR>
func! Run()
exec "!%:r"
endfunc
map <F9> :call CompileRunGpp()<CR>
func! CompileRunGpp()
exec "w"
exec "!g++ % -o %:r.exe"
exec "!%:r"
endfunc
map <F10> :call CompileRunGpp1()<CR>
func! CompileRunGpp1()
exec "w"
exec "!g++ % -o %:r.exe"
exec "!%:r <%:r.in"
endfunc
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction