Using Vim, the utlimate text editor

A text editor is where a script coder or hacker spends most of his time. Choosing one that we master and feel comfortable with is therefore a crucial factor affecting productivity and speed. A common mistake is made by new hackers: they find a text editor with a GUI and jump towards it because it's simple and similar to standard editors. This is a bad practice because they'll find only later that the capabilities of such editors are limited. It is recommended to start with a more advanced text editor since day one, even if this requires more effort, time and dedication to master. Today we'll talk about Vim, short for Vi Improved, a text editor that has earned the reputation of the most powerful editor out there.

Vim, the friendly beast that scared newbies

The very first thing we notice about Vim is that it doesn't have a GUI. Instead, it runs entirely in a terminal window. Add to this the lack of functionality for the mouse -- all interaction is done through the keyboard -- and you have the perfect recipe for a program that scares newbies. Hell, Vim is so unconventional that new users can't even find out how to close the program!

However, if you dedicate a small amount of time to learning how to use Vim, you will quickly realize that it's actually a good choice. Let's start with some basics.

Command mode/Insert mode

When you open a file with Vim, you view it in Command mode. The command mode does not allow you to edit the text but rather to move through the file or search and replace words. Below are the available actions in command mode:

  • h: move left
  • l: move right
  • j: move down
  • k: move up
  • e: go forward to end of current word
  • w: go forward to beginning of next word
  • b: go backwards to end of previous word
  • $: go to end of line
  • 0: go to beginning of line
  • G: go to end of file
  • gg: go to beginning of file


To use these commands, simply type the corresponding letter on your keyboard.

Let's now clarify how to open and close a file:


  • Open a file: in a terminal, vim filename
  • Write: :w
  • Write to "filename": :w filename
  • Write and quit: :wq
  • Discard and exit: :q!


Note that these should be used while in command mode.

To edit a file and start writing, simply press i while in Vim to enter insert mode and begin editing. To return to command mode, press Esc.

Configuring Vim

Every user can configure Vim to his own likings, thanks to a config file that we can edit. Let's enhance our Vim experience with the following options that I'll explain below:
    1) Open .vimrc: in a terminal, vim .vimrc
    2) Enter insert mode
    3) Input the following lines:
        syntax on
        set wrapmargin=8
        set number
    4) Back to command mode
    5) Write the file and exit


With these 3 lines of code, we have configured 3 different options:
syntax on tells Vim to emphasize code syntax by using color codes for different commands
set wrapmargin=8 prevents lines of code from getting through the window
set number tells Vim to display line numbers

More advanced commands

As mentioned earlier, Vim is very effective at searching for specific strings and/or replacing them. We can perform several types of searches using the following commands:

  • /string whatever: searches forward for the string "whatever"
  • ?string whatever: searches backwards for "whatever"
  • n: repeats the search forward
  • N: repeats it backwards
  • :%s/search/replace/whatever: finds and replaces "whatever"
  • :%s/search/replace/c/whatever: same as above but asks for confirmation before each replacement


In general, we have gone through all key features of Vim. Using it on a daily basis will end up saving you valuable time thanks to its efficiency. For any questions or remarks, leave a comment below and I will be glad to help. Stay tuned for the next post.

Comments

Popular posts from this blog

Pixie-Dust Wifi Attack: Theory & Practice

Getting started in scripting

How to capture a WPA handshake