قالب وردپرس درنا توس
Home / Tips and Tricks / An intro for Vim, the Unix text editor that every hacker should be familiar with «Null Byte :: WonderHowTo»

An intro for Vim, the Unix text editor that every hacker should be familiar with «Null Byte :: WonderHowTo»



As a pentester and hacker we will often work with text – word lists, configuration files, etc. We will do a lot of that on our computer, where we have access to the editor we prefer. The rest will be on remote machines where the editing tools are restricted. When Nano is installed, we have an easy-to-use terminal text editor that is not very powerful.

Luckily, most systems have either Vi or Vim installed. Vi stands for visual and is a powerful, fast-modal text editor that works either in insert mode (where you type in the document) or in normal mode (where you type commands for the session). Switching between sessions is as easy as pressing a button, making it ideal for those who do not like to take their hands off the keyboard.

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

Zero Bytes Picture

Vim may seem complicated and uninteresting, but don't worry – it feels like it all when they are just starting. The trick is to stick to it. The longer you use Vim, the better you get it – to the point where you do not even have to take your hands off the keyboard.

This tutorial will be very introductory. There are whole books on Vi / Vim, but I want to make sure that you at least know the basics to get you started.

Configuring Vim

Our first step is configuring Vim. Since we generally work with code or configuration, we want to emphasize row numbering and syntax. You can configure Vim by editing the file .vimrc 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 display that They are located in / Users / $ yourusername or roughly like root @ kali . If this is not the case, enter the following command, which will change the directory to your home directory.

  cd 

Step 2: Edit the file

Your .vimrc file contains your Vim configurations are saved. If you use Vim, you have another custom configuration in your .vimrc file. I keep a copy of my complex .vimrc on my GitHub. However, if you use Vim on a remote host, you generally do not have a custom user . .vimrc Therefore, it is important to be familiar with the basic behavior.

Enter the command in the terminal:

  vim .vimrc 

This will prompt Vim to open the file .vimrc . , This file may not exist. In my case, I have not configured .vimrc yet. As you can see below, this is a blank document, and the tilde (~) symbols just show blank lines.

Vim starts in command mode (aka normal mode), not insert mode. This means that pressed keys are interpreted as commands for Vim rather than data entry. To exit command mode, press i on your keyboard. This allows the insert mode. If you press and instead, Vim moves the cursor one field to the left and starts insert mode.

Step 3: Write your changes and exit

We must now press the esc key to return Vim's mode to command mode. The "INSERT" text at the bottom left of the screen should disappear when in command mode.

To write ( w ) and finish ( q ), we simply enter the following command, including the colon (: ) ,

: wq 

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

Step 4: Move in command mode

While we're at it In Command mode, we can move around the document quickly. The arrow keys will move around the document, as well as h j k l – – these keys work just like the arrow keys. If you have ever played crawls, you will be familiar with them.

  h goes to the left
j moves down
k moves up
l moves to the right 

Some additional movement keys:

  e brings you to the end of a word
w brings you to the beginning of the word
b brings you back to the beginning of a word
$ takes you to the end of a line
0 (zero) shifts the beginning of the line
G brings you to the end of a file
gg takes you to the beginning of a file 

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

Step 5: Finding a File

Common You need to find strings in files, usually in the configuration, but it can also be useful for history files or other items we may be working on. You must be in command mode for this. If you are not sure which mode you are in, press the Escape key. If you are not already there, return to command mode.

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 – password as in – to search backwards. The command n repeats the last forward search, and the command N repeats the last reverse search.

  / string searches forward (replace the string with your query).
String searches backwards from the end of the file (replace the string with your query)
n repeats the last forward search
N repeats the last backward 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 instance of tcpdump with ls . And :% s / myPrivEscalationScript / ls / c searches the entire file and replaces each instance only if you confirm it.

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

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

At Amazon: Learning The Editors for Vi and Vim: Word Processing at Maximum Speed ​​and Performance

Step 6: Saving, Quit, and Shell Escape

Quitting Vim is always a problem for people just starting. Use the following commands to exit:

: w writes the file
: wq writes the file and is terminated
: q! Ends the editor and discards all changes
: w someFileName writes the changes to a file named "someFileName" 

In some cases, we may want to go to a shell to browse directory structures or browse other files. To execute a system command in Vim, we use the command:

 :! Command 

This executes the command we put after the bang. This can be a shell :! Bash which we can leave to return to Vim, or we could :! Ls / etc Display the content of / etc. directory.

That should get you started

This item barely scratches the surface. As I mentioned in the introduction, Vim is a very powerful tool to which entire books are dedicated. However, with these basic commands, you should be able to bypass files, edit them, and exit the editor. I strongly recommend that you consult a copy of O & # 39; Reilly's Guides below until you feel comfortable.

If you are familiar with Vim, you will fly through these text files as if they were clouds.

Cover images and screenshots of Barrow / Null Byte

Source link