VIM
===
###### tags: `vim`
# 操作
[[Vim]vim的几种模式和按键映射](http://haoxiang.org/2011/09/vim-modes-and-mappin/)
# show all vim key binding
`:help map`
`:map`
`:imap`
`:vmap`
## [**VIM-tips**](https://github.com/vim-tw/vim-tips)
:**h key-notation 查看key mapping詳細訊息**
I:insert head of line
e:move to End of the word
E:move to the last char
ea:append end of word
- To delete forward up to character 'X' type dtX
- To delete forward through character 'X' type dfX
- To delete backward up to character 'X' type dTX
- To delete backward through character 'X' type dFX
多檔編輯時,每一個file是一個buffer,buffer的command都是以 b 開頭的:
:bn 編輯下一個開啟的buffer(file)
:bp 編輯上一個
:bd 刪除這一個
:b{數字} 編輯第N個
:badd open buffer without opening
# folding
`za`:展開/收折
`zr`:展開一階
`zm`:全部展開
`zi`:全部展開/收折
# mark
`m[alpha]`:mark name alapha
`'[alpha]`:jump to mark alpha
# file navigator
`:HE`:上下分隔
`:VE`:左右分隔
`-`:回到上一層
`s`:file sort
`x`:execute
`R`:rename
`D`:delete
# write,copy,move
`:[line1],[line2] w [line3]`:write line1~line2 into file
`:[line1],[line2] w >> [line3]`:append line line1~line2 into file
`:[line1],[line2] co [line3]`:copy line1~line2 to line3
`:[line1],[line2] mo [line3]`:move line1~line2 to line3
# substitute
`:1,$s/old/new/g`:replace all old to new
`:[line1],[line2]s/^/ /`:insert white space before 10~20 lines
`:%s/old/new/g`:replace old to new in all buffer
# marco
q\<register\>:record marco
q: :stop record marco
@\<register\>:use \<register\>marco
# history
`q:`or `q/`:command history
`:his`:history
# vim makefile
```
:map <f9> :make - map the F9 key to run make
:set makeprg - change what :make does
make will compile if you are using a Makefile. Use these to examine the compile errors:
:copen - open a mini-window with list of errors
- hit enter on an error to jump to line
:cclose - closes the mini-window
:cw - toggles the mini-window (if errors exist)
```
# vimdiff
\]c - next difference
\[c - previous difference
do - diff obtain
dp - diff put
zo - open folded text
zc - close folded text
:diffupdate - re-scan the files for differences
# [autocmd](http://vimdoc.sourceforge.net/htmldoc/autocmd.html)
show all command
# Plugins
:::warning
沒在用了,參考就好
:::
[rhysd](https://github.com/rhysd)**/**[vim-clang-format](https://github.com/rhysd/vim-clang-format)
An easy way to create the `.clang-format` file is:
clang-format -style=llvm -dump-config > .clang-format
**" map to <Leader>cf in C++ code**
normal mode下,\cf:clang format
visual mode下,\cf:clang format
**" Toggle auto formatting**
\C :開啟/關閉auto format
# [**fugitive.vim**](https://github.com/tpope/vim-fugitive)
pre:
git git-core[
](https://github.com/tpope/vim-fugitive)
## Gedit:
View any blob, tree, commit, or tag in the repository
## Gdiff:
git diff
在Gdiff視窗環境下
:diffget 從另一個diff窗得到該diff窗的內容。(:do)
:diffput 將目前游標位置(或選取)的內容put到另一個diff窗。(:dp)
conflit解決辦法:
http://vimcasts.org/episodes/fugitive-vim-resolving-merge-conflicts-with-vimdiff/
## Gstatus:
git status
在git status窗狀態下
在file的位置按下<->可以add file,再按一次可以reset
<enter>可以打開該file
<p>git add -patch( 可以一一檢視被更動的地方,並且可以決定是否要加入這次的 commit。)
<C>/Gcommit:commit
## Gblame:
git blame
brings up an interactive vertical split with `git blame` output. Press enter on a line to edit the commit where the line changed, or `o` to open it in a split. When you're done, use `:Gedit` in the historic buffer to go back to the work tree version.
Gmove:git mv Gremove:git rm
## Gwrite:
git add %
## Gread:
git checkout %
## Git [command]/G[command]:
use git command
## Git!:查看git命令說明
# [vim-gitgutter](https://github.com/airblade/vim-gitgutter)
`[h`,`]h`:jump to next hunk
# [vim-airline](https://github.com/vim-airline/vim-airline)
# [**ctrlp.vim**](https://github.com/ctrlpvim/ctrlp.vim)
Full path fuzzy **file**, **buffer**, **mru**, **tag**, **...** finder for Vim.
## **Basic Usage**
- Run `:CtrlP` or `:CtrlP [starting-directory]` to invoke CtrlP in find file mode.
- Run `:CtrlPBuffer` or `:CtrlPMRU` to invoke CtrlP in find buffer or find MRU file mode.
- Run `:CtrlPMixed` to search in Files, Buffers and MRU files at the same time.
# [yuratomo](https://github.com/yuratomo)/[**w3m.vim**](https://github.com/yuratomo/w3m.vim)
## **Usage**
**Open URL:**
input :W3m [url or keyword]
**Search Mode:**
input :W3m search-engine-name keyword
[search-engine-name]
alc : space alc
android : Android SDK
as3 : ActionScript 3.0
go : Go language
google : Google
java : JDK6
man : man
msdn : MSDN
perl : PERL
php : PHP
python : Python
rfc : RFC
ruby : Ruby
wikipedia : Wikipedia
yahoo : Yahoo
yahoodict : Yahoo dictionary
local : Local HTML file
**Open URL At New Tab:**
input :W3mTab [url or keyword]
**Open URL At Split Window:**
input :W3mSplit [url or keyword]
**Open URL At Vertical Split Window:**
input :W3mVSplit [url or keyword]
**Open Local Html File**
input :W3m local html-file-path
**Close:**
input :W3mClose
or
input :bd
**Copy URL To Clipboard:**
input :W3mCopyUrl
**Reload Current Page:**
input :W3mReload
**Change Url:**
input :W3mAddressBar
**Show External Browser:**
input :W3mShowExtenalBrowser
**Syntax Off:**
input :W3mSyntaxOff
**Syntax On:**
input :W3mSyntaxOn
**Change User-Agent**
input :W3mSetUserAgent (w3m|Chrome|Firefox|IE6|IE7|IE8|IE9|Opera|Android|iOS|KDDI|DoCoMo|SoftBank)
**Open from history:**
input :W3mHistory
**Clear history:**
input :W3mHistoryClear
## **Default Keymaps**
- <CR> Open link under the cursor.
- <S-CR> Open link under the cursor (with new tab).
- <TAB> Move cursor next link.
- <s-TAB> Move cursor previous link.
- <Space> Scroll down.
- <S-Space> Scroll up.
- <BS> Back page.
- <A-LEFT> Back page.
- <A-RIGHT> Forward page.
- = Show href under the cursor.
- f Hit-A-Hint.
- s Toggle Syntax On/Off.
- c Toggle Cookie On/Off.
- <M-D> Edit current url.
# [nhooyr](https://github.com/nhooyr)/[**neoman.vim**](https://github.com/nhooyr/neoman.vim)
## **Usage**
**Command**
The command is as follows:
Nman " display man page for <cWORD>
Nman [sect] page
Nman page[(sect)]
Nman path " if in current directory, start path with ./
<C-]>跳到游標所指的label的man page
<C-t>跳回
q:離開
\mv:Vman vertical
:Snman 3 printf "horizontal split
:Vnman 3 printf "verical split
:Tnman 3 printf "in a new tab
# [Chiel92](https://github.com/Chiel92)/[**vim-autoformat**](https://github.com/Chiel92/vim-autoformat)
pre:
clang-format,astyle,autopep8,yapf
自動排版程式碼
綁定<F3> ,存檔,就會自動編排
可以客製化
# [garbas](https://github.com/garbas)/[**vim-snipmate**](https://github.com/garbas/vim-snipmate)
強大的Code Snipes
用法:
開啟原始檔 for<tab> 就會自動展開for迴圈,按tab可以進行跳躍
for (i = 0; i < count; i++) {
/* code */
}
# [scrooloose](https://github.com/scrooloose)/[**syntastic**](https://github.com/scrooloose/syntastic)
# [jiangmiao](https://github.com/jiangmiao)/[**auto-pairs**](https://github.com/jiangmiao/auto-pairs)
<M- > :meta key 要靠函數將其調程alt
<CR>: Insert new indented line after return if cursor in blank brackets or quotes.
<BS> : Delete brackets in pair
<M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle)
<M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap)
<M-n> : Jump to next closed pair (g:AutoPairsShortcutJump)
<M-b> : BackInsert (g:AutoPairsShortcutBackInsert)
fast wrap:
在下括號的位置按下<ALT+e>
https://d2mxuefqeaa7sj.cloudfront.net/s_2641C33CCBEA5FD5A4EDE65CC5194E331E5C7657F41147BC3EF4F75D431A9B0B_1470901262115_file.png
https://d2mxuefqeaa7sj.cloudfront.net/s_2641C33CCBEA5FD5A4EDE65CC5194E331E5C7657F41147BC3EF4F75D431A9B0B_1470901570678_file.png
# [scrooloose](https://github.com/scrooloose)/[**nerdcommenter**](https://github.com/scrooloose/nerdcommenter)
詳細見:help nerdcommenter
[count]<leader>cc |NERDComComment|
- Comment out the current line or text selected in visual mode.
[count]<leader>cn |NERDComNestedComment|
- Same as cc but forces nesting.
[count]<leader>c<space> |NERDComToggleComment|
- Toggles the comment state of the selected line(s). If the topmost selected line is commented, all selected lines are uncommented and vice versa.
[count]<leader>cm |NERDComMinimalComment|
- Comments the given lines using only one set of multipart delimiters.
[count]<leader>ci |NERDComInvertComment|
- Toggles the comment state of the selected line(s) individually.
[count]<leader>cs |NERDComSexyComment|
- Comments out the selected lines with a pretty block formatted layout.
[count]<leader>cy |NERDComYankComment|
- Same as cc except that the commented line(s) are yanked first.
<leader>c$ |NERDComEOLComment|
- Comments the current line from the cursor to the end of line.
<leader>cA |NERDComAppendComment|
- Adds comment delimiters to the end of line and goes into insert mode between them.
[count]<leader>cu |NERDComUncommentLine|
- Uncomments the selected line(s).
# [terryma](https://github.com/terryma)/[**vim-multiple-cursors**](https://github.com/terryma/vim-multiple-cursors)
跳躍選取工具
https://github.com/terryma/vim-multiple-cursors/raw/master/assets/example1.gif?raw=true
" Default mapping
let g:multi_cursor_next_key='<C-n>'
let g:multi_cursor_prev_key='<C-p>'
let g:multi_cursor_skip_key='<C-x>'
let g:multi_cursor_quit_key='<Esc>'
" Map start key separately from next key
let g:multi_cursor_start_key='<F7>'
let g:multi_cursor_start_word_key='g<C-n>'
# [sjl](https://github.com/sjl)/[**gundo.vim**](https://github.com/sjl/gundo.vim)
歷史紀錄工具
快捷鍵<F5>
[官方教學](http://sjl.bitbucket.org/gundo.vim/)
# [davidhalter](https://github.com/davidhalter)/[**jedi-vim**](https://github.com/davidhalter/jedi-vim)
python 用
- Completion `<C-Space>`
- Goto assignments `<leader>g` (typical goto function)
- Goto definitions `<leader>d` (follow identifier as far as possible, includes imports and statements)
- Show Documentation/Pydoc `K` (shows a popup with assignments)
- Renaming `<leader>r`
- Usages `<leader>n` (shows all the usages of a name)
- Open module, e.g. `:Pyimport os` (opens the `os` module)
# [machakann](https://github.com/machakann)/[**vim-sandwich**](https://github.com/machakann/vim-sandwich)
**operator-sandwich**
A sandwiched text could be resolved into two parts, {surrounding} and {surrounded text}.
sa/sd/sr:字串替代(新增、刪除、替代)
**textobj-sandwich**
- Search and select a sandwiched text automatically: mapped to the key sequence `ib` and `ab`
- Search and select a sandwiched text with query: mapped to the key sequence `is` and `as`
ib and is selects {surrounded text}. ab and as selects {surrounded text} including {surrounding}s.
|<----ib,is---->|
{surrounding}{surrounded text}{surrounding}
|<-----------------ab,as----------------->|
# [andviro](https://github.com/andviro)/[**flake8-vim**](https://github.com/andviro/flake8-vim)
python code complexity checker
Disable/enable automatic checking of current file
:PyFlakeToggle
Run checks for current file
:PyFlake
Auto-fix pep8 errors for current file
:PyFlakeAuto
# [othree](https://github.com/othree)/[**vim-autocomplpop**](https://github.com/othree/vim-autocomplpop)
和snipmate搭配,會顯示出sinpmate當相關語法
用法:打大寫就會自動比對,接著按<tab>就會顯示
# [Valloric](https://github.com/Valloric)/[**YouCompleteMe**](https://github.com/Valloric/YouCompleteMe)
## Commands:
|command|desctiption|
|:------|:----------|
|YcmForceCompileAndDiagnostics|強制YCM重新編譯diagnostics並重新顯示|
|YcmDiags|將警告和error顯示|
|YcmShowDetailedDiagnostic|於目前cursor的地方顯示此行所有的diagnostic text|
|YcmDebugInfo|印出所有的debug information|
|YcmToggleLogs|開啟或關閉logfile視窗|
|YcmCompleter|呼叫YcmCompleter subcommand的前綴|
### YCM completer subscommand
goto:
`ctrl+o`:jump back
`ctrl+I`:jump forward
`:h jumplist` :for details
`:h quickfix` :more then one destionation for quicklist
|command|description|filetype|
|-------|-----------|--------|
|GoToInclude|Looks up the current line for a header|c, cpp, objc, objcpp
|GoToDeclaration|Looks up the symbol under the cursor and jumps to its declaration|c, cpp, objc, objcpp, cs, go, python, rust|
|GoToDefinition|Looks up the symbol under the cursor and jumps to its definition.|c, cpp, objc, objcpp, cs, go, javascript, python, rust, typescript|
|GoTo|Currently, this means that it tries to look up the symbol under the cursor and jumps to its definition if possible|c, cpp, objc, objcpp, cs, go, javascript, python, rust|
|GoToImprecise|Same as the GoTo command except that it doesn't recompile the file with libclang before looking up nodes in the AST.|c, cpp, objc, objcpp|
|GoToReferences|This command attempts to find all of the references within the project to the identifier under the cursor and populates the quickfix list with those locations.|javascript, python, typescript
|GoToImplementation|Looks up the symbol under the cursor and jumps to its implementation|cs
|GoToImplementationElseDeclaration|Looks up the symbol under the cursor and jumps to its implementation if one, else jump to its declaration.|cs
|GetType|Echos the type of the variable or method under the curso|c, cpp, objc, objcpp, javascript, typescript|
|GetTypeImprecise|Same as the GetType command except that it doesn't recompile the file with libclang before looking up nodes in the AST. |c, cpp, objc, objcpp|
|GetParent|Echos the semantic parent of the point under the cursor.|c, cpp, objc, objcpp|
|GetDoc|Displays the preview window populated with quick info about the identifier under the cursor. |c, cpp, objc, objcpp, cs, python, typescript, javascript, rust|
|GetDocImprecise|Same as the GetDoc command except that it doesn't recompile the file with libclang before looking up nodes in the AST.|c, cpp, objc, objcpp|
|FixIt|attempts to make changes to the buffer to correct diagnostics on the current line.|c, cpp, objc, objcpp, cs|
|RefactorRename \<new name\>|In supported file types, this command attempts to perform a semantic rename of the identifier under the cursor|javascript (variables only), typescript|
# taglist
<C-]>:go to defination
<C-t>:go back
# [vim-scripts](https://github.com/vim-scripts)/[**DoxygenToolkit.vim**](https://github.com/vim-scripts/DoxygenToolkit.vim)
:DoxLic 產生license
:DoxAuthor 產生author information
:Dox 對function/class doxscript
:DoxUndoc(DEBUG) 不產生名為DEBUG marco內的marco說明
:DoxBlock 產生doxygen block
# Markdown
## [plasticboy/vim-markdown](https://github.com/plasticboy/vim-markdown)
### Mappings
```
gx: 在同一個瀏覽器下打開cursor目前的link <Plug>Markdown_OpenUrlUnderCursor
ge: 在edit模式下打開link
<Plug>Markdown_EditUrlUnderCursor
]]: go to next header.
<Plug>Markdown_MoveToNextHeader
[[: go to previous header. Contrast with ]c.
<Plug>Markdown_MoveToPreviousHeader
][: go to next sibling header if any.
<Plug>Markdown_MoveToNextSiblingHeader
[]: go to previous sibling header if any.
<Plug>Markdown_MoveToPreviousSiblingHeader
]c: go to Current header.
<Plug>Markdown_MoveToCurHeader
]u: go to parent header (Up).
<Plug>Markdown_MoveToParentHeader
```
### commands
`:TableFormat`: Format the table under the cursor.
`:Toc`: create a quickfix vertical window navigable table of contents with the headers.
Hit <Enter> on a line to jump to the corresponding line of the markdown file.
`:Toch`: Same as :Toc but in an horizontal window.
`:Toct`: Same as :Toc but in a new tab.
`:Tocv`: Same as :Toc for symmetry with :Toch and :Tocv.
## [iamcco/markdown-preview.vim](https://github.com/iamcco/markdown-preview.vim)
Usage
Command:
MarkdownPreview
" open preview window in markdown buffer
MarkdownPreviewStop
" close the preview window and server
when MarkdownPreview command can't open preview window, you can use the MarkdownPreviewStop command before using MarkdownPreview command
Key Mapping:
```
nmap <silent> <F8> <Plug>MarkdownPreview " for normal mode
imap <silent> <F8> <Plug>MarkdownPreview " for insert mode
nmap <silent> <F9> <Plug>StopMarkdownPreview " for normal mode
imap <silent> <F9> <Plug>StopMarkdownPreview " for insert mode
```
# 文件格式
執行sh 會出現
```
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory
```
sol:
```
:set ff? 查看檔案格式
```
```
轉成unix style
:set ff=unix
```
# [vim-scripts/Conque-GDB](https://github.com/vim-scripts/Conque-GDB)
A gdb frontend in vim
# [chazy/cscope_maps](https://github.com/chazy/cscope_maps/blob/master/plugin/cscope_maps.vim)
```
find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name)
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find this text string
```
usage:
`ctrl + \ + c|d|e|f|g|i|s|t`
# vim tabluar
輸入下述內容:
```
|start|eat|left|
|12|5|7|
|20|5|15|
```
V 選取後, , 之後在下面繼續打, 都會自動排序. (若文字過長, 上面都會自動對齊)
:Tab/ 後面接要分隔的符號, ex: 上述是用 "|" 分隔, 所以輸入 :Tab/|, 程式若要排 "=", 則輸入 :Tab/=, 更多範例可見下述:
:Tab/:
:Tab/=
:Tab/:\zs # 只有排序 ":" 後面的文字