nvim-note

Cheat sheet

Redirect output to buffer

:r !ls

Navigate

C-O " Go to last pos in history
C-I " Go to next pos in history
`.  " Go to last edited pos. ( ` is go to mark, . is a special mark automatically setted to last changed pos.) 

" Tags 
C-] " jump into tag definition (need to use Ctags first)
C-T " jump back

" File navigate
:ls " view current opened files in buffer
:b xxx.xx  " fuzzy open current opened file
C-^ " last opened file

:edit . " Open curr dir tree
:find xxx " if set path+=** , it will recursive search and open file match xxx . 

Edit

Split / Tab

:sp [filename]
:10 sp " split with 10 line width
:vsp   " vertical split
<C-W>l,k,j,h " Go to left/right/up/down split
<C-W>p " Go to previous window
<C-W>T (shift+t) " move split to new tab

gt     " go to next tab
gT
#gt    " go to # tab (# is a number)

Plugin

surround.vim
ds[ " auto detect [] and delete it
cs[{ " auto detect [] and replace it to {}

" in visual mode
S[ " Add [] around selected text
EasyMotion
FZF + fzf.vim

Install bat to enable syntax highlighting in preview window. But note there is Install issue in ubunbu20.04

  • sudo apt install -o Dpkg::Options::="--force-overwrite" bat ripgrep
:FZF " fuzzy search file
:Rg " fuzzy search line
:h :Rg " Show other fzf.vim 
Coc
" insert mode
<c-j>,<c-k> " to jump around placeholder, when doing auto completion"
<c-space> " trigger completion of current word

" normal mode
<leader>rn " symbol rename
gr " show references
K " show doc of current symbol/method. Navigate the float window with <c-f>,<c-b>
<space>o " show outline list (I usually use it as a function/class list navigator)
<space>a " show diagnostics
<space>c " show coc commands you can use
<space>s " show symbol list

" visual/select mode
if " select inside current funtion block
af " select all current funtion block
ic " select inside current class block
ac " select all current class block
<leader>a " do CocAction on current select
defx.nvim

Settings Check

:checkhealth " Check if the env for nvim is set properly. E.g. node version, python version.
:checkhealth nvim-treesitter " Check the treesitter health. If you encounter some problems about treesittter, this will be useful.
:verbose map <key> " Show the mapping of this key and where its latest setting location.
:hi <Target>" Show one or all color highlighting settings. Like ":hi Pmenu".

Autoload Scripts

:help 'runtimepath'

~/.config/nvim
├── autoload
│   └── load.vim
├── ftplugin
│   └── ftypeAAA.vim
├── init.vim
├── plugin
│   └── autoload_test.vim
└── test

  • plugin/

    • The scripts are loaded when nvim start.
  • autoload/

    • The scripts are loaded only when its command is called.

    For example, the autoload/load.vim will be loaded after plugin/autoload_test.vim is executed.

    " plugin/autoload_test.vim
    call load#Hello()
    " autoload/load.vim
    function! load#Hello()
    echo "hello!"
    endfunction

    Note: It seems that we should use namespace to invoke the script under autoload .

  • ftplugin/

    • When a file is opened, script corresponded to the file type will be load

    For example, I edit a file called foo.txt. With :set ft? we can find it's a text type file. Then :set ft=ftypeAAA, it's ftypeAAA type now, and you can find nvim load ftypeAAA.vim immediately.

  • Reference


Script

  • Declare a function
" '!' is used to overwrite the func previously defined
function!  hello() 

echo "hello!"

endfunction

lua


Problems

  • Why Ctrl-V not enter visual mode?
    • Check if you are in Window ternimal first. The default setting of wt will map C-V to C-shift-V.
    • ref1, ref2