vim 配置替换错误E488: Trailing characters

错误描述

下面是我的vim配置:

    set nocompatible
    filetype on
    syntax on
    :filetype indent on
    set et
    set ci
    set shiftwidth=4
    set number

    :set ts=4
    set expandtab
    :%retab!
    :set modifiable

    map  :NERDTreeMirror
    map  :NERDTreeToggle

    ##实现新建.py和.sh文件时自动添加文件头
    "auto add file header --start
    autocmd BufNewFile *.sh 0r ~/.vim/template/shell_header
    autocmd BufNewFile *.py 0r ~/.vim/template/python_header
    autocmd BufNewFile *.py,*.sh  ks|call FileName()|'s
    autocmd BufNewFile *.py,*.sh  ks|call CreatedTime()|'s

    fun FileName()
        let info = "File Name      :"
        if line("$") > 10
            let len = 10
        else
            let len = line("$")
        endif
        exe "1," . len . "g/".info.".*/s/".info.".*/".info." ".expand("%")
        "exe 1,5g/File Name      :.*/s/File Name      :.*/File Name      :".expand("%")
    endfun


    fun CreatedTime()
        let info = "Created Time   :"
        if line("$") > 10
            let len = 10
        else
            let len = line("$")
        endif
        exe "1," . len . "g/".info.".*/s/".info.".*/".info." ".strftime("%Y-%m-%d %T")
    endfun
    "auto add file header --end  

python文件头的配置是python_header,sh文件头部与此类似。
错出的地方是是函数FileName中的替换命令。
当我执行vim test.py时,并没有什么错误;但是执行vim ryu-learn/xxx.sh时(ryu-learn是一个目录名),提示:

"~/.vim/template/shell_header" 8L, 225C
Error detected while processing function FileName:
line    7:
E488: Trailing characters
Press ENTER or type command to continue

解决问题

原来是因为使用vim新建文件时,vim后面的整个内容都会被当做文件名读入,包括目录名和符号/;而执行替换命令的分隔符也是/,这肯定会有错啊。所以把函数中替换命令的分隔符改成其他的,比如#,就可以了。修改后的vim配置如下:

......

    "auto add file header --start
    autocmd BufNewFile *.sh 0r ~/.vim/template/shell_header
    autocmd BufNewFile *.py 0r ~/.vim/template/python_header
    autocmd BufNewFile *.py,*.sh  ks|call FileName()|'s
    autocmd BufNewFile *.py,*.sh  ks|call CreatedTime()|'s

    fun FileName()
        let info = "File Name      :"
        if line("$") > 10
            let len = 10
        else
            let len = line("$")
        endif
        exe "1," . len . "g#".info.".*#s#".info.".*#".info." ".expand("%")
    endfun


    fun CreatedTime()
        let info = "Created Time   :"
        if line("$") > 10
            let len = 10
        else
            let len = line("$")
        endif
        exe "1," . len . "g#".info.".*#s#".info.".*#".info." ".strftime("%Y-%m-%d %T")
    endfun
    "auto add file header --end 

问题解决。

你可能感兴趣的:(Linux系统配置)