Vim
, 簡報
大二暑假不小心都拿來研究 vim – my_vim
十月把自己所有的設定包成一個 plugin aben20807.vim
寒假到開學寫了三個bug滿滿的 plugin commenter、surrounder、runner
還有一個寫到一半不知道何時繼續… snipmater
有任何問題都歡迎提出~
大型專案(?)、視窗類(C#, Qt)會較推薦 IDE
還有一些檔案相依性很大的 e.g. Java
不想等開啟時間就來用 vim 吧
主要還是看個人
或是看一下附近的人
Q:
A:
用 ssh?
利用 REPL 寫檔 e.g. perl、python?
https://superuser.com/a/464082
-u NONE
)$ vim
:q[uit]<enter>
:wq<enter> " write and quit "
:q[uit]!<enter> " 強制! "
:w[rite]<enter>
$ curl -fLo ~/xxx.c \
https://gist.githubusercontent.com/aben20807/c6a637586db809220d2af7c8029ce646/raw/ffa899f651c50763757bf4aa693ce9e45802020f/xxx.c
hackmd 也有 vim mode !!!!
h← j↓ k↑ l→
投影片用 hjkl 也可以移動 !!!!
w: 下一個單字頭
hello world -> hello world
e: 往後單字尾
hello world -> hello world
b: 往前單字頭
hello world -> hello world
fx/Fx 搜尋同一行的 x 字元
hello woxld -'fx'-> hello woxld
heylo world -'Fy'-> heylo world
4j
3w
2fx
gg 文件頭
G 文件尾
H/M/L 視窗上/中/下 (scrolloff)
^ 移到行首
$ 移到行尾
i/I, a/A, s/S, o/O
v/V/ctrl-v
ESC/ctrl-[ (回到 Normal)
hello world -'i'-> hello world
hello world -'I'-> hello world
hello world -'a'-> hello world
hello world -'A'-> hello world
hello world -'s'-> helo world
hello world -'S'->
hello world hello world hello world -'o'-> hello world hello world hello world
hello world hello world hello world -'O'-> hello world hello world hello world
複製 y
貼上 p
剪下 d/x
整行複製 Y/yy
整行剪下 dd
上一步 u
重做 ctrl + r
進入 Insert模式打一些字
不打字就回到 Normal模式
試試上面的操作
既然是練習就不管手腕了請按多次一點 OuO
viw Pneumonoultramicroscopicsilicovolcanoconiosis
vi"
ggVG
"+y
$ vim --version | grep clipboard
$ sudo apt install vim-gnome
gg=G
gv
~
ctrl-z
$ fg
通常會先打一個 :
:n " 移到第 n 行 , n ∈ 非負整數 "
:-n " 向上移 n 行 "
:+n " 向下移 n 行 "
:e[dit] filename " 編輯或新增 "
:vs[plit] [filename] " 不打filename就是原文件 "
搭配 :bn[ext], :bp[rev] 在多檔間移動
搭配 ctrl-w h, l 在多分割間移動
:h[elp] {keyword}
:set nu[mber]
:set nonu[mber]
$ vim ~/.vimrc
:h vimrc
nmap i k " n 表示 Normal, 也有 i, v... "
nmap j h
nmap k j
" i "
" j k l "
怎麽大家都變成 h 了 @@
i map k map j map h
nnoremap i k " nore 表示 no recursive "
nnoremap j h
nnoremap k j
" i "
" j k l "
想改 wasd 或用方向鍵都可以
最重要的是自己順手
不過初學都會建議使用 hjkl
手的移動範圍較小
set ttimeoutlen=100
set number
" hilight current line "
set cursorline
" hilight serach result "
set hlsearch
" show result before typing finished "
set incsearch
" igonre case "
set ignorecase
怎麼搜尋?
/xxx、#、* 加 n、N
取消搜尋結果
:noh
" tab size "
set tabstop=4
" regard multiple spaces as tab "
set softtabstop=4
" let tab become space "
set expandtab
" indent size "
set shiftwidth=4
" auto indent "
set autoindent
set smartindent
" let line cannot wrap "
set nowrap
$ curl -fLo ~/.vim/colors/ouo.vim --create-dirs \
https://raw.githubusercontent.com/aben20807/aben20807.vim/master/colors/ouo.vim
$ vim ~/.vimrc
syntax on
set t_Co=256
colorscheme ouo
" show line numbers, use <F2> to switch "
nnoremap <F2> :set norelativenumber!<CR>:set nonumber!<CR>
set number
set relativenumber
" return to last edit position when opening files "
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line('$') |
\ exe "normal! g`\"" |
\ endif
" let cursor in the middle of screen when entering vim "
autocmd VimEnter * :exec "normal! \zz"
" remove trailing whitespace when writing a buffer. "
" From: 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()
nnoremap <C-a> ggVG
" tab indent "
vmap <TAB> >gv
vmap <S-TAB> <gv
" move among split windows "
nnoremap <silent> <C-Right> <C-w>l
nnoremap <silent> <C-Left> <C-w>h
nnoremap <silent> <C-Up> <C-w>k
nnoremap <silent> <C-Down> <C-w>j
" command abbreviate "
cnoreabbrev WQ wq
cnoreabbrev Wq wq
cnoreabbrev W w
cnoreabbrev Q q
cnoreabbrev db bd
" exit vim "
nnoremap <F10> :exec "q!"<CR>
nnoremap <F11> :exec "up"<CR>
nnoremap <F12> :exec "x"<CR>
function! SpacesToTabs()
execute "set expandtab"
execute "retab"
execute "up"
endfunction
autocmd BufRead,BufNewFile ~/col5/os/**/*.c call SpacesToTabs()
autocmd BufRead,BufNewFile ~/col5/os/**/*.h call SpacesToTabs()
autocmd VimLeave ~/col5/os/**/*.c execute "!astyle *.c *.h"
autocmd VimLeave ~/col5/os/**/*.h execute "!astyle *.c *.h"
針對不同的檔案類型做設定
e.g. HTML 縮排間隔跟 C++ 不一樣
" 檔名:html.vim "
setlocal tabstop=2
setlocal softtabstop=2
setlocal shiftwidth=2
" 檔名:cpp.vim "
setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
雖然 vundle 不錯但我們用 vim-plug
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
$ vim ~/.vimrc
set nocompatible
filetype off
" set the runtime path to include Vundle and initialize "
call plug#begin('~/.vim/plugged')
" 插件和設定放這裡(設定也可以放外面) "
Plug 'Yggdroot/indentLine' " 直接複製 github 作者/插件名 "
let g:indentLine_setColors = 0
let g:indentLine_char = '┊'
let g:indentLine_fileTypeExclude = ['help', 'text']
nnoremap <F3> :IndentLinesToggle<CR>
call plug#end()
filetype plugin indent on
$ vim
:PlugInstall
:qa " 一次跳出多個視窗
$ mkdir ~/.vim/plugged/vim-addsemi/plugin -p
vim-addsemi/
doc/
vim-addsemi.txt
plugin/
vim-addsemi.vim
README
LICENSE
$ vim ~/.vim/plugged/vim-addsemi/plugin/vim-addsemi.vim
function! AddSemiColon()
let b:curline = line(".")
let b:curcol = col(".")
execute "normal! A;\<ESC>"
call cursor(b:curline, b:curcol)
endfunction
function! SetUpKeyMap()
execute "nnoremap <C-e> :<C-u>call AddSemiColon()<CR>"
execute "inoremap <C-e> <ESC>l:<C-u>call AddSemiColon()<CR>i"
endfunction
call SetUpKeyMap()
$ vim ~/.vimrc
Plug '~/.vim/plugged/vim-addsemi'
$ vim t.c