Text object, macro and Visual mode done

This commit is contained in:
2025-08-23 19:49:04 +03:00
parent 3f067bd029
commit 81fcd18ee9
6 changed files with 1121 additions and 37 deletions

View File

@@ -557,4 +557,296 @@ Line mode:
:set nu!
-> Text Objects and Macros:
**** Text Objects ****
@ Command: {operator}{a}{object}
What does it do?
It perform operation on a word, from the starting character of the word, eventhough the
cursor maybe present in the middle of the same word.
It will also perform perform operation on the space after that word until a new or a character is
encountered.
For ex:
daw = Delete A Word
@ Command: {operator}{i}{object}
What does it do?
It perform operation on a word, from the starting charcter of the word, eventhough the cursor
maybe present in the middle of the same word.
However, unlike {a} operator, it will not perform operation on the space after the word to
be deleted.
For ex:
ciw = Change Inner Word.
@ Command: {operator}{as}
For ex:
das = Delete a Sentence
What does it do?
It will delete a sentence until the period, including the space after the period
until the next character is encountered.
@ Command: {operator}{is}
For ex:
dis: Delete inner Sentence
What does it do?
It wil delete a sentence until the period, but the space after the period.
Note:
Vim consider line-break as part of the sentence.
%% TIPS %%
To repeate these command we can use "." command.
@ Command: {operator}{ap}
For ex:
dap = Delete a paragraph
What does it do?
It will delete a paragraph until the boundary of the paragraph object,
including the blank line.
@ Command: {operator}{ip}
For ex:
dip = Delete a paragraph
What does it do?
It will delete a paragraph until the boundary of the paragraph object,
i.e. the blank line. However, it does not include the blank line.
@ Command: {operator}{a<symbol>} and {operator}{i<symbol>}
<symbol>
{ } or B, ( ) or b, < >, ' ', " ", [ ],` `
For ex:
di{ or di}
What does it do?
It will delete anything between { }. With "a" operator
will also delete the { }.
dab or da( or da)
What does it do?
It will delete anything between ( ) including the brackets.
%% TIPS %%
This command is benificial for programming languages.
at and it
t here means tags.
It will helpful for programming languages with opening
and closing tags like html and xml.
Even custom made tags are applicable in this case.
**** Macros ****
Macros are recorded to a register of vim. (named register)
%% To recored a macro %%
Use command: q<register-name>
It will start the recording.
Recording ends by using only the command "q".
%% To use the recorded macro %%
Use Command: @<register-name>
%% To use last recorded macro %%
Use command: @@
Macro Best Practices
--------------------
* Normalize the cursor position
-> 0
* Perform edits and operations.
* Position you cursor to enable easy replays.
-> j
%% TIPS %%
After recording macros to certain register, we can again append to
more macros to the register by using q+<Capital-register>.
For ex:
q+A instead of q+a.
This is true for appending anything to the register.
Applying Macros of particular register on the range of line:
------------------------------------------------------------
Use command:
:<start-line-number>,<end-line-number>normal @<register-name>
From all the line the file.
Use command:
:.,$normal @<register-name>
Saving Macros
---------------
* viminfo file
+ .viminfo ( for unix like system like LINUX or MAC )
+ _viminfo ( for windows like system )
* Stores history and non-empty registers.
* Read when vim starts.
* Can easily overwrite registers.
* vimrc files
+ .vimrc
+ _vimrc
We can define macros in register inside vimrc file.
let @<register-name> = '<Marcro>'
%% TIPS %%
To know the character of certain key-stroke, like Esc character.
We can know this by being in insert mode then type CTRL+v and the
Key for which we want to know the character.
-> Visual mode:
Use v to start characterwise visual mode.
Use V to start linewise visual mode.
Use Ctrl-v to start blockwise visual mode.
You can use motions to expand the visual area.
You can also use text objects to expand the visual area.
Just some of commands you can use in visual mode include:
~ - Switch case
c - Change
d - Delete
y - Yank
r - Replace
x - Delete
I - Insert
A - Append
J - Join
u - Make lowercase
U - Make uppercase
> - Shift right
< - Shift left
o - To move to the end of the highlighted part in the visual mode.
O - To move the opposite corner of the highlighted block diagonally.
gv - Will highlight everything which it got highlighted last time
and it will position the cursor in the last location where
it was present in visual mode and the mode will also the
same visual mode which was used last time.
%% TIPS %%
To add same character or set of character to multiple lines, use VISUAL-BLOCK
mode.
Go to VISUAL-BLOCK mode select the block, then use INSERT or APPEND, then
add the desired character or set of character and Esc. All the character will
appear on desired places.
Note: 'i' and 'a' command don't work in VISUAL mode.
To shift a line without using visual mode, we can use ">>" or "<<" command.
%% TIPS %%
To the Shift width and tabs space it applies, we can use:
Command:
:set shiftwidth?
:set tabstop?
To identify tab, use ":set list" and the symbol with ^I character represent
tab.
---------------------------
If shiftwidth = 8 and tabstop = 8 and expandtab is set to noexpandtab,
then shift operation is equivalent to tab.
expandtab expands with appropiate number of spaces instead of actual
tab character.
--------------------
In visual mode, if we select certain part and use line command to
execute line commmand like ":s/<old>/<new>/" substitution command
we can get the range of the highlighted text automatically in form
":'<,'>" and we can use it with any line command.
For ex:
":'<,'>center"
This will move highlighted text to the center.
":'<,'>center 40"
Here, 40 is the space count to move it to the center.
center -> center aligned. (ce)
left -> left aligned. (le)
right -> right aligned. (ri)
We can use short-hand version as well.