V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xinali
V2EX  ›  Vim

vim 插入文件头问题

  •  
  •   xinali · 2016-07-28 23:47:59 +08:00 · 2607 次点击
    这是一个创建于 3075 天前的主题,其中的信息可能已经有所发展或是发生改变。

    .vimrc :

    
    set nocompatible
    "set lines=40 columns=100 " set font in linux set guifont=Monospace\ 14
    " set font in windows
    " set guifont=Consolas:h14
    
    " for gvim
    " set guioptions-=T  set guioptions-=m  colorscheme murphy " c coding indent, turn off when use python
    " set cindent
    
    syntax on
    set nu
    set autoindent
    set shiftwidth=4
    set tabstop=4
    set softtabstop=4
    set nobackup
    set showcmd
    set showmode
    set showmatch
    filetype on
    
    
    " insert # as comment
    map <C-I> I# <ESC>
    
    map <silent> <F5> :call Compile()<CR>
    function! Compile()
        exec "w"
    
    	if &filetype == "python"
    		exec "!python %"
        endif
    
    	if &filetype == "javascript"
    		exec "!node %"
    	endif 
    
        if &filetype == 'c'
            exec "!g++ % -o %<"
    		exec "!g++ % -o test"
            exec  "!./test"
        elseif &filetype == 'cpp'
            exec "!g++ % -o test"
            exec "!./test"
        elseif &filetype == 'java'
            exec "!javac %"
            exec "!java %<"
        elseif &filetype == 'sh'
            exec "!./%"
    	elseif &filetype == 'php'
    		exec "!php -f %"
    	endif
    endfunc
    
    
    " Python default settings
    autocmd FileType python set tabstop=4 shiftwidth=4 expandtab ai
    autocmd FileType ruby set tabstop=2 shiftwidth=2 softtabstop=2 expandtab ai
    autocmd BufRead,BufNew *.md,*.mkd,*.markdown  set filetype=markdown.mkd
    
    " when save python file, then delete redundant space
    fun! <SID>StripTrailingWhitespaces()
        let l = line(".")
        let c = col(".")
        %s/\s\+$//e
        call cursor(l, c)
    endfun
    
    autocmd FileType c,cpp,java,go,php,javascript,puppet,python,rust,twig,xml,yml,perl autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()
    
    
    " automate insert the header of file
    autocmd BufNewFile *.sh,*.py exec ":call AutoSetFileHead()"
    "autocmd BufNewFile *.sh,*.py call AutoSetFileHead()
    function! AutoSetFileHead()
        if &filetype == 'sh'
            call setline(1, "\#!/bin/bash")
        endif
        if &filetype == 'python'
            call setline(1, "\#!/usr/bin/env python")
            call append(1, "\# encoding: utf-8")
        endif
        normal G
        normal o
        normal o
    endfunc
    
    " when vimrc changed, thend auto load  with windows
    "autocmd! bufwritepost _vimrc source %
    " when vimrc changed, thend auto load  with linux
    autocmd! bufwritepost .vimrc source %
    
    "when vim exit, cat file content in terminal
    "set t_ti = t_te=
    
    " history account
    set history=2000
    
    call plug#begin('~/.vim/plugged')
    
    " Make sure you use single quotes
    
    " Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
    Plug 'junegunn/vim-easy-align'
    
    " Any valid git URL is allowed
    Plug 'https://github.com/junegunn/vim-github-dashboard.git'
    
    " Group dependencies, vim-snippets depends on ultisnips
    Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'
    
    " On-demand loading
    Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
    Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
    
    " Using a non-master branch
    Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }
    
    " Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
    Plug 'fatih/vim-go', { 'tag': '*' }
    
    " Plugin options
    Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }
    
    " Plugin outside ~/.vim/plugged with post-update hook
    Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
    
    " Unmanaged plugin (manually installed and updated)
    Plug '~/my-prototype-plugin'
    
    " Add plugins to &runtimepath
    call plug#end()
    
    
    
    
    " NERDTree config
    map <F2> :NERDTreeToggle<CR>
    "map :silent! NERDTreeToggle
    "autocmd vimEnter * NERDTree
    
    

    以前是好用的,创建 python 文件可以自动插入文件头,但是现在不知道怎么回事,就是无法插入成功,只是插入两行空行,没有任何文字,这是为什么呢?

    6 条回复    2016-07-29 22:30:54 +08:00
    yangtukun1412
        1
    yangtukun1412  
       2016-07-29 10:11:16 +08:00
    你是不是用 nerdtree 新建的文件?
    xinali
        2
    xinali  
    OP
       2016-07-29 10:15:02 +08:00
    @yangtukun1412 不是,现在查到毛病出在哪里了,只要添加了安装插件的那些行就不会自动添加(也插入,但是插入的是空行),注释了或是不用插件就没有问题,没办法我只能重新定义了快捷键,自动插入是没招了
    yangtukun1412
        3
    yangtukun1412  
       2016-07-29 10:22:39 +08:00
    @xinali 嗯, 之前遇到过用 nerdtree 新建文件时 autocmd 不起作用的问题. 你可以把 vim-plug 管理插件的部分移到最前面试一下.
    ashfinal
        4
    ashfinal  
       2016-07-29 13:55:41 +08:00
    建议使用 ultisnips 来插入文件头。
    你这么写到 vimrc 里显得有点乱。
    rainysia
        5
    rainysia  
       2016-07-29 15:31:42 +08:00
    把这函数改改吧,

    function! AutoSetFileHead()
    if &filetype == 'sh'
    call setline(1, "\#!/bin/bash")
    elseif &filetype == 'python'
    call setline(1,"#!/usr/bin/env python")
    call append(line("."),"# -*- coding: UTF-8 -*-")
    call append(line(".")+1, "")
    normal G
    normal o
    normal o
    endif
    endfunc
    chemzqm
        6
    chemzqm  
       2016-07-29 22:30:54 +08:00
    call plug#begin('~/.vim/plugged') 会调用 filetype off 这是这个插件比较恶心的地方
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2611 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 03:20 · PVG 11:20 · LAX 19:20 · JFK 22:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.