Vim
Write to read-only file
Section titled “Write to read-only file”:w !sudo tee "%"Delete any number of lines
Section titled “Delete any number of lines”Shift+V from point of cursor and press D key to delete.
Search and replace
Section titled “Search and replace”:%s/search/replace/g-
Open Telescope with fuzzy finding:
<leader>fw -
Type in search term
-
Add all results to quickfix list at once using
<Ctrl-q>or alternatively, press Tab on every desired entry and press<Alt-q> -
Type the following command:
:cfdo %s/search/replace/g -
Write all changes:
:wa
Disable search highlight
Section titled “Disable search highlight”:nohPrepend multiple lines with a character
Section titled “Prepend multiple lines with a character”- Shift+V from point of cursor to enter Visual Block mode
- Move up/down to select multiple lines
:to enter command modes/^/#/gto replace first character on the lines with a#
The prompt will look something like this when all done:
:'<,'>s/^/#/gFormat to certain column width
Section titled “Format to certain column width”-
Set text width to desired width:
:set textwidth=80 -
Move to start of file:
gg -
Format text from start to the end:
gqG
Measure start-up speed
Section titled “Measure start-up speed”$ hyperfine "nvim --headless +qa" --warmup 5Benchmark 1: nvim --headless +qa Time (mean ± σ): 40.3 ms ± 1.8 ms [User: 16.1 ms, System: 8.0 ms] Range (min … max): 37.2 ms … 46.3 ms 58 runs$ nvim --startuptime startup.log -c exit && head -n 21 startup.log--- Startup times for process: Primary/TUI ---
times in msec clock self+sourced self: sourced script clock elapsed: other lines
000.000 000.000: --- NVIM STARTING ---000.070 000.070: event init000.197 000.126: early init000.368 000.172: locale set000.414 000.046: init first window011.338 010.925: inits 1011.346 000.008: window checked011.350 000.004: parsing arguments011.733 000.028 000.028: require('vim.shared')011.798 000.031 000.031: require('vim.inspect')011.836 000.028 000.028: require('vim._options')011.837 000.100 000.041: require('vim._editor')011.838 000.155 000.027: require('vim._init_packages')011.840 000.334: init lua interpreter012.142 000.302: --- NVIM STARTED ---:Lazy profileRe-indent according to file type
Section titled “Re-indent according to file type”- A single line:
== - A range of lines:
<range>== - Entire file:
gg=G- Probably won’t be as intelligent as a language-specific formatter
Reverse lines
Section titled “Reverse lines”:g/^/m0More information here.
Use visual mode to selectively reverse ((:g|:global) command is still required).
Useful for re-ordering commits with git rebase -i if you know you made the most relevant commit first and subsequent minor changes afterwards.
Undo options
Section titled “Undo options”:set spell:set spell!:set formatoptions&Diff two open buffers
Section titled “Diff two open buffers”Run :diffthis in both of the buffers. Run :diffoff to disable.
Cancel a Git commit with a non-zero exit
Section titled “Cancel a Git commit with a non-zero exit”:cqOpen new file
Section titled “Open new file”:e <file path>When using relative paths, your actual working directory needs to match as well
Get the file path to the currently opened buffer
Section titled “Get the file path to the currently opened buffer”- Enter Insert mode
- Press
<Ctrl-r>and%
The active line should now contain the file path to the currently opened buffer, but remember that this is relative to the current working directory. So, whereever Vim was opened will be the root.