Personal Knowledge Base

A personal knowledge base

Vim

#Auto-Rebuild on Write

Depends on Neoterm plugin:

:au BufWritePost *.m T ./bin/build prince

Customise the file pattern (*.m) and the command as desired.

#Open URL under cursor

In normal mode type gx to open the URL in the default browser.

#Rebuild Project on Write

Using the terminal built into Neovim you can trigger a build of a project whenever you write a file. In the example below, whenever a Rust file is written cargo build is run. The example makes use of neoterm, which provides the T command to make dealing with the Neovim terminal easier.

autocmd! bufwritepost *.rs T cargo build

#Use vim as a Syntax Highlighted Pager

vim/neovim come with a script that makes vim behave as a pager (like less). It's basically a syntax highlighted version of less. Great for inspecting the content of source files. On my Arch box it's at /usr/share/nvim/runtime/macros/less.sh.

#Underline the Current Line With Dashes

yyp<c-v>$r-

— http://vim.wikia.com/wiki/Underline_using_dashes_automatically#Using_a_mapping

#Convert Ruby Hash Syntax

%s/:\([a-z_]\+\) => /\1: /gc

#Convert Image tag to Rails Image Helper

%s/img src="site\/images\/\([^"]\+\)" width="\([^"]\+\)" height="\([^"]\+\)" class="\([^"]\+\)"\/>/%= image_tag '\1', :width => \2, :height => \3, :class => '\4' %>/

#Replace HTML Comments With ERB Comments

%s/<!--\(.*\)-->/<%#\1%>/

#Repeat Macro to End of File

%normal @q

#Open Paths With Line Numbers in Vim

See: http://www.wezm.net/technical/2015/09/vim-open-file-at-colon-line-number/

#Specify File Encoding on Command Line

This example was used to open a UTF-16LE file generated by iTunes (Export Playlist... > Unicode Text):

vim -c "e ++enc=utf-16le ++ff=mac" file.txt

#Perform Replacement on 2nd Line of all Buffers

bufdo 2s/foo/bar/ | w

#Virtual Replace Mode

Virtual Replace mode is similar to Replace mode, but instead of replacing actual characters in the file, you are replacing screen real estate, so that characters further on in the file never appear to move.

— http://vimdoc.sourceforge.net/htmldoc/insert.html#Virtual-Replace-mode

Last modified: 10 January 2023