Vim配置文件(自用)

yuanheci 2023年05月20日 713次浏览

在vim和Windows中实现无缝复制粘贴:

而ctrl-c以及ctrl-v用到的是系统剪贴板(system clipboard)。vim寄存器和系统剪贴板不是一个东西。顾名思义,vim寄存器的数据作用域仅限于vim本地,甚至如果开多个vim窗口,每个窗口都有一套自己完整的寄存器,互相不影响。而系统剪贴板作为系统级别的全局变量,两边当然不能混用。所以vim专门提供了"+寄存器作为对系统剪贴板的映射。可以理解成自动把"+寄存器的内容再复制一份到系统剪贴板,前提是你得把clipboard属性设置成打开。有些版本(比如MacOS自带的vim)就不支持这个映射。重装vim就是为了打开这个开关。(如果有简便的直接改设置方法,请纠正我)。打开以后用"+y命令把内容复制到和系统剪贴板关联的寄存器"+上。而y只是复制到默认无名寄存器""上。
作者:胖君
链接:https://www.zhihu.com/question/19863631/answer/442180294
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • win中内容粘贴到vim中:ctrl+shift+v
  • 通过下面的vim配置后,将vim中内容复制到win中:ctrl+a,ctrl+v
  1. " An example for a vimrc file.
  2. "
  3. " To use it, copy it to
  4. " for Unix and OS/2: ~/.vimrc
  5. " for Amiga: s:.vimrc
  6. " for MS-DOS and Win32: $VIM\_vimrc
  7. " for OpenVMS: sys$login:.vimrc

  8. " When started as "evim", evim.vim will already have done these settings.
  9. if v:progname =~? "evim"
  10. finish
  11. endif

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

  15. " allow backspacing over everything in insert mode
  16. set backspace=indent,eol,start

  17. if has("vms")
  18. set nobackup " do not keep a backup file, use versions instead
  19. else
  20. set backup " keep a backup file
  21. endif
  22. set history=50 " keep 50 lines of command line history
  23. set ruler " show the cursor position all the time
  24. set showcmd " display incomplete commands
  25. set incsearch " do incremental searching
  26. "==========================================================================
  27. "My Setting-sunshanlu
  28. "==========================================================================
  29. vmap <leader>y :w! /tmp/vitmp<CR>
  30. nmap <leader>p :r! cat /tmp/vitmp<CR>

  31. "语法高亮
  32. syntax enable
  33. syntax on
  34. "显示行号
  35. set nu

  36. "修改默认注释颜色
  37. "hi Comment ctermfg=DarkCyan
  38. "允许退格键删除
  39. "set backspace=2
  40. "启用鼠标
  41. set mouse=a
  42. set selection=exclusive
  43. set selectmode=mouse,key
  44. "按C语言格式缩进
  45. set cindent
  46. set autoindent
  47. set smartindent
  48. set shiftwidth=4

  49. " 允许在有未保存的修改时切换缓冲区
  50. "set hidden

  51. " 设置无备份文件
  52. set writebackup
  53. set nobackup

  54. "显示括号匹配
  55. "set showmatch
  56. "括号匹配显示时间为1(单位是十分之一秒)
  57. set matchtime=5
  58. "显示当前的行号列号:
  59. set ruler
  60. "在状态栏显示正在输入的命令
  61. set showcmd

  62. set foldmethod=syntax
  63. "默认情况下不折叠
  64. set foldlevel=100
  65. " 开启状态栏信息
  66. set laststatus=2
  67. " 命令行的高度,默认为1,这里设为2
  68. "set cmdheight=2

  69. " 显示Tab符,使用一高亮竖线代替
  70. "set list
  71. "set listchars=tab:\|\ ,
  72. "set listchars=tab:>-,trail:-

  73. inoremap jk <ESC>

  74. map <C-a> ggVG
  75. map <C-c>: "+y

  76. let g:netrw_winsize=25
  77. let g:newtrw_browse_split=3

  78. "侦测文件类型
  79. filetype on
  80. "载入文件类型插件
  81. filetype plugin on
  82. "为特定文件类型载入相关缩进文件
  83. filetype indent on
  84. " 启用自动补全
  85. filetype plugin indent on


  86. "设置编码自动识别, 中文引号显示
  87. filetype on "打开文件类型检测
  88. "set fileencodings=euc-cn,ucs-bom,utf-8,cp936,gb2312,gb18030,gbk,big5,euc-jp,euc-kr,latin1
  89. set fileencodings=utf-8,gb2312,gbk,gb18030
  90. "这个用能很给劲,不管encoding是什么编码,都能将文本显示汉字
  91. "set termencoding=gb2312
  92. set termencoding=utf-8
  93. "新建文件使用的编码
  94. set fileencoding=utf-8
  95. "set fileencoding=gb2312
  96. "用于显示的编码,仅仅是显示
  97. set encoding=utf-8
  98. "set encoding=utf-8
  99. "set encoding=euc-cn
  100. "set encoding=gbk
  101. "set encoding=gb2312
  102. "set ambiwidth=double
  103. set fileformat=unix


  104. "设置高亮搜索
  105. set hlsearch
  106. "在搜索时,输入的词句的逐字符高亮
  107. set incsearch

  108. " 着色模式
  109. set t_Co=256
  110. "colorscheme wombat256mod
  111. "colorscheme gardener
  112. "colorscheme elflord
  113. colorscheme desert
  114. "colorscheme evening
  115. "colorscheme darkblue
  116. "colorscheme torte
  117. "colorscheme default

  118. " 字体 && 字号
  119. set guifont=Monaco:h10
  120. "set guifont=Consolas:h10

  121. " :LoadTemplate 根据文件后缀自动加载模板
  122. "let g:template_path='/home/ruchee/.vim/template/'

  123. " :AuthorInfoDetect 自动添加作者、时间等信息,本质是NERD_commenter && authorinfo的结合
  124. ""let g:vimrc_author='sunshanlu'
  125. ""let g:vimrc_email='sunshanlu@baidu.com'
  126. ""let g:vimrc_homepage='http://www.sunshanlu.com'
  127. "
  128. "
  129. " Ctrl + E 一步加载语法模板和作者、时间信息
  130. ""map <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi
  131. ""imap <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi
  132. ""vmap <c-e> <ESC>:AuthorInfoDetect<CR><ESC>Gi

  133. map <c-]> g<c-]>

  134. " ======= 引号 && 括号自动匹配 ======= "
  135. "
  136. ":inoremap ( ()<ESC>i

  137. ":inoremap ) <c-r>=ClosePair(')')<CR>
  138. "
  139. ":inoremap { {}<ESC>i
  140. "
  141. ":inoremap } <c-r>=ClosePair('}')<CR>
  142. "
  143. ":inoremap [ []<ESC>i
  144. "
  145. ":inoremap ] <c-r>=ClosePair(']')<CR>
  146. "
  147. ":inoremap < <><ESC>i
  148. "
  149. ":inoremap > <c-r>=ClosePair('>')<CR>
  150. "
  151. "":inoremap " ""<ESC>i
  152. "
  153. ":inoremap ' ''<ESC>i
  154. "
  155. ":inoremap ` ``<ESC>i
  156. "
  157. ":inoremap * **<ESC>i

  158. " 每行超过80个的字符用下划线标示
  159. ""au BufRead,BufNewFile *.s,*.asm,*.h,*.c,*.cpp,*.java,*.cs,*.lisp,*.el,*.erl,*.tex,*.sh,*.lua,*.pl,*.php,*.tpl,*.py,*.rb,*.erb,*.vim,*.js,*.jade,*.coffee,*.css,*.xml,*.html,*.shtml,*.xhtml Underlined /.\%81v/
  160. "
  161. "
  162. " For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
  163. " let &guioptions = substitute(&guioptions, "t", "", "g")

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

  166. " This is an alternative that also works in block mode, but the deleted
  167. " text is lost and it only works for putting the current register.
  168. "vnoremap p "_dp

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

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

  177. " Enable file type detection.
  178. " Use the default filetype settings, so that mail gets 'tw' set to 72,
  179. " 'cindent' is on in C files, etc.
  180. " Also load indent files, to automatically do language-dependent indenting.
  181. filetype plugin indent on

  182. " Put these in an autocmd group, so that we can delete them easily.
  183. augroup vimrcEx
  184. au!

  185. " For all text files set 'textwidth' to 80 characters.
  186. autocmd FileType text setlocal textwidth=80

  187. " When editing a file, always jump to the last known cursor position.
  188. " Don't do it when the position is invalid or when inside an event handler
  189. " (happens when dropping a file on gvim).
  190. autocmd BufReadPost *
  191. \ if line("'\"") > 0 && line("'\"") <= line("$") |
  192. \ exe "normal g`\"" |
  193. \ endif

  194. augroup END

  195. else

  196. set autoindent " always set autoindenting on

  197. endif " has("autocmd")

  198. " 增加鼠标行高亮
  199. "set cursorline
  200. "hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white

  201. " 设置tab是四个空格
  202. set ts=4
  203. set expandtab

  204. " 主要给Tlist使用
  205. let Tlist_Exit_OnlyWindow = 1
  206. let Tlist_Auto_Open = 1


  207. "配置vim的内置终端
  208. noremap <C-i> :belowright term ++rows=8<CR>

  209. ""NerdTree插件的配置信息

  210. ""打开vim时,自动打开NERDTree
  211. "autocmd vimenter * NERDTree
  212. ""将F2设置为开关NERDTree的快捷键
  213. "map <F2> :NERDTreeMirror<CR>
  214. "map <f2> :NERDTreeToggle<cr>

  215. "设置Ctrl+n为开关NERDTree的快捷键
  216. map <C-n> :NERDTreeMirror<CR>
  217. map <C-n> :NERDTreeToggle<cR>
  218. ""修改树的显示图标
  219. let g:NERDTreeDirArrowExpandable = '+'
  220. let g:NERDTreeDirArrowCollapsible = '-'
  221. ""窗口位置
  222. let g:NERDTreeWinPos='left'
  223. ""窗口尺寸
  224. let g:NERDTreeSize=30
  225. ""窗口是否显示行号
  226. let g:NERDTreeShowLineNumbers=1
  227. ""不显示隐藏文件
  228. let g:NERDTreeHidden=0



  229. "ctags"

  230. set tags=tags;
  231. set autochdir

  232. "========================="
  233. set tags+=~/.vim/systags
  234. "========================="

  235. if has("cscope")
  236. set csto=0
  237. set nocsverb
  238. " add any database in current directory
  239. if filereadable("cscope.out")
  240. "cs add cscope.out
  241. cs add $PWD/cscope.out $PWD
  242. else "子目录打开,向上查找
  243. let cscope_file=findfile("cscope.out", ".;")
  244. let cscope_pre=matchstr(cscope_file, ".*/")
  245. if !empty(cscope_file) && filereadable(cscope_file)
  246. exe "cs add" cscope_file cscope_pre
  247. endif
  248. endif
  249. set csverb
  250. "set cst 这两句会将cscope当作tag,当找不到时会卡住,因此注释掉
  251. "set cscopetag
  252. endif

  253. nmap zs :cs find s <C-R>=expand("<cword>")<CR><CR>
  254. nmap zg :cs find g <C-R>=expand("<cword>")<CR><CR>
  255. nmap zc :cs find c <C-R>=expand("<cword>")<CR><CR>
  256. nmap zt :cs find t <C-R>=expand("<cword>")<CR><CR>
  257. nmap ze :cs find e <C-R>=expand("<cword>")<CR><CR>
  258. nmap zf :cs find f <C-R>=expand("<cfile>")<CR><CR>
  259. nmap zi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  260. nmap zd :cs find d <C-R>=expand("<cword>")<CR><CR>