Trace: VIM Notes

VIM Notes

VIM Notes

If the cursor colour is not visible while a line is highlighted, change the setting of the terminal emulator. VIM does not set this.

VIM settings (vimrc)

Place the following in the file .vimrc in your home directory for some nice enhancements to VIM, the most significant being the status line, which I find immensely helpful.

" vim: ts=4 sw=4:
set nocompatible
" use syntax highlighting
syntax enable
" # f /usr/share/vim/vim8*/colors/
" blue.vim      default.vim  desert.vim   evening.vim   koehler.vim
" murphy.vim  peachpuff.vim  ron.vim    slate.vim  zellner.vim
" darkblue.vim  delek.vim    elflord.vim  industry.vim  morning.vim  pablo.vim
" shine.vim  torte.vim
" evening is a good scheme.  industry is brighter.
" colorscheme murphy
" Gentoo looks best with comment FG=grey and BG=Blue
" Endeavour looks best with FG=black and BG=blue
if match(readfile("/etc/os-release"), "Gentoo") != -1
	colorscheme ron
	highlight Comment ctermfg=Yellow ctermbg=Blue
elseif match(readfile("/etc/os-release"), "fedora") != -1
	colorscheme ron
	highlight Comment ctermfg=Grey ctermbg=DarkBlue
elseif match(readfile("/etc/hostname"), "t510") != -1
	colorscheme industry
	highlight Comment ctermfg=White ctermbg=DarkBlue
else
	colorscheme industry
	highlight Comment ctermfg=Cyan ctermbg=Blue
	"colorscheme murphy
	"highlight Comment ctermfg=Black ctermbg=DarkBlue
endif

" allow reformatting of paragraphs
filetype indent on
" but turn off general plug-in options or it causes pasted stuff to be all
" comments
filetype plugin off
set formatoptions=tcqnl1
"
" listen to mode line in first or last fives lines of files
set modeline
set modelines=5

" Default tab spacing.  Using a multiple of 8 makes things look better when cat'd from the CLI.
set ts=4
set sw=4
set noexpandtab

" show matching brackets
set showmatch

" allow mouse clicks to move cursor
"set mouse=a
" disable all mouse interaction
set mouse=
set ttymouse=

function! TotalBuffers()
	return len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
endfunction
"
" User1 is highlight group 1 (%1*)
" User2 highlight group is dim, for less important details
" StatusLine is the colour of the overall status line
if filereadable("/etc/pacman.conf")
    hi User1		ctermfg=Black		ctermbg=DarkCyan
    hi User2		ctermfg=DarkMagenta	ctermbg=Grey
    hi User3		ctermfg=Blue		ctermbg=Grey	cterm=inverse
	hi StatusLine	ctermfg=Black		ctermbg=Grey	cterm=none
	runtime! archlinux.vim
elseif filereadable("/etc/yum.conf")
	" Oracle, Red Hat, CentOS
    hi User1		ctermfg=Black		ctermbg=Grey
    hi User2		ctermfg=Blue		ctermbg=Grey
    hi User3		ctermfg=White		ctermbg=Red
	hi StatusLine	term=reverse cterm=reverse
else
    " Gentoo and anything else
    hi User1		ctermfg=Black		ctermbg=DarkCyan
    hi User2		ctermfg=DarkMagenta	ctermbg=Grey
    hi User3		ctermfg=Blue		ctermbg=Grey	cterm=inverse
	hi StatusLine	ctermfg=Black		ctermbg=Grey	cterm=none
