Main  --> Computer Science  --> Editors  --> Vim

Vim

Some history

First there was ed. ed was originally written by Ken Thompson in the early 1970s.
ed is a line-based editor and part of the Portable Operating System Interface (POSIX).

To make ed more user friendly ex was created in the 1970s. ex is also line-based and like ed part of POSIX.

The vi was created on the base of ex. It got a visual mode and became a screen-oriented text editor. vi is part of POSIX, too.

Beginning at the end of the 1980s and in the early 1990s Bram Moolenaar improved the vi and Vim (Vi iMproved) was born.

How to start

Unlike vi vim isn't part of POSIX. That's why you might have to download and install Vim before you can use it.
You can start Vim simply by typing: vim <file>

If you want to pass some options use: vim [options] <file>

There are many different options like '-R' to open the file readonly or '--clean' to start vim without any personal configurations, plugins, ...
For more details refer to the man page: man 1 vim

To open multiple files in the same window horizontally splitted use vim -o file1 file2 file2. In order to open the files vertically splitted use vim -O file1 file2.

How to end

If you started vim you'll ask how to terminate it. First go into the normal mode by pressing ESC and change with : into the command mode. To quit without saving enter :q!. If you want to safe and quit enter :wq or :x.

How to edit a text

Like the vi vim has different modes: normal mode, insert mode, command mode, visual mode and some more.

When you started vim it will be in the normal mode (unless configured differently). To insert text press i (insert) to enter the insert mode. You can now start writing your text, source code or your shopping list. If you want to leave the insert mode press ESC.

Do you think this feels strange? Think of a painter who is watching its canvas. This is the normal mode. When the painter puts her brush on the canvas it enters the insert mode. As soon as the brush is removed from the canvas she enters the normal mode again.

i let you insert text at your cursor position. If you want to insert text behind the cursor position press a (append) when you're in the normal mode. If you like to insert text at the beginning of the current line press the uppercase I and to append text at the end of the line press the uppercase A.

Writing text is nice, but what to do if you want to delete some characters, words, lines? To delete the character under the cursor press x to delete the character before the cursor press the uppercase X. If you want to delete a whole word press dw (delete word) and for a whole line dd.

You can also delete multiple characters, words and lines by using numbers. 4x will delete the next 4 characters, d5w will delete the next 5 words and d3d will delete the next 3 lines. If you want to undo changes just press u. To redo your changes which were undone with u press Ctrl-r. A very important command is the .. It does exactly the same action again. If you did d2w and you press . it will delete another 2 words. Increase and decrease numbers If you want to increase a number by one put your cursor in the line with the number. Go somewhere in front of the number and press Ctrl-a To decrease the number press Ctrl-x For increasing or decreasing by more than one put the number in front: 4Ctrl-a will increase the number by 4. If you have a list of numbers you can mark them with Ctrl-v and then increase or decrease them with Ctrl-a or Ctrl-x

Using vim as hex editor1

From time to time I need to view binary files in hexadecimal representation in vim. The simplest way I know, is to use the external command xxd in vim:

:%!xxd

If you changed the hexadecimal part you can turn back to the pure text representation by:

:%!xxd -r

Inserting result of an external command

Inserting the output of an shell command like find subdir/ -type f -name "*something*" | grep 'pattern' | sort -r | uniq in the middle of a file is pretty easy with vim:

:r!find subdir/ -type f -name "*something*" | grep 'pattern' | sort -r | uniq

1

https://vi.stackexchange.com/questions/343/how-to-edit-binary-files-with-vim