How do I apply a macro to every non-blank line in the file while skipping empty lines?
:g/./norm @q
Combining the :global command with :normal lets you run a macro on every non-blank line in one shot.
:g/./norm @q
Combining the :global command with :normal lets you run a macro on every non-blank line in one shot.
:'<,'>sort!
The sort! command sorts the selected lines in reverse (descending) order.
readfile() and writefile()
readfile({path}) reads a file and returns its contents as a list of lines, and writefile({list}, {path}) writes a list of lines back to disk.
:set keywordprg
The K key in normal mode looks up the word under the cursor using the program defined by keywordprg.
systemlist()
systemlist({cmd}) runs a shell command and returns the output as a list of strings, one per line — unlike system() which returns everything as a single string
:doautocmd
:doautocmd fires any autocommand event manually, exactly as if that event had occurred naturally.
:silent! %normal @q
Combining :silent!, the % range, and :normal @q gives you a powerful pattern for applying a macro across an entire file while gracefully skipping lines that don
:sign define and :sign place
Vim's built-in sign system lets you define custom symbols and place them in the sign column — the narrow gutter to the left of line numbers.
:0put
The :put Ex command inserts register contents as a new line below the specified line number.
:set formatexpr=MyFormat()
The formatexpr option lets you replace Vim's built-in line-breaking logic for the gq operator with your own Vimscript expression or function.
:w !cmd
The :w !cmd command sends the buffer's contents as standard input to an external command without modifying the buffer.
<C-f> (in command-line mode)
When you're already on the Vim command line and realize you need complex edits — inserting text from multiple positions, reordering arguments, or referencing
<C-r>= (command-line mode)
Just like = lets you insert evaluated expressions in insert mode, you can use it inside an Ex command on the command line to embed any Vimscript expression resu
:lua =expr
In Neovim, prefixing a Lua expression with = on the :lua command line evaluates it and pretty-prints the result using vim.
:%s/\n/ /g
Using \n in the pattern of :substitute matches the newline character at the end of each line, letting you join lines with any separator you choose — something
:nnoremap <expr> {key} {expression}
The argument to any map command (:nmap, :inoremap, etc.
config #config #macros #insert-mode #normal-mode #ex-commands
:g/^\s*$/d
The global command :g/^\s$/d removes every line that is empty or contains only whitespace — a common cleanup task when tidying up code, configuration files, o
:setfiletype {filetype}
:setfiletype {filetype} sets the filetype option for the current buffer only if it has not already been set.
:set nomodifiable
:set nomodifiable locks a buffer so that no changes can be made at all — not even temporary ones.
:set formatprg=<cmd>
When formatprg is set, the gq operator pipes the selected text through that external program and replaces it with the program's output.