vim 插入文件头问题

2016-07-28 23:47:59 +08:00
 xinali

.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 文件可以自动插入文件头,但是现在不知道怎么回事,就是无法插入成功,只是插入两行空行,没有任何文字,这是为什么呢?

2588 次点击
所在节点    Vim
6 条回复
yangtukun1412
2016-07-29 10:11:16 +08:00
你是不是用 nerdtree 新建的文件?
xinali
2016-07-29 10:15:02 +08:00
@yangtukun1412 不是,现在查到毛病出在哪里了,只要添加了安装插件的那些行就不会自动添加(也插入,但是插入的是空行),注释了或是不用插件就没有问题,没办法我只能重新定义了快捷键,自动插入是没招了
yangtukun1412
2016-07-29 10:22:39 +08:00
@xinali 嗯, 之前遇到过用 nerdtree 新建文件时 autocmd 不起作用的问题. 你可以把 vim-plug 管理插件的部分移到最前面试一下.
ashfinal
2016-07-29 13:55:41 +08:00
建议使用 ultisnips 来插入文件头。
你这么写到 vimrc 里显得有点乱。
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
2016-07-29 22:30:54 +08:00
call plug#begin('~/.vim/plugged') 会调用 filetype off 这是这个插件比较恶心的地方

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/295642

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX