Wednesday, September 17, 2008

Linux - Vim. Basics

At times there is a need for a powerful text editor (and sometimes exclusively for the console). Under Linux, there are two very popular and expanding applications: Vim and Emacs. They both support the myriad functions, which, with the possibility of adding what is missing (if you need more than just a set text). This article will cover the first couple of the editors. Worth mentioning that both have huge army of fans and frequently erupted holivary about the best of them.

Vim means Vi Improved, improved vi. Vim developed from 1988 - the first year, so it is understandable why this is such a monster (though Emacs will pomonstruoznee). In addition, there are versions for various operating systems - such as Linux, Win16, Win32, DOS, various Unix, BeOS and many others. In addition, Vim uses many users quite unusual approach to editing the text: there are two modes - command (as is clear from the title, then used different commands) and Paste (directly enter text). So the first step is to at least a little bit (a lot better) deal with the principle of work and remember, as is being done. Do not hope that the article could even disclose any significant portion of opportunities vim (he is too powerful), but the basis for further study will be laid, and if you enjoy editor, it would be easier to absorb various wisdom.

Important note. When editing upper and lower case letters have different meanings (although sometimes close).

To begin with, let's find somewhere for editing. The demonstration will take place on the text of GPL v3 (GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007). But if you have any other text, it is not scared.

$ Cp / usr/share/common-licenses/GPL-3 ~ / temp /

$ Vim ~ / temp/GPL-3

Now we have opened vim to the file content (by the way, what is displayed on the screen, called a buffer), but not hurry to press anything, because all perceived as a pressing team, the shift in the mode inserts performed by pressing keys i. Once you enter that you need, press Esc to return to the team (or core, it is normal) mode. Now you can customize the text. To move can be used as arrows, and h (left), j (down), k (top), l (right). The main advantage hjkl that its use does not need to carry hand on the arrows, as a result of this (with proper practice) speed increases. To go to the top line, use 0 (zero), and by the end - $. According to Enter should jump to a new line to the first character, which is not a gap. A movement to the next floor head w. (And) move the cursor to the beginning and end of sentence, respectively. To search applies / go that (so many console). To find the next suitable element, click n, and to search in the opposite direction -? That go.


/ License - cursor at the first appearance of the word License

n - the second

? License - again on the first.

A particularly rich opportunities provides the so-called repetition rate, which recruited before calling the team. For example, 8/License moves the cursor to 8 - e hit License words in the text, and 15) moves the cursor to the end of 15 - the first sentence, starting from this. This is available for many commands and can very much easier life.

To delete button is used d (short for Delete). For example, dl 1 removes the character to the right (the same right d), dw removes the current word, d Enter removes the line and following it, d $ removes the text before the end of the line, and dd - an entire line. d / the text removes provisions of the cursor to the first meeting article the. If the combine, you can get something like 4d5w, that removes 4 times in five words (that is 20 words, starting with the current).

Copying text applies similarly disposal. In order for something to copy, use y. Box execute commands p and P. A separate function "cut" with Vim not. Its function is removal. Each time you delete text, it is placed in the clipboard (more precisely, into the buffer by default as they may be large quantities). For instance, to 6 lines in the buffer, you need to perform "a6yy, and thence to insert -" ap. During all these operations data in the ring buffer remain unchanged. Once you've produced, you can undo your changes by clicking u.

Above all, in Vim is the so-called command-line mode. To press its use:. It is precisely disclosed some features of the editor. Unlike the keys in command mode, there are no restrictions on the number of teams. For example,: syn includes a backlight syntax that conveniently when editing configuration files or source codes. Above all, there are very powerful and extensive help system, which can cause a command: help section.

To replace text, the command: s


: 1,35 s / GPL / AAAAAAA / - replaces text in the file "GPL" text "AAAAAAA" in rows with the first of 35 - yu. A: 1, $ s / License / SSSSSS / replaces throughout the License at SSSSSS.

Here is a list of commands:

: shell - a shell of the launch command Vim;

: w - writes the contents of the buffer to a file without breaking the vi;

: w! -- Writes the contents of the buffer to a file without breaking the vi (for Read Only files);

: w abc - writes the contents of the buffer to file abc (if not it will be established, otherwise - overwritten), without leaving vi;

: 1,10 w abc - writes lines 1 - and on 10 - to file yu abc;

: 'a, $ w abc - writes the lines of the line marked "a", until the last line abc;

: e abc - download the file for editing abc instead of the current file. Returns mistake, if changes in this file have not been saved;

: e! abc - download the file for editing abc, not paying attention to not saved changes in the current file;

: e # - download the file for editing previous (command: e # next);

: f abc - changes the name of the current buffer vi on abc;

: q - leaves the editor, if no changes are not saved;

: q! -- Goes and destroys with all the changes are not saved;

: r abc - abc file reads in the current buffer vi and puts his cursor;

:! cmd - performs command (who, sort, ls, etc.).

In addition, there are teams for direct settings editor. They are established with the help: set

: set option - activates option;

: set option = value - appoints value for the option;

: set no option - deactivates option;

: set - shows the options set by the user;

: set all - shows all options - as set by the user, and the option "by default";

: set option? -- Shows the importance of options.

For example,

: set number - includes numbered lines;

: set nonumber - numbering off lines.

This, of course, well, but the settings are working only from the current session, and the editor had to restart when they enter again. To solve this problem, you can create a file with user preferences.

$ Vim ~ /. Vimrc

Now you can in practice and test their knowledge. To Vim correctly read it, you can just write without a team at the beginning of the colon. For example, something like:

set nowrap

set autoindent

syn on

colorscheme evening

set number

set spell

Save and exit. Thus, the setting (of course, we recorded an extremely primitive configuration) will be loaded each time before starting.

Of course, vim supports much more (taboos, macros, autocompletion in programming, etc.), but this material enough for the whole book, not a small article. Also on the Internet mass of resources devoted to this remarkable editor. Learn!

At the symposium otklanivaetsya your humble servant. Prior to future meetings!

1) "Get Money for Clicks" - Fastest Growing Domain Parking Company in the World.
2) Search your domain name wishing to have! FREE DOMAINS -

No comments:

Post a Comment