Vim - Configuration

Updated: 2021-12-20

Where to Set Configurations

  • Global config: /usr/share/vim/vimrc
  • Personal config: ~/.vimrc (Personal overrule global)
  • Inside vim: in normal mode, press : (Once exit the configs will be lost.)

Also:

  • To use a different config than ~/.vimrc: $ vim -u <vimrc>
  • To use factory setting: $ vim -u NONE

Configs

  • :syntax on: Enable syntax highlight:
  • :set ts=4: Set tab width
  • :set shiftwidth=4: Set shiftwidth (when using command like >>)
  • :set expandtab: Expand tab to space
  • :set laststatus=2: show statusline
  • :set laststatus=0: hide statusline
  • :set guioptions-=m: remove the menu
  • :set guioptions-=T: remove the toolbar
  • :set compatible: compatible
  • :set guifont=Courier\ 14: Change fonts: only work for gvim, space need to be escaped
  • :set guifont=Courier\ New\ 12, Arial\ 10: With alternative
  • :set guifont=*: show font list

statusline

:help 'statusline'
:set statusline format

:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\
[HEX=\%02.2B]\ [POS=%04l,%04v]\ [%p%%]\ [LEN=%L]

Set cursor horizontal and vertical highlights

:set cursorline
:highlight CursorLine guibg=lightblue ctermbg=lightgray
:set cursorcolumn

Hide

:set nocursorline
:set nocursorcolumn

Show/hide line number

:set number
:set numberwidth=XXX
:set nonumber

spell check

:set spell
:set spelllang=de
:set spelllang=en,da,de,it
:set spellsuggest=X

Change Color Scheme

:colorscheme <ColorScheme>

e.g. :colorscheme default

Find installed schemes in /usr/share/vim/vim<version>/colors

Traverse installed schemes, press tab button after the whitespace: :colorscheme <tab>

Highlight:

:highlight MyGroup ctermbg=red guibg=red gctermfg=yellow
guifg=yellow term=bold
  • ctermb: Background color in console
  • guibg: Background color in Gvim
  • ctermf: Text color in console
  • guifg: Text color in Gvim
  • gui: Font formatting in Gvim
  • term: Font formatting in console (for example, bold)

Hightlighting

Add syntax file to (e.g. pig.vim) ~/.vim/syntax/pig.vim

Add these to ~/.vimrc

augroup filetypedetect
    au BufNewFile,BufRead \*.pig set filetype=pig syntax=pig
augroup END

Show whitespace Characters

:set list
:set nolist

Toggle

:set list!

In Vim, list is a boolean option that defaults to off. If list is on, whitespace characters are made visible. The listchars option can be used to customize the way whitespace characters are shown. The default displays ^I for each tab, and $ at each EOL (end of line, so trailing whitespace can be seen). :help 'list'

The following example toggles list, then sets listchars to not display an end-of-line character, and to display > for the first character occupied by a tab, and - for any subsequent characters that the tab may occupy.

:set list!
:set listchars=tab:>-

Example .vimrc

syntax on
set ts=4
set shiftwidth=4
set expandtab
set backspace=2

How To Turn Off Search highlight

To turn off the previous search highlight:

:noh

Or

set nohlsearch

To toggle

set hlsearch!

Backspace(delete) Not Working

Symptom

the backspace does not work

Solution

Add this to ~/.vimrc:

set backspace=2

Example

" Highlight "bad" spaces
let c_space_errors=1

" Also highlight empty lines at EOF.
match ErrorMsg /\s\+$\| \+\ze\t/

" Enable incremental search.
set incsearch