How to customize vim editor

|

This post explains basic configurations of vim editor. I referenced the following sites for this post.

Copy below and paste to ~/.vimrc.

" Indentation options
set autoindent
set expandtab
set shiftwidth=4
set smarttab
set ts=4
set sts=4

autocmd FileType c,cpp setlocal cindent
autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4

" Search options
set hlsearch
set ignorecase
set incsearch
set smartcase

" Text rendering options
set display+=lastline
set encoding=utf-8
set fileencoding=utf-8
set linebreak
set scrolloff=5
set sidescrolloff=5
set wrap

if has("syntax")
    syntax on
endif

" User interface options
set laststatus=2
set ruler
set wildmenu
set cursorline
set number
set mouse=a
set title
set showmatch
set showcmd
set list
set listchars=tab:›\ ,trail:,extends:#,nbsp:.

" Miscellaneous options
set backspace=indent,eol,start
set backupdir=~/.cache/vim/backup
set confirm
set hidden
set history=1000
set wildignore+=.pyc,.swp

set undodir=~/.cache/vim/undo-dir
set undofile

if !isdirectory($HOME."/.cache")
    call mkdir($HOME."/.cache", "", 0770)
endif
if !isdirectory($HOME."/.cache/vim")
    call mkdir($HOME."/.cache/vim", "", 0700)
endif
if !isdirectory($HOME."/.cache/vim/backup")
    call mkdir($HOME."/.cache/vim/backup", "", 0700)
endif
if !isdirectory($HOME."/.cache/vim/undo-dir")
    call mkdir($HOME."/.cache/vim/undo-dir", "", 0700)
endif

" Set custom statusline
set statusline=%F\ %y%m%r\ %=Line:\ %l/%L\ [%p%%]\ Col:%c\ Buf:%n

" Return to last edit position when opening files
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "norm g`\"" |
\ endif

Indentation

  • set autoindent: New lines inherit the indentation of previous lines.
  • set expandtab: Convert tabs to spaces.
  • set shiftwidth=4: When shifting, indent using four spaces.
  • set smarttab: Insert “tabstop” number of spaces when the “tab” key is pressed.
  • set ts=4: Indent using four spaces.
  • set hlsearch: Enable search highlighting.
  • set ignorecase: Ignore case when searching.
  • set incsearch: Incremental search that shows partial matches.
  • set smartcase: Automatically switch search to case-sensitive when search query contains an uppercase letter.

Text rendering

  • set display+=lastline: Always try to show a paragraph’s last line.
  • set encoding=utf-8: Use an encoding that supports unicode.
  • set fileencoding=utf-8: Change the output encoding of the file that is written.
  • set linebreak: Avoid wrapping a line in the middle of a word.
  • set scrolloff=5: The number of screen lines to keep above and below the cursor.
  • set sidescrolloff=5: The number of screen columns to keep to the left and right of the cursor.
  • set wrap: Enable line wrapping.

User interface

  • set laststatus=2: Always display the status bar.
  • set ruler: Always show cursor position.
  • set wildmenu: Display command line’s tab complete options as a menu.
  • set cursorline: Highlight the line currently under cursor.
  • set number: Show line numbers on the sidebar.
  • set mouse=a: Enable mouse for scrolling and resizing.
  • set title: Set the window’s title, reflecting the file currently being edited.
  • set showmatch: Highlight the matching brackets or braces.
  • set showcmd: Show the last command entered in the bottom line.

Miscellaneous

  • set backspace=indent,eol,start: Allow backspacing over indention, line breaks and insertion start.
  • set backupdir=~/.cache/vim/backup: Directory to store backup files.
  • set confirm: Display a confirmation dialog when closing an unsaved file.
  • set hidden: Hide files in the background instead of closing them.
  • set history=1000: Increase the undo limit.
  • set wildignore+=.pyc,.swp: Ignore files matching these patterns when opening files based on a glob pattern.
  • set undodir=~/.cache/vim/undo-dir: Directory to store undo information files.
  • set undofile: Store the undo information in a file.

Comments