Otto background

Linux Hack of The Week #3: Vim Tricks

If you spend time in Linux then you will learn Vi. There are other great editors such as Nano, Pico, emacs, gedit, etc. But Vi is installed on almost every system you log in to.  Vi is also incredibly powerful when you dig deeper into its abilities. I have been using it for years, so much so that you will occasionally get an email from me ending with :wq! . Without further ado here are some of my favorite and most useful tricks for Vi.

Global find and replace

Vi will run sed commands inside of the editor. By issuing an ESC :%s/pattern/replace/g you will replace all occurrences of pattern in the file. If you leave off the g you will only replace the first occurrence on each line.

Change word

This is one I thought everyone knew, but I’m always surprised when I show it to someone after they hit xxxxxx i newWord. Go to the beginning of the word you are looking to change, and type cw . Then change the word and hit ESC

Search

If you want to search for a word in Vi you can hit ESC /  . Type the word or characters to search for and hit enter. To find the next word hit n , or b to go back.

Repeat

To repeat the last command used in vi just hit the period key. So tying together the last 2 commands . Search for the word “foo”, enter cw, change to bar hit ESC, hit n to go the next occurence and hit. to change that to bar .

Another way to repeat a command is to make a preemptive attack. If you want to delete a line you can use dd, if you want to delete 10 lines, you can enter 10dd . Enter a number ahead of a command will repeat it that many times.

Run shell commands

You may want to enter the name of a file or something similar in Vi. To do that simply hit ESC :!command. I.e. ESC !ls will run ls in the  ls command.

Insert output of commands in current file.

To include the output of a command in Vi run ESC !!lcommand . So running ESC !!ls would insert the output of the command ls right in to file you are editing.

Drop to a shell

Instead of exiting a file to check something on the system simply running ESC:!bash will drop you out to a shell . Which is a handy way to run commands as root and not have them show up in the bash history. Try sudo vi foo , ESC : !bash , whoami and you will see you are root.

Cut and Paste // Yank and Put

In Vi you can delete lines using dd, if you want to cut 3 lines hit 3dd, then p will past those 3 lines. If you want to copy a line use yy, multiple lines simply use number YY, then p for put.  Feel free to post your own hacks and tips in the comments!


Automox for Easy IT Operations

Automox is the cloud-native IT operations platform for modern organizations. It makes it easy to keep every endpoint automatically configured, patched, and secured – anywhere in the world. With the push of a button, IT admins can fix critical vulnerabilities faster, slash cost and complexity, and win back hours in their day. 

Grab your free trial of Automox and join thousands of companies transforming IT operations into a strategic business driver.