An Intro to Vim, the Unix Text Editor Every Hacker Should Be Familiar With « Null Byte :: WonderHowTo

As pentesters and hackers, we're going to be working with text frequently — wordlists, configuration files, etc. A lot of this we'll be doing on our machine, where we have access to whatever editor we prefer. The rest of it will be on remote machines, where the tools for editing will be limited. Ifnanois installed, we have an easy-to-use terminal text editor, but it isn't very powerful.Luckily, most systems will have eitherViorViminstalled. Vi stands forvisualand is a powerful, fast modal text editor that works in either insert mode (where you're typing inside the document) or normal mode (where you input commands for the session). Switching between these sessions is as easy as a keystroke, so it's great for those of you whodon't like taking your hands off your keyboard.Vim, which stands forVi IMproved, has all the features of Vi with some excellent additions that aid in editing source code. There's also a comprehensive help system and lots of customization options available. Many systems symlink Vi to Vim, includingmacOS. Personally, Vim is for all of my editing on remote hosts.Image via Null ByteVim may seem complicated and unintuitive, but don't worry — it feels that way for everyone when they're just starting. The trick is to keep at it. The longer you use Vim, the better you will get with it — to the point where you won't even need to take your hands off the keyboard.This guide will be very introductory. There are entire books written about Vi/Vim, but I want to make sure you know at least the basics so you can get up and running with it.Configuring VimOur first step is to configure Vim. Since we'll generally be working with code or configuration, we'll want line numbering and syntax highlighting on. Vim can be configured by editing the.vimrcfile in your home directory.Step 1: Open a TerminalOpen up a terminal emulator such asiTerm, and ensure you are at your home directory with the command:pwdThe terminal should show that you are in/Users/$yourusernameor something likeroot@kali. If it doesn't, enter the following command, which willchange directoriesto your home directory.cdStep 2: Edit the FileYour.vimrcfile is where your Vim configurations are stored. As you use Vim, you will start to have more custom configuration in your.vimrcfile. I keep a copy of my complex.vimrcon my GitHub, but in general, when you are using Vim on a remote host, unless you are an authorized user, you won't have a custom.vimrcso it's important to be familiar with basic behavior.In the terminal, enter the command:vim .vimrcThis tells Vim to open the file.vimrc. This file may not exist. In my case, I haven't configured.vimrcyet. As you can see below, it's an empty document, and the tilde (~) symbols simply indicate empty lines.Vim starts up in command mode (aka normal mode), not insert mode. This means that keys pressed will be interpreted as commands for Vim and not data entry. In order to get out of command mode, you will need to pression your keyboard. This enables insert mode. If you were to pressainstead, Vim would move the cursor one space to the left and begin insert mode there.In the image above, in the bottom left of the screen, we can see we are in insert mode. Now we can type in our configuration. You will want the following lines in the file:syntax on
set wrapmargin=8
set numberSyntax onenables built-in syntax highlighting for many programming languages and configuration files.Set wrapmargin=8gives us an 8-character buffer before the end of the terminal and makes the screen more legible.Set numbersimply turns on line numbering.On Amazon:Vi and Vim Editors Pocket Reference: Support for Every Text Editing TaskStep 3: Write Your Changes & QuitWe will now need to press theesckey in order to change Vim's mode back to command mode. The "INSERT" text at the bottom left of the screen should disappear when you are in command mode.In order to write (w) and quit (q), we simply enter the following command, including the colon (:) character.:wqWe now have a.vimrcfile, and the next time we edit something with Vim, we will see the changes. As you can see below, it looks a whole lot better.Step 4: Move Around in Command ModeWhen we're in command mode, we can move around the document quickly. The arrow keys will move around the document, as well ash,j,k,l— these keys work just like the arrow keys. If you've ever played Crawl, you will be very familiar with them.h       moves left
j       moves down
k       moves up
l       moves rightSome additional movement keys:e       moves you forward to the end of a word
w       moves you forward to the beginning of a word
b       moves you back to the beginning of a word
$       moves you to the end of a line
0       (zero) moves you the beginning of a line
G       moves you to the end of a file
gg      moves you to the start of a fileThere are, of course, many more ways to move around a file, but these should cover most use-cases.Step 5: Search a FileOften we will need to find strings in files, usually configuration, but it can also help with history files or anything else we maybe editing. For this, you will need to be in command mode. If you aren't sure what mode you are in, press the escape key, which will bring you back to command mode if you're not there already.For a simple search, we use/and then the string. For example,/password. If we were at the end of the file, we would use?, as in?password, to search backward. Thencommand will repeat the last forward search, and theNcommand will repeat the last backward search./string     searches forward (replace string with your query)
?string     searches backward from end of file (replace string with your query)
n           repeats the last forward search
N           repeats the last backward searchIn order to search and replace, we use the:%s/search/replace/syntax (you will need to enter the colon). For instance,:%s/tcpdump/ls/will search the entire file and replace every instance oftcpdumpwithls. And:%s/myPrivEscalationScript/ls/cwill search the entire file and replace each instance only if you confirm it.:%s/search/replace/     searches entire file for "search" phrase, replaces with "replace" phrase
:%s/search/replace/c    same as above, but requires confirmation to replaceVim also supports regular expressions in the same way thatgrepdoes.Don't Miss:An Introduction to Regular Expressions (Regex)On Amazon:Learning the Vi and Vim Editors: Text Processing at Maximum Speed and PowerStep 6: Save, Quit & Shell EscapeExiting Vim is always a problem for people just starting out. In order to exit, use these commands::w                  writes the file
:wq                 writes the file and quits
:q!                 exits the editor and discards all changes
:w someFileName     writes the changes to a file called "someFileName"In some cases, we might want to escape to a shell to browse directory trees or look at other files. In order to execute a system command in Vim, we use the command::!commandThis will execute whatever command we put after the bang. This can be a shell:!bash, which we can exit to return to Vim, or we could:!ls /etcto view the contents of the/etcdirectory.That Should Get You StartedThis article barely scratches the surface. As I stated in the introduction, Vim is a very powerful tool with entire books being dedicated to it. However, with these basic commands, you should be able to get around files, manipulate them, and exit the editor. I highly recommend picking up a copy of O'Reilly's guides below until you feel comfortable using it.Vi and Vim Editors Pocket Reference: Support for Every Text Editing TaskLearning the Vi and Vim Editors: Text Processing at Maximum Speed and PowerOnce you become skilled with Vim, you'll be flying through those text files like they were clouds.Don't Miss:A Guide to Choosing Your Optimal Text Editor or IDEFollow Null Byte onTwitter,Flipboard, andYouTubeSign up forNull Byte's weekly newsletterFollow WonderHowTo onFacebook,Twitter,Pinterest, andFlipboardWant to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseCover image and screenshots by Barrow/Null ByteRelatedHow To:VIM, as Ugly as It Is FastMac for Hackers:How to Set Up Homebrew to Install & Update Open-Source ToolsHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 22 (Samba)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 24 (The Linux Philosophy)How To:Slip a Backdoor into PHP Websites with WeevelyMac for Hackers:How to Get Your Mac Ready for HackingHow To:Create a medieval Knights Templar styled intro in After EffectsHow To:Use SecGen to Generate a Random Vulnerable MachineHow To:Use MinGW to Compile Windows Exploits on Kali LinuxHow To:Find Your Computer's Vulnerability Using LynisHow To:Punchabunch Just Made SSH Local Forwarding Stupid EasyNews:How to Study for the White Hat Hacker Associate Certification (CWA)How To:VIM, Using the Normal Mode Part 1How To:Use Zero-Width Characters to Hide Secret Messages in Text (& Even Reveal Leaks)How To:Use a Misconfigured SUID Bit to Escalate Privileges & Get RootHow To:Set Up an SSH Server with Tor to Hide It from Shodan & HackersHack Like a Pro:Finding Potential SUID/SGID Vulnerabilities on Linux & Unix SystemsHack Like a Pro:Metasploit for the Aspiring Hacker, Part 9 (How to Install New Modules)Hack Like a Pro:Perl Scripting for the Aspiring Hacker, Part 1Hacking macOS:How to Automate Screenshot Exfiltration from a Backdoored MacBookHow To:Attack a Vulnerable Practice Computer: A Guide from Scan to ShellCoding Basics:A Guide to Choosing Your Optimal Text Editor or IDEHow To:Mask Your IP Address and Remain Anonymous with OpenVPN for LinuxGoodnight Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingWeekend Homework:How to Become a Null Byte Contributor (2/24/2012)News:Lightweight Programs You Should Use for a Faster LinuxNews:Recut Movie Trailers - Horror VersionsCommunity Byte:HackThisSite Walkthrough, Part 2 - Legal Hacker TrainingNews:17th Annual Interactive Fiction Competitors AnnouncedHow To:Skip the Pre-Menu Credits in Deus Ex: Human RevolutionNews:INTRO TO SCREENWRITINGNews:Alex Responds to Obama’s Text Editor’s So Called ‘Mistake’News:Jim Winter 'FINGERS' Palmistry intro 1Community Byte:HackThisSite Walkthrough, Part 5 - Legal Hacker TrainingHow To:A Guide to Steganography, Part 1: How to Hide Secret Messages in ImagesHow To:Make a Gmail Notifier in Python