`
haoningabc
  • 浏览: 1465942 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

vim复制代码包含注释时格式会乱掉(转)

vim 
阅读更多
乱码问题
一般来说只需要正确设置vim的编码识别序列就很少会遇到乱码问题:
set fileencodings=ucs-bom,utf-8,utf-16,gbk,big5,gb18030,latin1

转自:http://socol.iteye.com/blog/579503


每次复制代码时,如果代码里有 // 这样的注释就容易让格式乱掉,通过下面的设置就可以避免这种情况。

粘贴代码时取消自动缩进
VIM在粘贴代码时会自动缩进,把代码搞得一团糟糕,甚至可能因为某行的一个注释造成后面的代码全部被注释掉,我知道有同学这个时候会用vi去打开文件再粘贴上去(鄙人以前就是这样),其实需要先设置一下
set paste
然后再进入插入模式粘贴,代码就不会被自动缩进。可是敲代码的时候需要自动缩进,又得改回来:
set nopaste
最方便的方法就是在.vimrc中加一句:
set pastetoggle=<F9>
以后在插入模式下,只要按F9键就可以切换自动缩进。

中文乱码问题
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
   set termencoding=utf-8
   set encoding=utf-8

.vimrc
	" An example for a vimrc file.
	"
	" Maintainer:	Bram Moolenaar <Bram@vim.org>
	" Last change:	2000 Jan 06
	"
	" To use it, copy it to
	"     for Unix and OS/2:  ~/.vimrc
	"	      for Amiga:  s:.vimrc
	"  for MS-DOS and Win32:  $VIM\_vimrc
	"		for VMS:  sys$login:.vimrc

	" Use Vim settings, rather then Vi settings (much better!).
	" This must be first, because it changes other options as a side effect.
	set nocompatible

	"set fileencodings=gb2312,gb18030,utf-8
	"set termencoding=utf-8
	"set encoding=prc

	set nu
	set bs=2		" allow backspacing over everything in insert mode
	set noai			" always set autoindenting on
	"set backup		" keep a backup file
	set viminfo='20,\"50	" read/write a .viminfo file, don't store more
				" than 50 lines of registers
	set history=50		" keep 50 lines of command line history
	set ruler		" show the cursor position all the time
	"set nonu
	"set bdir=~/.vimbackup
	set term=linux
	set nowrap
	set bg=dark
	set tabstop=4
	" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
	" let &guioptions = substitute(&guioptions, "t", "", "g")

	" Don't use Ex mode, use Q for formatting
	map Q gq

	" Make p in Visual mode replace the selected text with the "" register.
	vnoremap p <Esc>:let current_reg = @"<CR>gvdi<C-R>=current_reg<CR><Esc>

	" Switch syntax highlighting on, when the terminal has colors
	" Also switch on highlighting the last used search pattern.
	if &t_Co > 2 || has("gui_running")
	  syntax on
	  set hlsearch
	endif

	" Only do this part when compiled with support for autocommands.
	if has("autocmd")

	 " In text files, always limit the width of text to 78 characters
	 autocmd BufRead *.txt set tw=78

	 augroup cprog
	  " Remove all cprog autocommands
	  au!

	  " When starting to edit a file:
	  "   For C and C++ files set formatting of comments and set C-indenting on.
	  "   For other files switch it off.
	  "   Don't change the order, it's important that the line with * comes first.
	  autocmd FileType *      set formatoptions=tcql nocindent comments&
	  autocmd FileType c,cpp  set formatoptions=croql cindent comments=sr:/*,mb:*,el:*/,://
	 augroup END

	 augroup gzip
	  " Remove all gzip autocommands
	  au!

	  " Enable editing of gzipped files
	  " set binary mode before reading the file
	  autocmd BufReadPre,FileReadPre	*.gz,*.bz2 set bin
	  autocmd BufReadPost,FileReadPost	*.gz call GZIP_read("gunzip")
	  autocmd BufReadPost,FileReadPost	*.bz2 call GZIP_read("bunzip2")
	  autocmd BufWritePost,FileWritePost	*.gz call GZIP_write("gzip")
	  autocmd BufWritePost,FileWritePost	*.bz2 call GZIP_write("bzip2")
	  autocmd FileAppendPre			*.gz call GZIP_appre("gunzip")
	  autocmd FileAppendPre			*.bz2 call GZIP_appre("bunzip2")
	  autocmd FileAppendPost		*.gz call GZIP_write("gzip")
	  autocmd FileAppendPost		*.bz2 call GZIP_write("bzip2")

	  " After reading compressed file: Uncompress text in buffer with "cmd"
	  fun! GZIP_read(cmd)
	    " set 'cmdheight' to two, to avoid the hit-return prompt
	    let ch_save = &ch
	    set ch=3
	    " when filtering the whole buffer, it will become empty
	    let empty = line("'[") == 1 && line("']") == line("$")
	    let tmp = tempname()
	    let tmpe = tmp . "." . expand("<afile>:e")
	    " write the just read lines to a temp file "'[,']w tmp.gz"
	    execute "'[,']w " . tmpe
	    " uncompress the temp file "!gunzip tmp.gz"
	    execute "!" . a:cmd . " " . tmpe
	    " delete the compressed lines
	    '[,']d
	    " read in the uncompressed lines "'[-1r tmp"
	    set nobin
	    execute "'[-1r " . tmp
	    " if buffer became empty, delete trailing blank line
	    if empty
	      normal Gdd''
	    endif
	    " delete the temp file
	    call delete(tmp)
	    let &ch = ch_save
	    " When uncompressed the whole buffer, do autocommands
	    if empty
	      execute ":doautocmd BufReadPost " . expand("%:r")
	    endif
	  endfun

	  " After writing compressed file: Compress written file with "cmd"
	  fun! GZIP_write(cmd)
	    if rename(expand("<afile>"), expand("<afile>:r")) == 0
	      execute "!" . a:cmd . " <afile>:r"
	    endif
	  endfun

	  " Before appending to compressed file: Uncompress file with "cmd"
	  fun! GZIP_appre(cmd)
	    execute "!" . a:cmd . " <afile>"
	    call rename(expand("<afile>:r"), expand("<afile>"))
	  endfun

	 augroup END

	 " This is disabled, because it changes the jumplist.  Can't use CTRL-O to go
	 " back to positions in previous files more than once.
	 if 0
	  " When editing a file, always jump to the last cursor position.
	  " This must be after the uncompress commands.
	   autocmd BufReadPost * if line("'\"") && line("'\"") <= line("$") | exe "normal `\"" | endif
	 endif

	endif " has("autocmd")
	set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
	set termencoding=utf-8
	set encoding=utf-8