endif
" define a status line with useful details and nice colours
set statusline=%<									" clear the status line
set statusline+=%.50F						    	" show max 50 chars of full file name
set statusline+=\ (file\ %n/%{TotalBuffers()}		" file buffer/total
set statusline+=%3*%M%* 							" if modified: User3 colour (%3*), show indicator (%M), end highlight (%*)
set statusline+=%R%H)								" show read-only and/or help flags if applicable, then close parentheses
set statusline+=%=									" switch to the right side
set statusline+=%17(%1*↕%5l%*\ %1*↔%5v%*%)		    " X/Y coordinates - %17(%) is superfluous, but shows an example of setting min. spacing for a group
set statusline+=\|%*%3p%%							" Show percentage through the file at cursor position
set statusline+=\|%2*CHR%5b%8((0x%B)%)%*			" Show the decimal and hex value of character under the cursor, fix the width of the entire segment
set statusline+=\|%2*%{&fileencoding?&fileencoding:&encoding}%*
set laststatus=2									" Always show the status line
" define CR to clear the last used search pattern
nnoremap <CR> :let @/ = ""<CR><CR>
" Remap Ctrl+arrow keys to stop them from deleting lines
map <ESC>[1;5A <C-Up>
map <ESC>[1;5B <C-Down>
map <ESC>[1;5C <C-Right>
map <ESC>[1;5D <C-Left>
" Some distros have messed up mappings of other common keys, so let's be sure they work.
map <ESC>[3~ <Delete>
map <ESC>[2~ <Insert>
map <esc>[6~ <PageDown>
map <Esc>[5~ <PageUp>
map <Esc>OH <Home>
map <Esc>OF <End>
" docs say map include all other modes, but it does not work that way in Alpine Linux for some reason.
" I need to explicitly also do imap for insert mode to work.
imap <ESC>[1;5A <C-Up>
imap <ESC>[1;5B <C-Down>
imap <ESC>[1;5C <C-Right>
imap <ESC>[1;5D <C-Left>
imap <ESC>[3~ <Delete>
imap <ESC>[2~ <Insert>
imap <esc>[6~ <PageDown>
imap <Esc>[5~ <PageUp>
imap <Esc>OH <Home>
imap <Esc>OF <End>
" When using tmux prevent Ctrl+arrow keys from deleting text in insert mode
if &term =~ '^screen'
	" tmux will send xterm-style keys when its xterm-keys option is on
	" Put in tmux.conf:  set-window-option -g xterm-keys on
	execute "set <xUp>=\e[1;*A"
	execute "set <xDown>=\e[1;*B"
	execute "set <xRight>=\e[1;*C"
	execute "set <xLeft>=\e[1;*D"
endif

set number											" Show line numbers
set cursorline										" Highlight the current line
hi CursorLine   cterm=underline					    " Using colours overrides syntax highlighting, so just use underline

" if I comment the encoding=utf-8 then files with UTF-8 in them may not appear, e.g., /boot/grub/grub.cfg
set encoding=utf-8
" the fileencoding one does not change how a file with UTF-8 appears on the screen; grub.cfg still came up as Latin1.
"set fileencoding=utf-8
set hlsearch

" Store swap files in /var/tmp/ because if they are stored where the file
" exists it can cause watchdogs like Puppet to restart a service (e.g., httpd)
" while you are simply viewing a file.  Ending the path in double-slashes is
" recommended so the swap file name will include the full path of the source
" file, ensuring uniqueness.
set directory=/var/tmp//

Status Line

The status line can be set in increments to aid in commenting and make it easier to decipher, e.g.:

set statusline=
set statusline+=%<"           Truncate the line at this spot if too long.  If given at the start, this clears the status line.
set statusline+=%="           Move to the right side

Here is a collection of status line codes.

Formatting Codes

Code Description
%< Truncate the line at the point this appears, if what follows is too long to fit.
%= Separator between left and right sides of the status line. Text is added from right-to-left after this.
%! Evaluate what comes next, most commonly meaning: run the named function, e.g., #!MyFunction()
Field size specification: %-#{minwidth}.{maxwidth}{item} — examples follow:
%#(…%) Fixed spacing of # characters for everything inside the parentheses. Notice that the closing parenthesis is %), not just the close by itself.
%# Format the item following the value (#) to a minimum of # characters, but with no maximum. Since there is no maximum given, the field can grow beyond the given characters if needed. If the content is smaller than the minimum size, then the content will be right-justified.
%-# Same as %#, but left-justified.
%.# Allow a maximum of # characters, but no minimum, for the item following. Specifying no minimum trims white space.
This is helpful if you want to show full file name up to a maximum of # characters, e.g., %.20F.
%0# Pad with leading zero, for numeric values.
%#* Highlight group #.
• If # is between 1 and 9 then this starts highlighting.
• If # is zero or not specified, this ends highlighting.

Value Codes

Code Description Variation How it differs
%b Decimal value of the character under the cursor. %B Hexadecimal
%c Cursor column number
%f File name as entered on the command line. %F Full path of file.
%h Help buffer flag, shown as “[help]” %H Help buffer flag, shown as “HLP”
%m Modified flag surrounded by square brackets. %M Modified flag without square brackets.
%n Buffer number, i.e., the file number when editing multiple files.
%l Cursor line (row) number
%r Read-only flag, shown as “[RO]” %R Read-only flag, shown as “RO”
%v Cursor virtual column number (usually the same as %c)

References

Command Notes

A list of commands that I find myself using frequently, but sometimes forget if I haven't used in a while.

Keys What they do
do In vimdiff, copy the other (right) side difference to the current (left) side. I think of it as diff other, but it actually stands for diff obtain.
dp In vimdiff, copy the current (left) side difference to the other (right) side. It stands for diff put.
:noh Clear highlighting.
/ Search forward
? Search backward
=
=G
=#=
highlight + =
The equal-sign will reformat the code within the scope specified. You can specify the scope in all of the typical ways, e.g.
• highlighting lines beforehand with Shift+V then pressing =
• entering a number + another equal-sign (e.g., =25= to indent 25 lines)
• pressing capital-G to format to the end of the file

Sometimes BASH files do not format properly with the prefix function. Not always, but sometimes.
So if you notice indentation is wrong and are using function myfunction { notation, try changing to myfunction() { notation.