ubuntu, updated
Vim: common settings
A short ~/.vimrc with the settings worth having on a server: line numbers, search, indentation, mouse and colors.
How settings work
Type a setting in vim with :set ... to try it. It’s gone when you close vim. To keep it, put it into ~/.vimrc (without the :). Each user has their own file; for root it is /root/.vimrc.
| What | Command |
|---|---|
| Turn a setting on / off | :set number / :set nonumber |
| Toggle | :set number! |
| Show the current value | :set tabstop? |
Reload .vimrc without restarting | :source ~/.vimrc |
A good server .vimrc
" --- display ---
syntax on " colors for config files and scripts
set number " line numbers
set ruler " line and column at the bottom
set showmode " show -- INSERT -- etc.
set background=dark " readable colors on a dark terminal
colorscheme habamax " clean dark theme (ships with Vim 9+)
" --- search ---
set hlsearch " highlight all hits
set incsearch " jump to hits while typing
set ignorecase " search case-insensitive...
set smartcase " ...unless the search contains capitals
" --- indentation: 2 spaces, never tabs ---
set expandtab " Tab key inserts spaces (important for YAML!)
set tabstop=2
set shiftwidth=2
set autoindent
" --- behaviour ---
set hidden " switch files without saving first
set backspace=indent,eol,start
set mouse= " let the terminal handle the mouse (easy copy)
set scrolloff=5 " keep 5 lines visible above/below the cursor
" Show tabs and trailing spaces (YAML/Netplan errors!)
set list listchars=tab:>-,trail:·
Settings worth knowing
| Setting | Does |
|---|---|
set number / set relativenumber | Line numbers / distance to the cursor (handy for 5dd) |
set paste | Paste without auto-indent (see copy and paste) |
set nowrap | Don’t wrap long lines (log files) |
set list | Show tabs and line ends; set nolist hides them |
set fileformat=unix | Convert Windows line endings (^M) when saving |
set spell | Spell checking |
colorscheme desert | Another color scheme (:colorscheme then Tab lists them) |
Per file type
Different indentation for different files, e.g. 4 spaces for shell scripts:
filetype plugin indent on
autocmd FileType sh setlocal shiftwidth=4 tabstop=4
autocmd FileType yaml setlocal shiftwidth=2 tabstop=2 expandtab
Settings for everyone
System-wide settings for all users go in /etc/vim/vimrc.local (create it). Users’ own ~/.vimrc still wins.
Note:
sudoeditandsudo -euse your own~/.vimrc.sudo vimuses root’s.