Skip to content

aben20807/my_vim

Repository files navigation

Vim 配置整理

  • 環境: Windows 10, Cygwin, Vim8

Abbreviations (縮寫)

:iab <buffer> <expr> function CodeAbbr("function", "function
    \<CR>
    \<CR>endfunction
    \<UP><TAB>
    \<UP><ESC>$li <C-R>=Eatchar(\'\\m\\s\\<bar>\\r\')<CR>
    \")

Demo

Airline-theme (Airline主題)

  • 檔案: .vim/bundle/vim-airline-themes/autoload/airline/themes/ouo.vim
  • 修改了警告、錯誤區塊的顏色

Demo

Alt key map (Alt鍵的mapping)

  • 檔案: .keymap.vim
  • 貼文: 106.07.19 vim map alt(meta) key
  • 設定後可用 <M-x> 來map Alt+x, 其中x 為 a~z, A~Z, 0~9, 還有一些特殊符號像.和/

Author Information (添加作者資訊)

  • 檔案: .author.vim, .vim/after/ftplugin/*.vim
  • 參考: http://www.gegugu.com/2016/02/11/17175.html
  • 透過 <F4> 在文件開頭添加作者、檔案資訊
  • 如果原本就有只會更新日期、檔名,沒有才會新增
  • 請使用 AddTitle() 定義各檔案類型的資訊內容
  • e.g 在 .vim/after/ftplugin/vim.vim 定義如下
function! AddTitle()
    call append(0,"\" Author: Huang Po-Hsuan <aben20807@gmail.com>")
    call append(1,"\" Filename: ".expand("%:t"))
    call append(2,"\" Last Modified: ".strftime("%Y-%m-%d %H:%M:%S"))
    call append(3,"\" Vim: enc=utf-8")
    call append(4,"")
endfunction

Demo

Brackets (括號補全)

Colortheme (vim主題)

  • 檔案: .vim/color/ouo.vim
  • 對應的區塊大部分都在註解裡面了去找吧

Comment (註解)

  • 檔案: .comment.vim, .vim/after/ftplugin/*.vim
  • 請先在各個 ftplugin 的檔案中設定 call CommentFormat("")
  • 使用 <M-/> 來註解或取消註解
  • 使用 visual mode 可一次操作多行

Demo

Compile and Run (編譯執行)

map <F5> :call CompileAndRun()<CR>
" save -> close ALE -> print date -> [execute] run -> open ALE
function! CompileAndRun()
    " save only when changed
    execute "up"
    execute "ALEDisable"
    if &filetype == 'markdown'
        " markdown preview
        try
            " Stop before starting and handle exception
            execute "MarkdownPreviewStop"
        catch /^Vim:E492:/
            execute "MarkdownPreview"
        endtry
    else
        " echo date time
        silent execute "!echo"
        silent execute "!echo -e '\033[31m ╔══════════════════════════════╗' "
        silent execute "!echo -n ' ║ '"
        silent execute "!echo -n `date`"
        silent execute "!echo    ' ║ '"
        silent execute "!echo -e '\033[31m ╚══════════════════════════════╝' \033[37m"
        " detect file type
        if &filetype == 'rust'
            " execute "!rustc % && time ./%< && rm %<"
            execute "!time cargo run"
        elseif &filetype == 'c'
            execute "!gcc -std=c11 % -o /tmp/a.out && time /tmp/a.out"
        elseif &filetype == 'cpp'
            execute "!g++ -std=c++11 % -o /tmp/a.out && time /tmp/a.out"
        elseif &filetype == 'java'
            execute "!javac -encoding utf-8 %"
            execute "!time java %<"
        elseif &filetype == 'sh'
            :!%
        elseif &filetype == 'python'
            execute "!time python3 %"
        else
            redraw
            echohl WarningMsg
                echo strftime("   ❖  不支援  ❖ ")
            echohl NONE
        endif
    endif
    execute "ALEEnable"
endfunction

Demo Demo

Completion (補全插件)

Demo

Indent highlight (縮排顯示插件)

Plugin 'Yggdroot/indentLine'
let g:indentLine_setColors = 0
let g:indentLine_char = ''
nnoremap <F3> :IndentLinesToggle<CR>

Line number (行號)

  • 檔案: .vimrc
  • 混和版本: 目前行使用絕對行,其他行使用相對行
  • <F2> 開關顯示
nnoremap <F2> :set norelativenumber!<CR>:set nonumber!<CR>
:set number
:set relativenumber

Markdown preview (Markdown預覽插件)

let g:mkdp_path_to_chrome = "cygstart /chrome.lnk"
let g:mkdp_auto_start = 0

Demo

Remove trailing white space (移除空白)

  • 檔案: .vimrc
  • 來源: Vigil <vim5632@rainslide.net>
  • 自動在寫檔前把行末空白、文件末空行刪除
function RemoveTrailingWhitespace()
    if &ft != "diff"
        let b:curcol = col(".")
        let b:curline = line(".")
        silent! %s/\s\+$//
        silent! %s/\(\s*\n\)\+\%$//
        call cursor(b:curline, b:curcol)
    endif
endfunction
autocmd BufWritePre * call RemoveTrailingWhitespace()

Surround (加上括號)

  • 檔案: .surround.vim
  • 如果輸入字元不支援會顯示 ❖ 不支援字元 ❖
  • <M-s>x: 在光標所在的單字左右加入x, 其中x 為 ' , " , ( , [ , { , <
  • <M-s>x: 在 visual mode 則在選取的左右加入
  • n<M-s>x: 加在包含光標以後 n 個單字, n 為整數
  • <M-d>x: 若有包在 x 裡面則刪除字元 x
  • <M-f>xy: 用 y 取代 x

Terminal (內部終端機插件)

nnoremap <C-t> :ConqueTermVSplit bash<CR>

Demo

Time (顯示時間)

  • 檔案: .keymap.vim
  • 在 normal mode 按 <M-t> 就會顯示現在時間
function! DateAndTime()
    redraw
    echohl WarningMsg
        echo strftime("   ❖  現在時間 %H:%M ❖ ")
    echohl NONE
endfunction
nnoremap <M-t> :call DateAndTime()<CR>

Demo