Vim - Tips


(how to) make all tab windows equally high and wide

(how to) check current mode in vimscript

function! SetDiffMode()
  if &diff
    " ...
  endif
endfunction

(how to) count number of matches of a pattern

  1. http://vim.wikia.com/wiki/Count_number_of_matches_of_a_pattern

count matches:

:%s/pattern//gn

count lines with matches:

:%s/pattern//n

(how to) repeat previous search in history

//

(how to) paste the word under cursor to command line

<C-r><C-w>

(how to) tweak colorscheme for specific filetype

  1. https://robots.thoughtbot.com/writing-vim-syntax-plugins#syntax
  2. https://stackoverflow.com/a/15702132/3632318
  3. :help syn-match

say, I want to use different foreground color for Elixir typespecs.

create ~/.vim/syntax/elixir.vim to override current colorscheme settings:

hi Typespec guifg=#A0A0B0 gui=italic
syn match Typespec /^\s*@spec.*$/

syn match command wouldn’t work in ~/vim/after/ftplugin/elixir.vim for some reason.

(how to) get relative path of current file

  1. https://stackoverflow.com/a/24463362/3632318

https://stackoverflow.com/a/24463362/3632318

Although expand(‘%’) often works, there are rare occasions where it does not.

show current file path relative to PWD or path relative to home directory if current file is not in PWD:

:echo fnamemodify(expand('%'), ':~:.')

or else it’s possible to show absolute path if current file is not in PWD:

:echo fnamemodify(expand('%'), ':.')

(how to) wrap text on demand

  1. https://vim.fandom.com/wiki/Automatic_word_wrapping

gq command formats text which includes wrapping: