📜 ⬆️ ⬇️

effective use of vim: "incredibly tips, part I"

Good all the time of day
trying to keep my promise, I want to publish the trail. vim editor article
The article implies that the reader is already familiar with the basics of working in vim'e, it will be devoting to a more efficient use of accumulated knowledge.


tips 1:

I forgot which file I was editing ...


view file name - CTRL-G
"Php.ini" 1255 lines --1% -
Detailed Information - gCTRL-G
Col 1 of 71; Line 18 of 1255; Word 79 of 6980; Byte 563 of 44684

')
tips 2:

You do not use gi ?????????


gi is very useful
You write in the 143rd line of the php.ini file, exit insert mode, look at another file / buffer, press gi and continue to edit the 143rd line of php.ini.


tips 3:

Girl, we repeat!



spice 1, single repeat


CTRL-E, TRL-Y in the command mode, shift the screen by 1 line well below / above
in insert mode inserts characters from the bottom \ top line


example:
has the string:
$ i-> love () -> mylove () -> because-> sheis-> VerySuccessfully ();
need to print
$ i-> love () -> mylove () -> because-> sheis-> VerySexy ();
all that is highlighted in black is printed using CTRL-Y ,
they really printed only VerySexy ();

spice 2, repeat last entry


RTL-A - inserts the last entered sentence (in insert mode!)

example:
has the string:
$ i-> love () -> mylove () -> because-> sheis-> VerySilly ();
we want to build it here:
RTL-A (and everything, you do not need to switch to another mode, copy it and etc.)
$ i-> love () -> mylove () -> because-> sheis-> VerySilly ();

Spice 3, why assume why suffer when there is CTRL-R =
insert mode:
CRTL + R = 5 * 5 + 25 - will insert 50;

Go

spice 4


: read! date - inserts the current date
: read! cat file - paste the contents of the file
: grep -iR "TODO" * + copen - will all the TODO in the project
: read! cat / dev / random - will give out the password of the administrator \ current user (ONLY FOR true unix users !?) most importantly, do not forget CTRL + C to press


spice 5, paragraphs


Go
CRTL-T, CRTL-D insert paragraphs in edit mode


Search is natural


* / # - search for the next / previous word position
[I,] I - shows all the lines that contain the word under the cursor
hmm ...
for more convenient use let's do:
: nmap [I: let nr = input ("Which one:„) exe “normal”. nr. "[\ t"

So, my mouth has already closed, and faith in reality seems to have returned, although spiritual values ​​and faith in other editors seem not to resume ...
I will continue…

Search in vim is natural



repetition is the mother of learning


/ - Looking for trail entry ? - searches for pre n occurrence - repeat last search
N - repeat the last search in the opposite direction


so:

I would like to touch on the topic of regular expressions (bl *, pi **** =)


Unfortunately, regular language is a topic for a whole book.
here I want to show examples that I use more often than others:
d / ^ # - delete everything from the current line to the first comment
10d / ^ # - appropriately until the 10th

y / ^ class /; function will allocate everything from the current position to the first occurrence of function in the class class.
:% s / foo / bar - replaces foo with bar in the entire file (equivalent: 1, $ s / foo / bar /)
:., / <\ / body> / s,
, gc - rules the tag
from current line to tag
Asking for confirmation without spaces, this is a nice effect
(c - cautios, caution)
The author was so drunk ... that he left the reader to comment on the trail. expression:
: -23, 'ts / wow / WOW /
?
& - repeat last replacement in current line
: && - repeat the last replacement in the current line with the same flags
g & - repeat the replacement for the entire file


shift + left left left hand notepad'ovts move
aw, iw, as, ap, a {- the soul of a vim sings



go to the viral mode:
iw, iW - highlight a word
aw, aW - highlight the word, whitespace is not taken into account
as, is - select a sequence of words (as, is - there is a difference, see - iw, aw)
ap, ip - highlight paragraph
a {, i { - select the entire block {..} or the text inside it
a (, i ( - select the entire block (..) or the text inside it
a <, i < - select the whole block (..) or the text inside it
a ', i' - select a single-row drain or text inside it
a ", i" - select a two-line point or text inside it
(vim knows that \ "is not necessary to take into account)
at, it - select the whole tag or the text inside it (hello XML & HTML)

examples:
das - delete the sequence, including whitespace after it.
ci ( - replace text inside (...)
yat - copy the entire tag inside which the cursor is located
gU ' - sql requests we write in upper case! (changes the case in the single-row string to the upper one)
vip - select the entire paragraph without whitespace


smart insert


: set autoindent - Formats a paragraph when inserting a new line.
] p,] P - insert before \ after and format the indents
not always convenient (
: nnoremap P P '[v'] =
: nnoremap P P '[v'] =
before] - I do not reach, therefore I use

work with registers


It was partially described in the previous article - habrahabr.ru/blog/vim/45414.html
I would like to add:
"0 - the last copied text that was not entered in the register
"1 - the last deleted text that was not entered in the register
(" - and was longer than one line)
...
"2 -" 9 deleted text for the 2.9 team
". - last inserted text
"% - the name of the current file
"_ - black box registrar (its use does not affect other registers)
: reg - displays the value of all current registers


macro recording


q start recording a macro; sequential q stops recording
@ executes a macro
@@ executes the last macro executed

I think here you can give a small example
used to write in code
global $ db;
Now the style has changed a bit and I began to write
$ db = Database :: get ();
This code is quite a lot, although it is quite small

What are the necessary actions for me to replace?
1. find global $ db;
2. replace global $ db;
3.in $ db = Database :: get ();

now through vim;
1st point -: grep -iR "global $ db;" *
2.3 I will try to do as a macro:
so, after the 1st point, command mode, press qa, d2W, i
enter $ db = Database :: get ();
q.
recorded macro
: copen
looking for trace entry global $ db;
now just hit @a
go to the next entry

ps forgive me to note that this is something like hello world for macros, those are just the simplest example.

Thourht time in the past


probably another of the possibilities of vim'a which amazes me =)
g- / g + - slide between older / new text
: earler Ns, m, h - return the contents of the file, which was s, m, h "time" later
: later - on the contrary, respectively, return the contents of the file, which was s, m, h “time” after
: ealer 60m - damn, well, I sold the wine today =)

Thank you all for your attention ...
ps have any questions? write, collective intelligence will help you!

Source: https://habr.com/ru/post/28155/


All Articles