Jump to: navigation, search


1,519 bytes added, 5 years ago
Started explanation about visual mode
==Insert Mode==
The first thing you must grok is that Vim has several modes -- command mode, insert mode, visual mode, and last-line mode (also known as ex mode). When you start Vim, you'll be in command mode. Here, all of the keys are used to perform commands, not input text. To switch to input mode, hit <span style="color:green">i</span>, and you're able start editing the file, adding text, using Backspace, etc.
To return to command mode, hit <span style="color:green">Esc</span>. To enter last-line or Ex mode, use :, and then input the command you wish to enter.
You can move faster by using <span style="color:green">b</span> and <span style="color:green">w</span> to move backward and forward by one "word" at a time, respectively. Vim looks at "words" as a string of alphanumeric characters. So "eix" is a word, but "eix-sync" is multi-word because it's broken up by a non-alphanumeric character.
You can also move from one "paragraph" to the other thanks to {{c|<nowiki>{</nowiki>}} and {{c|<nowiki>}</nowiki>}} that respectively move the cursor to the previous and next blank line.
Deleting is done with <span style="color:green">d</span> or <span style="color:green">x</span>. To delete a single character, move the cursor over that character and use <span style="color:green">x</span>. Using <span style="color:green">dw</span> will delete the word the cursor is over, and <span style="color:green">db</span> will delete the previous word.
To delete an entire line, use <span style="color:green">dd</span>. To delete from the cursor to the end of the line, use <span style="color:green">d$</span>. To delete from the cursor to the beginning of the line, use <span style="color:green">d0</span>. The $ is shorthand for "end of the line," and 0 is shorthand for beginning of the line. You can also use ^ (shorthand for "first non-blank character of the line").
==Copying and Pasting in Vim==Let's look at copying and pasting real quick. To highlight text to copya line, use {{c|yy}} (yank). To copy from the cursor the the end of the <span style="color:green">v</span>line, <span style="color:green">V</span> and <span style="color:green">Ctrl-V</span> commandsuse {{c|y$}}. You might have guessed by now that Vim commands are case-sensitiveTo copy 3 lines, So v and V are different thingsuse {{c|3yy}}, ...
The <span style="color:green">v</span> command simply allows you Yanked lines are stored in a buffer, to paste the content of the buffer after the cursor, use {{c|p}}, to highlight changes character by character using paste it before the movement (hljk cursor, use {{c|P}}. Note that {{c|d}} and others) or arrow keys. The <span style="color:green">v</span> command highlights entire lines. And {{c|x}} also copy the deleted content to the <span style="color:green">Ctrl-v</span> command highlights blocks of text -- very useful for highlighting and copying columns of textbuffer.
Once you've highlighted the text you want to copy, hit <span style="color:green">y</span> to "yank" the text into the buffer.
To paste the text, use <span style="color:green">p</span> or <span style="color:green">P</span> to paste. The p command will paste after the cursor, and P pastes before the cursor.
==Search and Replace==
To search through the document, use the / key to initiate a forward search, or ? to initiate a backward search.
To search and replace, use <span style="color:green">:s</span> (substitute) with the range of lines and search terms. Like so:
A usual search would look something like :%s/old/new/gc, and when you hit Enter you'll be prompted before you make changes. I recommend using confirm; otherwise you can wind up with unexpected results.
If the "search" part of the substitute command is omitted, the last searched term will be used. The following exemple will search for the string "hello" and then substitute all "hello" occurrences by "salut".
If <leader> is set to the default backslash '\' the combination of keys '\px' pastes the text from the X clipboard(register +), setting paste mode temporarily, this requires a vim built with the "X" USE flag.
== Visual mode ==
I noticed that some of my friends that use Vim as their main editor, do not use the visual mode at all. However, it is of a great power and will save you a lot of time. Basically, it allows you to select some text (as you would do with your pointer in any graphic base editor) and process almost any command available from the command mode on the selected part.
To switch to the visual mode, type {{c|v}} ({{c|<nowiki><Esc></nowiki>}} to go back to the command mode). Then you can move just as in the command mode ({{c|h}}, {{c|j}}, {{c|k}}, {{c|l}}, {{c|w}}, {{c|$}}, {{c|<nowiki>}</nowiki>}}, etc). If you want to delete the selection, type {{c|d}}. If you want to copy the selection, type {{c|y}}.
If you type {{c|:}}, you will notice that it automatically appends {{c|'<,'>}} in the command-line. This means that the following command will be applied in the range of the selection. If you append the following command {{c|s/foo/bar/g}}, all occurences of "foo", will be replaced by "bar" within the selection.
When a command is processed, you go back to the command mode. Typing {{c|1v}} will restore the previous selection, starting from the current position of the cursor. The former is often useful when you work in "visual line" mode. To enter "visual line" mode, type {{c|V}} (upper V). This mode allows you to select lines instead of characters. This is especially useful when you want to delete a bunch of lines, large enough to take some time to count them all before typing {{c|17dd}} for instance.
Eventually, there is also a "visual block" mode. This awesome mode, that you would probably not find in common non-text-based editors, allows you to select columns of text, instead of lines.

Navigation menu