I have seen that many people use the Arrow Key in Vim…
emmm…
Therefore, I will show my way in this article.
Of course, Your way is also welcome in the comment area.
(
<C-x> means
<Ctrl>x
<M-x> means
<Alt>x)
Contents
Quick Move Like Mouse
Don’t move your fingers!
-
j or
k or
h or
l: You can move the cursor without moving you fingers!
Vim can help you to get rid of the mouse, but NOT by the Arrow Key!
Unplug the Arrow Key in your keyboard or…buy a keyboard without Arrow keys! (Explain below) -
The <Leader> key is one of the most important key in Vim.
Let it just be <Space> please.
So you don’t have to move your fingers.
1 |
let mapleader = "\<space>" |
-
The <Ctrl> key is also one of the most important key in almost all software.
But…wait a minute…the most useless key <Caps Lock> sleep in the base line of the keyboard?
Please…swap them!
(By the Gnome Tweak Tools or the setxkbmap command.)
But…how can I switch the case of letters?
Besides <Shift>, there are many efficient ways in Vim. -
Buy a 60% keyboard with Unix Layout.
Unix Layout means… <Ctrl> is close to A’s left.
If you have a little money, just buy a HHKB!
Or…buy a ‘GH60’ is awesome because you can add a vim mode to your keyboard!
Build-in Keys
- <C-u> or <C-d>: Move half page up or down.
- gg or G: Move to the first line or the last line.
- ^ or $: Move to the ‘first’ column or the ‘last’ column in the current line.
- 66G or 6j: Move to the specific line by your eye’s quick calculation. (hhh)
- <C-o> or <C-i>: Move to the last or the next position.
-
* or /word: Move to the next or the last specific word.
The n and N will navigate you among those same words.
(Could be enhanced by incsearch.vim.) -
fa or Fa: Move to the next or the last a in the current line. ( fb, fc, …)
The ; and , will navigate you among those same characters.
No kidding you, f is my most frequently key. (Not the ‘Fxxx’ word.)
EasyMotion
OK. Now I assume you have fall in love with the
f key (not the ‘f’ word).
The vim-easymotion plugin will let you two get married, trust me.
1 2 3 4 5 6 7 8 |
" change the prefix from <Leader><Leader> to <Leader> map <Leader> <Plug>(easymotion-prefix) " only map what you need (in case of confiltion) let g:EasyMotion_do_mapping = 0 map <Leader>f <Plug>(easymotion-s) map <Leader>j <Plug>(easymotion-j) map <Leader>k <Plug>(easymotion-k) |
-
<Leader>fa: Move to any ‘a’ in the current page (NOT only the current line!).
All ‘a’ will be highlighted with different red characters, try to press one of them. -
<Leader>j: Move to any line below in the current page.
Likes 6j, but…without your eye’s quick calculation. (hhh) -
<Leader>k: Move to any line above in the current page.
Buffers
Just use the beautifully vim-airline and the handy vim-unimpaired and the powerful vim-ctrlspace and a little plugin vim-bbye.
- Buffer is just a buffer of the file’s content.
- Window is just a window which displays the buffer.
- Tab is just a container of windows.
Each tab has it’s own windows layout, which is similar to Workspace.
But all tabs share one buffers list, which is not similar to Workspace.
That is why we need vim-ctrlspace.
1 2 |
# so as not to be disturbed by Ctrl-S ctrl-Q in terminals stty -ixon |
发生错误,请稍后重试。 |
1 2 3 4 5 6 7 8 9 10 11 12 |
let g:CtrlSpaceDefaultMappingKey = "<C-s>" if executable("ag") let g:CtrlSpaceGlobCommand = 'ag -l --nocolor -g ""' endif " this save about 10ms init time let g:CtrlSpaceFileEngine = "~/.vim/pack/z_last_load/start/vim-ctrlspace/bin/file_engine_linux_amd64" " overwrite vim-unimpair's map "au FileType * nnoremap <silent> [b :CtrlSpaceGoUp<CR> "au FileType * nnoremap <silent> ]b :CtrlSpaceGoDown<CR> |
1 2 |
" Delete buffer without close window nnoremap <Leader>q :Bdelete<CR> |
- [b or ]b: Move to the last or the next buffer of the current tab.
- <Leader>q: Close the current buffer without close the window.
- <C-s>: Open the buffer list of the current tab. (Followed by f``j``k)
Directories
I also use NERDTree until I read this article which drive me to vim-dirvish
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
augroup dirvish_config autocmd! " Map `t` to open in new tab. autocmd FileType dirvish \ nnoremap <silent><buffer> t :call dirvish#open('tabedit', 0)<CR> \ |xnoremap <silent><buffer> t :call dirvish#open('tabedit', 0)<CR> " Map `gr` to reload. autocmd FileType dirvish nnoremap <silent><buffer> \ gr :<C-U>Dirvish %<CR> " Map `gh` to hide dot-prefixed files. Press `R` to "toggle" (reload). autocmd FileType dirvish nnoremap <silent><buffer> \ gh :silent keeppatterns g@\v/\.[^\/]+/?$@d _<cr>:setl cole=3<cr> augroup END |
- -: Open the current directory in a new buffer.
- t: Open the selected file in a new tab.
- :!mkdir %foo: Create directories.
- :e %foo.txt: Create files.
- :'<,'>!xargs du -hs: Pipe to :! to see inline results.
More powerful quick jump
Keywords
CtrlSF
1 2 3 4 |
let g:ctrlsf_follow_symlinks = 0 nmap <C-f>w <Plug>CtrlSFCwordPath nnoremap <C-f>o :CtrlSFToggle<CR> |
YES! The ‘f’ key again!
- <C-f>n: Find the word under the cursor.
- <C-f>w: Find the word you type.
- <C-f>o: Toggle the quick search window.
EasyGrep
- <Leader>vv: Grep for the word under the cursor.
- <Leader>vr: Perform a global search and replace on the word under the cursor.
- :Grep [arg]: Grep for the specified arg, like <Leader>vv.
- :Replace [target] [replacement]: Perform a global search and replace, like <Leader>vr.
Tags
Replace Exuberant Ctags by Universal Ctags.
1 2 3 4 5 6 |
git clone https://github.com/universal-ctags/ctags --depth 1 cd ctags ./autogen.sh ./configure make sudo make install |
Install Pygments in Python.
1 |
pip install Pygments |
Expand Universal Ctags by Gtags.
1 2 3 4 5 6 |
wget http://tamacom.com/global/global-6.6.2.tar.gz tar xvf global-6.6.2.tar.gz cd global-6.6.2 ./configure --with-universal-ctags=/usr/local/bin/ctags make -j4 sudo make install |
Generate tag files automatically by Gutentags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
let g:gutentags_generate_on_new = 0 let g:gutentags_generate_on_write = 0 let g:gutentags_generate_on_missing = 0 let g:gutentags_cache_dir = expand('~/.cache/gutentags') let g:gutentags_exclude_project_root = [expand('~/.vim')] let g:gutentags_define_advanced_commands = 1 let g:gutentags_modules = [] if executable('ctags') "let g:gutentags_modules += ['ctags'] endif if executable('gtags-cscope') && executable('gtags') let g:gutentags_modules += ['gtags_cscope'] endif if executable('rg') let g:gutentags_file_list_command = 'rg --files' else let g:gutentags_modules = [] endif " Universal Ctags support Wildcard in options. let g:gutentags_ctags_extra_args = ['--fields=*', '--extras=*', '--all-kinds=*'] let g:gutentags_ctags_extra_args += ['--output-format=e-ctags'] " If built-in parser exists for the target, it is used. " Else if pygments parser exists it is used. let $GTAGSLABEL = 'native-pygments' let $GTAGSCONF = expand('~/.vim/globalrc') |
Build-in Keys and vim-unimpaired
- <C-]> or <C-w>] or g<C-]>: Jump to tags with the name under the cursor.
- <C-t>: Return after a tag jump.
- [t or ]t: Jump to the previous or the next tag with the same name.
- :tag <tagname> or :tag /<pattern>: Jump to a tag with the specific name.
- :tag: Jump to a previously jumped tag location, which is stored in the tag stack.
- :tags: List the contents of the tag stack.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
" Appearance let g:Lf_StlColorscheme = 'solarized' let g:Lf_PopupColorscheme = 'solarized' let g:Lf_WindowPosition = 'popup' let g:Lf_PopupPreviewPosition = 'top' let g:Lf_StlSeparator = { 'left': '', 'right': '' } let g:Lf_HideHelp = 1 " Behavior let g:Lf_WorkingDirectoryMode = 'Ac' let g:Lf_PreviewScrollStepSize = 10 let g:Lf_PreviewResult = { \ 'File': 0, \ 'Buffer': 0, \} " Config keymaps in coc-fzf. let g:Lf_ShortcutF = '' let g:Lf_ShortcutB = '' let g:Lf_CommandMap = {'<C-UP>': ['<C-u>'], '<C-DOWN>': ['<C-d>']} |
OH NO! The ‘f’ key again!
- <C-f>f: Find functions.
- <C-f>t: Find tags.
- <C-f>l: Find lines.
- <C-f>b: Find buffers.
- <C-f>p: Find files.
- <C-f>u: Find the MRU files.
- <C-f>c: Find cmd history.
- <C-f>s: Find search history.
- <C-f>h: Find helps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
" Disable the default keymaps. let g:gutentags_plus_nomap = 1 " Get symbol (reference) under cursor. noremap <silent> <leader>gs :GscopeFind s <C-R><C-W><cr> " Get symbol definition under cursor. noremap <silent> <leader>gg :GscopeFind g <C-R><C-W><cr> " Get functions called by this function. noremap <silent> <leader>gd :GscopeFind d <C-R><C-W><cr> " Get functions calling this function. noremap <silent> <leader>gc :GscopeFind c <C-R><C-W><cr> " Get text string under cursor. noremap <silent> <leader>gt :GscopeFind t <C-R><C-W><cr> " Get egrep pattern under cursor. noremap <silent> <leader>ge :GscopeFind e <C-R><C-W><cr> " Get file name under cursor. noremap <silent> <leader>gf :GscopeFind f <C-R>=expand("<cfile>")<cr><cr> " Get files #including the file name under cursor. noremap <silent> <leader>gi :GscopeFind i <C-R>=expand("<cfile>")<cr><cr> " Get places where current symbol is assigned. noremap <silent> <leader>ga :GscopeFind a <C-R><C-W><cr> |
- <Leader>gc: Get functions calling this function.
- <Leader>gd: Get functions called by this function.
- <Leader>gs: Get this C symbol.
- <Leader>gg: Get this definition
- <Leader>gf: Get this file.
- <Leader>gi: Get files #including this file.
- <Leader>ge: Get this egrep pattern.
Preview
1 2 3 4 5 6 7 |
noremap <m-u> :PreviewScroll -1<cr> noremap <m-d> :PreviewScroll +1<cr> inoremap <m-u> <c-\><c-o>:PreviewScroll -1<cr> inoremap <m-d> <c-\><c-o>:PreviewScroll +1<cr> autocmd FileType qf nnoremap <silent><buffer> p :PreviewQuickfix<cr> autocmd FileType qf nnoremap <silent><buffer> P :PreviewClose<cr> |
- p or P: Open or close the preview window of the current quickfix-list item (tags).
- <M-u> or <M-d>: Scroll the preview window up or down remotely.
Semantic
Welcome to LSP!
Backend
Install cquery.
1 2 3 4 5 6 7 8 9 |
git clone https://github.com/cquery-project/cquery.git --recursive --depth 1 cd cquery git submodule update --init mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_EXPORT_COMPILE_COMMANDS=YES # if you have clang 5.0 installed # add -DSYSTEM_CLANG=ON make -j4 sudo make install |
Frontend
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
let g:LanguageClient_diagnosticsEnable = 0 let g:LanguageClient_selectionUI = 'quickfix' let g:LanguageClient_serverCommands = { \ 'c': ['cquery', '--init={"cacheDirectory": "' . expand('~/.cache/cquery') . '"}'], \ 'cpp': ['cquery', '--init={"cacheDirectory": "' . expand('~/.cache/cquery') . '"}'], \ } noremap <leader>td :call LanguageClient#textDocument_definition({'gotoCmd': 'vsplit'})<CR> noremap <leader>gt :call LanguageClient#textDocument_hover()<CR> noremap <leader>rr :call LanguageClient#textDocument_references()<CR> nnoremap <silent> <F2> :call LanguageClient#textDocument_rename()<CR> fun! LanguageClientStop_() LanguageClientStop let g:LanguageClient_autoStart=0 endfun command LCS call LanguageClientStop_() |
- <Leader>td: To Definition of the symbol under the cursor.
- <Leader>gt: Get Type of the symbol under the cursor.
- <F2>: Rename the symbol under the cursor.
好帅! (全英yoooooo)
再也不用上下左右键了x