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.
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
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.
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!
Facing growing threats and a rapidly expanding attack surface, understaffed and alert-fatigued organizations need more efficient ways to eliminate their exposure to vulnerabilities. Automox is a modern cyber hygiene platform that closes the aperture of attack by more than 80% with just half the effort of traditional solutions.
Cloud-native and globally available, Automox enforces OS & third-party patch management, security configurations, and custom scripting across Windows, Mac, and Linux from a single intuitive console. IT and SecOps can quickly gain control and share visibility of on-prem, remote and virtual endpoints without the need to deploy costly infrastructure.
Experience modern, cloud-native patch management today with a 15-day free trial of Automox and start recapturing more than half the time you're currently spending on managing your attack surface. Automox dramatically reduces corporate risk while raising operational efficiency to deliver best-in-class security outcomes, faster and with fewer resources.