分享到:
评论

相关推荐

    linux-轻量级Vim配置框架全中文注释

    总之,这份“轻量级Vim配置框架全中文注释”是Vim初学者和进阶用户的宝贵资源,它将帮助你提升Vim的使用效率,享受更高效、个性化的代码编写体验。通过学习和实践,你将能够充分利用Vim的强大功能,提高开发效率。

    vim-configure.tar.gz_vim_vim_configure

    "vim-configure.tar.gz_vim_vim_configure" 是一个包含了 Vim 配置文件的压缩包,旨在帮助用户优化 Vim 的使用体验,提供自动提示、补全功能以及代码高亮等特性。 在 Vim 中配置这些功能,可以使编辑效率大幅提升。...

    Windows快捷键与Vim快捷键冲突解决

    因此,对于习惯了Linux版Vim快捷键的用户来说,使用Windows版Vim时会感到非常不适应。 #### 解决方案 针对这些冲突,我们可以采取以下几种方法来解决: 1. **替换常用的复制、剪切和粘贴快捷键**: - **复制**:...

    caw.vim:Vim注释插件:受支持的operatornon-operator映射,可通过点命令重复,具有300多种文件类型

    caw.vim 充分利用了这一点,使得你可以快速地对多行代码进行相同的注释操作。只需在第一次应用注释后,移动光标到新位置并按下 `.`,就可以在新的位置执行相同的操作。 **4. 支持的文件类型** caw.vim 对超过300种...

    vim自动生成文件模板 templates 设置方法

    在Vim中,模板是一种预先定义好的文本结构,当我们创建新文件时,可以根据所选模板自动插入特定格式的代码或文档框架,避免手动输入重复内容。这对于编写规范化的代码或文档非常有帮助。 设置Vim模板的方法主要包括...

    Vim编辑器配置插件

    在处理复杂的代码结构时,DoxygenToolkit.vim可以帮助你保持注释的一致性。它能够识别并更新已存在的注释,比如当你更改函数签名时,插件会自动调整对应的注释。这有助于确保你的代码注释始终与实际代码同步。 除了...

    jeff-vim-read(vim 配置)

    14. **增强的复制和粘贴**:处理复制和粘贴时,可能已经解决了Vim与系统剪贴板之间的兼容问题。 15. **缓冲区管理**:高效管理多个打开的文件,避免频繁的窗口切换。 在安装和使用"jeffy-vim-read"时,用户需要先...

    超强vim配置文件

    vim本来就是很强大 很方便的编辑器 加上我的代码后肯定会如虎添翼 或许读者使用其他编程语言 可以根据自己的需要进行修改 配置文件里面已经加上注释 读者感兴趣的话直接复制下面的代码到文本文件 然后把文件改名...

    vim配置+各种颜色方案

    在提供的“新建文件夹”中,可能包含了多种不同的颜色方案文件,它们通常是`.vim`或`.colors`格式。例如,`colors/molokai.vim`代表Molokai颜色方案,这是一种深色背景的流行方案,适合长时间编写代码。 应用颜色...

    我的vim附带vimrc

    "我的vim附带vimrc"这个标题表明这是一个包含作者个人定制的Vim配置文件(vimrc)的压缩包,用于优化和个性化Vim的使用体验,使其更适合C、C++和Python等编程语言的开发。 Vimrc是Vim的初始化脚本,它允许用户...

    VIM学习资料

    - VIM的强大力量之一是其插件系统,如NERD Commenter(代码注释)、Vim-Fugitive(Git集成)和YouCompleteMe(自动补全)等。 - 安装插件通常通过Vundle或Pathogen等管理工具完成。 6. **VIM配置文件`.vimrc`**:...

    vim7.1 for windows

    4. **插件支持**:Vim 7.1版本已经支持插件,用户可以通过安装插件来扩展其功能,例如NERDCommenter用于注释代码,Vim-Syntax高亮显示代码语法,CtrlP实现快速文件查找等。 5. **配色方案**:Vim允许用户自定义颜色...

    vim linux版本

    6. **插件系统**:Vim拥有丰富的插件库,如NERD Commenter用于注释管理,YouCompleteMe提供智能代码补全,Vim-Fugitive方便Git操作等,这些插件大大扩展了Vim的功能。 7. **跨平台**:Vim不仅在Linux上表现优秀,也...

    vim配置文件

    在压缩包文件`vim-master`中,可能包含了完整的Vim配置示例,包括`.vimrc`文件和可能需要的插件。你可以解压这个文件,研究并学习其中的设置,根据个人需求进行调整。 总之,通过细心配置`.vimrc`,我们可以将Vim...

    Vim用户手册中文版72

    6. **插件系统**:Vim拥有丰富的插件生态系统,如NERDCommenter用于代码注释,YouCompleteMe提供智能补全,Vim-Fugitive用于Git操作等,极大地扩展了其功能。 7. **映射与自动命令**:通过`map`命令,用户可以...

    超全的vim配置文件和插件

    .vimrc是vim的初始化配置文件,它包含了启动vim时执行的一系列命令和设置。这些设置涵盖了语法高亮、自动缩进、快捷键映射、颜色方案、文件类型识别等多个方面。例如,通过在.vimrc中添加以下行,你可以开启自动缩进...

    vim基本配置以及常用插件

    Vim是一款强大的文本编辑器,深受程序员和系统管理员的喜爱。...下载提供的“vim配置”压缩包后,只需按照说明替换`.vimrc`文件,并将插件文件复制到相应的目录,即可快速拥有一个高效的工作环境。

    我的VIM配置

    `.vimrc`是VIM的初始化配置文件,它包含了VIM启动时执行的一系列设置和命令。这个文件可以定制VIM的行为,如设置快捷键、修改颜色方案、启用或禁用特定插件等。例如,`set number`会显示行号,`map &lt;F5&gt; :make&lt;CR&gt;`...

    vim安装包,用于linux十分便捷

    tar -xf vim.tar -C ~ vim /etc/vimrc vim /root/.vimrc set ts=4 设置tab有多少空格 set ai 自动对齐 set nu set mouse=a set shiftwidth=4 shift + . 向左向右缩进 shift + , jklh ,cs 性感的注释 ...

Global site tag (gtag.js) - Google Analytics