Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

經典實用的 Linux Vim 設定檔:F5 編譯執行、自動補全一次搞定

這份 vim 設定的亮點相當多:

1. 按 F5 可直接編譯並執行 C、C++、Java 程式以及執行 shell 腳本,按 F8 可對 C、C++ 進行除錯。

2. 自動插入檔案標頭:新建 C、C++ 原始檔時自動帶入檔名、作者、聯絡方式、建立時間等。

3. 將 Ctrl+A 映射為全選並複製。

4. 按 F2 可一鍵消除程式碼中的空行。

5. F3 列出當前目錄檔案,開啟樹狀目錄。

6. 支援滑鼠選擇、方向鍵移動。

7. 程式碼高亮、自動縮進、顯示行號與狀態行。

8. Ctrl+P 自動補全。

9. []、{}、()、""、'' 等自動配對補全。

10. 更多功能可自行研究設定檔。

使用方法:把下面的內容複製到文字檔,改名為「.vimrc」(別忘了前面的點),放到使用者家目錄根層即可,重新開啟 vim 就能看到效果。此設定也已托管到 GitHub,後續新增了不少功能,詳見:https://github.com/test/vim

經典實用的 Linux Vim 設定檔:F5 編譯執行、自動補全一次搞定

精簡版設定檔(無插件)

""""""""""""""""""" 顯示相關 """""""""""""""""""
set go=                        " 不要圖形按鈕
set guifont=Courier_New:h10:cANSI
autocmd InsertLeave * se nocul  " 淺色高亮當前行
autocmd InsertEnter * se cul
set showcmd                    " 顯示輸入中的命令
set novisualbell
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
set laststatus=1
set foldenable
set foldmethod=manual
set nocompatible
if version >= 603
  set helplang=cn
  set encoding=utf-8
endif
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8

""""""""""" 新文件標題 """""""""""
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
func SetTitle()
  if &filetype == 'sh'
    call setline(1,"\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#")
    call append(line("."), "\# File Name: ".expand("%"))
    call append(line(".")+1, "\# Author: test")
    call append(line(".")+2, "\# Created Time: ".strftime("%c"))
    call append(line(".")+5, "\#!/bin/bash")
  else
    call setline(1, "/***********************************************************************")
    call append(line("."), " > File Name: ".expand("%"))
    call append(line(".")+1, " > Author: test")
    call append(line(".")+3, " > Created Time: ".strftime("%c"))
    call append(line(".")+4, " **********************************************************************/")
  endif
  if &filetype == 'cpp'
    call append(line(".")+6, "#include<iostream>")
    call append(line(".")+7, "using namespace std;")
  endif
  if &filetype == 'c'
    call append(line(".")+6, "#include<stdio.h>")
  endif
  autocmd BufNewFile * normal G
endfunc

""""""""""""""""""" 鍵盤命令 """""""""""""""""
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
vmap <C-c> "+y
nnoremap <F2> :g/^\s*$/d<CR>          " 去空行
nnoremap <C-F2> :vert diffsplit         " 比較檔案
map <F3> :tabnew .<CR>                  " 列出目錄
map <C-F3> \be                          " 樹狀目錄

" C、C++ 按 F5 編譯執行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
  exec "w"
  if &filetype == 'c'
    exec "!g++ % -o %<"
    exec "! ./%<"
  elseif &filetype == 'cpp'
    exec "!g++ % -o %<"
    exec "! ./%<"
  elseif &filetype == 'java'
    exec "!javac %"
    exec "!java %<"
  elseif &filetype == 'sh'
    :!./%
  endif
endfunc

" C、C++ 除錯
map <F8> :call Rungdb()<CR>
func! Rungdb()
  exec "w"
  exec "!g++ % -g -o %<"
  exec "!gdb ./%<"
endfunc

""""""""""""""""""" 實用設置 """""""""""""""""
set autoread
set completeopt=preview,menu
filetype plugin on
set clipboard+=unnamed
set nobackup
set autowrite
set cursorline
set number
set history=1000
set noswapfile
set ignorecase
set hlsearch
set incsearch
set autoindent
set cindent
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set smarttab
set enc=utf-8
set langmenu=zh_CN.UTF-8
set helplang=cn
set laststatus=2
set cmdheight=2
filetype on
filetype plugin on
filetype indent on
set wildmenu
set backspace=2
set whichwrap+=<,>,h,l
set mouse=a
set showmatch
set matchtime=1
set scrolloff=3
set smartindent

" 自動補全括號
:inoremap ( ()<ESC>i
:inoremap { {<CR>}<ESC>O
:inoremap [ []<ESC>i
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
  if getline('.')[col('.') - 1] == a:char
    return "\"
  else
    return a:char
  endif
endfunction
set completeopt=longest,menu

""""""""""""""""""" CTags 設定 """""""""""""""""
let Tlist_Sort_Type = "name"
let Tlist_Use_Right_Window = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_File_Fold_Auto_Close = 0
set tags=tags
let Tlist_Auto_Open=1
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
let Tlist_Show_One_File = 1

把以上內容存成 ~/.vimrc 即可立即使用。無論是寫 C/C++、Java、shell 還是日常編輯,這份設定都能大幅提升效率。

Copyright © Linux教程網 All Rights Reserved