Skip to content

Instantly share code, notes, and snippets.

View petergi's full-sized avatar
💭
Just Busy Living On The Side Of A Square

Peter Giannopoulos petergi

💭
Just Busy Living On The Side Of A Square
View GitHub Profile
@petergi
petergi / Globally replace a string in Neovim.txt
Created May 2, 2024 18:34
Replaces all occurrences of "PETER" with "pete".
:g/PETER/s//pete/g
"equivalent to:
:%s/PETER/pete/g

:1,10co20 "copy lines 1 through 10 after line 20"

:4,15t$ "copy lines 4 through 15 to end of document (t == co)"

:-t$ "copy previous line to end of document"

:m0 "move current line to line 0 (i.e. the top of document)"

:.,+3m$-1 "move current line +3 subsequent to the last line -1 (i.e. next to last)"

@petergi
petergi / Fast delete of all lines matching a pattern in Neovim.md
Last active July 4, 2024 22:34
When a line is deleted, it is first copied to a register—since no register is specified, the default (unnamed) register is used. If many thousands of lines are deleted, the copy process can take a significant time. To avoid that waste of time, the blackhole register (_) can be specified because any copy or cut into the blackhole register perform…

:g/pattern/d \_

@petergi
petergi / Add text to the end of a line that begins with a certain string in Neovim.txt
Created May 2, 2024 18:21
Add text to the end of a line that begins with a certain string
:g/^pattern/s/$/mytext
@petergi
petergi / Reverse a file in Neovim.txt
Last active May 2, 2024 18:19
Reverse entire file. The first pass of :g marks every line matching {pattern}, while the second pass (again starting at the file's beginning and proceeding to the end) performs the [cmd]. Note: if :g processed lines in any order other than from top to bottom, this command would not work.
:g/^/m0
@petergi
petergi / Comment lines containing "DEBUG" statements in a C program using Neovim.txt
Created May 2, 2024 18:14
Comment lines containing "DEBUG" statements in a C program
" using :normal
g/^\\s\*DEBUG/exe "norm! I/\* \\<Esc>A \*/\\<Esc>"
" using :substituting
g/^\\s\*DEBUG/s!.\*!/\* & \*/!
@petergi
petergi / Double space a file in Neovim.txt
Created May 2, 2024 18:12
Double space the file (^ is start of line which matches each line)
:g/^/pu =\\"\\n\\"
" Alternative (:put inserts nothing from the blackhole register)
:g/^/pu \_
@petergi
petergi / Delete all blank lines in Neovim.txt
Created May 2, 2024 18:10
Delete all blank lines (^ is start of line; \s* is zero or more whitespace characters; $ is end of line)
:g/^\\s\*$/d
@petergi
petergi / Delete all lines that do not match a pattern in Neovim.txt
Created May 2, 2024 18:08
The commands shown are equivalent (v is "inverse").
:g!/pattern/d
:v/pattern/d
@petergi
petergi / Delete all lines matching a pattern in Neovim.txt
Created May 2, 2024 18:07
Delete all lines matching a pattern in Neovim
:g/pattern/d