Chat

Read Tao

Play Pacman

Software Repository

Linux Ditributions

Chatbot Source Code

SNES Emulator and Games

PS2 Emulator and Games

DNS Tools

Music

Docs

 

VI Editor Tricks and Tips
Updated: Jun 2, 2024
Replace all occurrences in the next 20 lines from the current line only.
:+20s/foo/bar/g

The tricks I use in vi/vim are mostly the arcane flags.
:set nows

will not search past the top or bottom.
:set sw=4

will make a nice indentation shiftwidth, especially for using the indent command (>). Works great for programming,
especially with autoindent (:set ai). But when programming with autoindent, you often need to unindent one shiftwidth…
do that by typing control-D at the beginning of the line. You can go to the very beginning of an autoindented line with 0
control-D.
:set list
:set nolist

will turn on/off hidden characters, and show end of lines. Great for finding tabs or spaces at the end of a line.
:set nu

will turn on line numbering.
Of course, if you want actual line numbers in your file, in *nix you’d use
:%!cat -n

%
when pressed over a parenthesis, finds the matching parenthesis or brackets

move up to the top line of the block to be delete
mm (sets a marker “m”)
move down to the last line in the block
d`m (deletes to marker “m”, and that’s the grave below the tilde, not the back-quote)

Also, you can do use “ma” to mark the beginning line, “mb” to mark the ending line, and then:
:’a,’bs/FROM/TO/g
And if you add a c (confirm) to the end
:’a,’bs/FROM/TO/gc
you will get a Y/N to replace that instance or not, in case you don’t want to replace every occurrence. if you search like this
:’a,’b g/FINDME/ s/FROM/TO/gc
:.,$ g/FINDME/p will search from your current cursor position (.) to the end of the document ($) and get /regular expression/ print (i.e grep) inside of vi.

And, while we’re having fun with search and replace, ^ will match the beginning of a line, so if you mark as above, and then change the command to:
:’a,’bs/^/#/

you will have commented out a section of your code without having to insert a comment character independently on each line.
Reverse it with:
:’a,’bs/^#//

Also, you don’t have to use the / command as a separator. Anything typed after s will become the separator, so if you want to, say, change all your Windows paths to Unix paths, instead of starting with:
:%s/\\/\//g

which, while undeniably cool, can be more easily written as:
:%s;\\;/;g

Some examples of changing things on various lines:

# add ‘gronk’ to the end of every line
# 1 is line 1, $ is the last line
:1,$ s/$/gronk/
# put ‘bing’ at the start of lines 5-10
:5,10 s/^/bing/
# change foo to bar for all occurrences in the rest of
# the file from where the cursor is
:s/foo/bar/g

Invoke aspell
:!aspell -c %

Yank lines 50-60
:50,60yy

Some useful one liners
find ./ -name "*.bak" -exec rm {} \;
find . -inum (INODE NUMBER) -delete'
find . -exec grep -q {} \; -print
find . -name '*.avi' -exec sh -c 'mv "$0" "${0%.avi}.mp4"' {} \;
find . -name \*.xml -exec sh -c “svn propget svn:mime-type {} | grep -q application/xml” \; -exec svn propset svn:mime-type text/xml {} \;
ls *.psd | cut -d . -f 1 | xargs -L1 -i convert {}.psd {}.png