قالب وردپرس درنا توس
Home / Tips and Tricks / An Intro to Vim, Unix Text Editor Every Hacker should be familiar with «Zero Byte :: WonderHowTo

An Intro to Vim, Unix Text Editor Every Hacker should be familiar with «Zero Byte :: WonderHowTo



As pentesters and hackers we will work with text often – glossaries, configuration files etc. Many of this we will do on our machine, where we have access to which editor we prefer. The rest of it will be on remote machines, where the tools for editing will be limited. If nano is installed, we have an easy to use terminal text editor, but it is not that powerful.

Fortunately, most systems have either Vi or Vim installed. We stand for visual and are a powerful, fast modal text editor that works in either insert mode (where you type inside the document) or normal mode (where you enter commands for the session). Switching between these sessions is as easy as a keystroke, so it's good for those who don't like to take their hands off the keyboard.

Vim, which stands for We IMproved has all the features of We with some excellent additions that help with editing source code. There is also an extensive help system and many customization options are available. Many systems symbolize Vi to Vim, including macOS. Personally Vim is for all my editing on remote hosts.

Image via Null Byte

Vim can seem complicated and unintuitive, but don't worry – it feels so for everyone when they just start. The trick is to keep it. The longer you use Vim, the better you will bring it – to the point where you don't even have to take your hands off the keyboard.

This guide will be very introductory. There are whole books about Vi / Vim, but I want to make sure you know at least the basics so that you can get started with it.

Configuring Vim

Our first step is to configure Vim. Since we will generally work with code or configuration, we want to listen to numbering and syntax on. Vim can be configured by editing the .vimrc file in your home directory.

Step 1: Open a terminal

Open a terminal emulator like iTerm and make sure you are at your home directory with the command:

  pwd 

The terminal should show that you are in / User / $ your username or something like root @ kali . If it does not, enter the following command, which will change directories to your home directory.

  cd 

Step 2: Edit the file

Your .vimrc file is where your Vim configurations are stored. When using Vim, you begin to have more custom configuration in the file .vimrc . I keep a copy of my complex .vimrc on my GitHub, but generally, when you use Vim on a remote host, if you are not an authorized user, you do not have a custom Vimrc so it is important to be familiar with basic behavior.

In the terminal, enter the command:

  vim. vimrc 

This tells Vim to open the file .vimrc . This file may not be available. In my case I have not configured .vimrc yet. As you can see below, there is a blank document and the symbols tilde (~) simply indicate empty lines.

Vim starts in command mode (aka normal mode), not insertion mode. This means that buttons that are pressed will be interpreted as commands for Vim and not data entry. To get out of command mode, press i on the keyboard. This enables the deposit mode. If you were to press a instead, Vim would move the cursor a space to the left and begin to insert the position there.

Step 3: Write Your Changes and Exit

We now need to press the esc key to change Vim's location back to command mode. The text "INSERT" at the bottom left of the screen should disappear when you are in command mode.

To write ( w ) and quit ( q ), we just enter the following command, including the colon sign (: ).

: wq 

We now have a file .vimrc and the next time we edit something with Vim, we will see the changes. As you can see below, it looks much better.

Step 4: Move around in Command Mode

When we are in command mode, we can quickly move around the document. The arrow keys move around the document, as well as h k l – these keys work just like arrow keys. If you've ever played Crawl, you'll be very familiar with them.

  h moves to the left
j moves down
k goes up
I'm moving right 

Some additional motion buttons:

  e move you to the end of a word
w moves you 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 the beginning of a row
G moves you to the end of a file
gg moves you to the beginning of a file 

There are, of course, many more ways to move around a file, but they should cover most usage cases.

Step 5: Find a file

Often we will need to find strings in files, usually configuration, but it can also help with history files or anything else we might edit. For this you must be in command mode. If you are not sure which mode you are in, press the Escape key, which will take you back to command mode if you are not already there.

For a simple search we use / and then the string. For example, / password . If we were at the end of the file, would we use ? as in ? Password to search backwards. The command will repeat the last forward search, and the command N will repeat the last reverse search.

  / string searches ahead (replace strictly with your question)
? the string searches back from the end of the file (replace the string with your query)
n repeats the last past search
N repeats last back search 

To search and replace we use the syntax :% s / search / replace / (you must enter the colon). For example, :% s / tcpdump / ls / searches the entire file and replaces each occurrence of tcpdump with ls . And :% s / myPrivEscalationScript / ls / c will search the entire file and replace only each instance if you confirm it.

:% s / search / replace / search the entire file for "search" phrase, replace with "replace" phrase
:% s / search / replace / c same as above, but requires confirmation to replace 

Vim also supports regular expressions in the same way as grep does.

At Amazon: Learning We and the Vim Editors: Maximum Speed ​​and Power Word Processing

Step 6: Save, Exit and Shell Escape

Final Vim is always a problem for people just starting out. To exit, use the following commands:

: w writes the file
: wq writes the file and ends
: Q! leaves the editor and discards all changes
: w someFileName writes the changes to a file called "someFileName" 

In some cases, we may want to fly to a shell to browse the directory or look at other files. To perform a system command in Vim, we use the command:

 :! Command 

This will execute the command that we post the tape. This can be a shell : bash that we can leave to return to Vim, or we could :! Ls / etc to see the contents of / etc .

It should get you started

This article carefully repairs the surface. As I mentioned in the introduction, Vim is a very powerful tool with whole books dedicated to it. But with these basic commands you should be able to get around files, manipulate them and leave the editor. I strongly recommend that you download a copy of Reilly's guides below until you feel comfortable using it.

When you become skilled with Vim, you will fly through the text files that they were clouds.

Cover image and screenshots of Barrow / Null Byte

Source link