In vim, when you want to delete an inner word, you can press the following in the normal mode:
diw
but if you want to delete an outer word, you should press:
daw
now the question is, i represents inner, but why in the outer situation, it is not dow but daw?
From :help text-objects:
The commands that start with "a" select "a"n object
including white space, the commands starting with "i" select an "inner" object
without white space, or just the white space. Thus the "inner" commands
always select less text than the "a" commands.
Note that the boundaries of a text object are an integral part of the text object. If "inner" is what is inside those boundaries, then "outer" would be what is outside of them, thus everything else in the buffer.
From that point of view, selecting what is inside the boundaries and the boundaries themselves is really selecting "a" whole text objet.
Foo <span>bar</span> baz.
-- actual meaning --
<----> <-----> the boundaries of the text object
<-> what is inside those boundaries
-> "inner text object"
-> "it"
<--------------> the boundaries + what is inside them
-> "a text object"
-> "at"
-- hypothetical meaning --
<--> <---> what is outside of the boundaries
-> "outer text object"
-> "ot"
Related
I'm new at vim, started to play with macroses, and founded not obvious override behaviour for jump/change lists.
The main question - are there any stable ways to jump by changes/moves inside same line (except marking each point), cause this lists remember only last change on the same line (if changes are consequtive withing on line).
Example for jumplist. Cursor position at '(', '{' - marked as m.
( smth ) { } %`m%
I jumped firt to ')', next by mark at the same line and next to last bracket
I'm able to do only one ^o - last jump at first line. But expected to jump 2 times more.
Same situation with changes list and g;
't1 t2 t3' -> 't2 t3 t4', I can't move between all 3 t
At least I wanted to jump by same points as stored at undo history. All modern editors like VSCode, Sublime, e.t.c. have this stable option "Alt -" / "Alt +" to jump all prev. cursor positions. What is Vim way?
Founded this when tried to make macro, which swaps any 2 pair of brackets.
{ smth } (
)
Let's mark first pair of brackets with 'mm', to remember whith whom should we swap.
let's mark '{' as main bracket and go to '(', starting macro 'qq'. I'm marking current pair of brackets as 'mc' and swapping later between main and current.
#q=mc%x`m%Plx^O^Op`mx`cPlx`mP
( smth ) {
}
moving cursor to { and running '#q' - we get initial text, double swapping brackets
Problems started, case second pair brackets marked as mm, and cursor located at first pair of brackets. '(' - mm, '{' - start position
{ smth } (
)
only first ^o would work, and then macro will jump outside of this block
There are two rules: "small" moves are never remembered; and, "changes" are remembered upon leaving from insert mode.
So all those macros and stuff they are sort of "batch processing tools", not "usual line editing tools".
Instead, to get t2 t3 t4 from t1 t2 t3 you could do ctrl-a w . w . Or to swap the brackets with d%%p. And so on.
This sort of "small edits" should be done by normals in Vim. So you need to master the normals first. Maybe reading the "Practical Vim" by Drew Neil could help.
In elixir we need to write a lot of maps(key-value pairs).
Syntax is:
%{one: one, two: two, three: three}
How can we write some kind of function/snippet/abbreviation so that we can achieve:
${1:one}: ${1:one} # => %{one: one_} # => where _ denotes cursor
Where we enter one and it does the following:
Enters one in key: value pair, i.e. one: one
Takes cursor to second one's end character i.e. one: one(cursor)
And if we have to use some snippet for this then how can we assign some shortcut-key to it ?
========================
Example:
========================
Say I'm anywhere in a file (mostly inside %{} for elixir)
I hit some keyboard shortcut (e.g. ) and type elephant then i want to get the following:
elephant: elephant and want my cursor at the end of second word's last letter(i.e. t).
Initially %{|} where | denotes cursor.
We press: <C-m> and get %{term|: term}. and term - first word is selected.
We write: elephant and get %{elephant: elephant|}
Done.
I'm new to vim. And I'm pressing too many buttons doing basic text wrapping:
string -> "string"
long string with many words -> 'long string with many words'
a + b * c -> (a + b) * c
(elem0, elem1, elem2) -> [elem0, elem1, elem2] (optional)
I'm doing all that manually: go to begin, Insert mode, press key, Normal mode, (the same for second character).
How to do it faster? E.g.: visually select the text, smart-wrap it with what you need. Or even without visual selection.
string -> "string"
ciw"<C-r>""
long string with many words -> 'long string with many words'
veeeeec'<C-r>"'
a + b * c -> (a + b) * c
vwwc(<C-r>")
(elem0, elem1, elem2) -> [elem0, elem1, elem2] (optional)
"edibxs[<C-r>e]
That one is a bit more complicated:
"edib cut the content of those parentheses into
an arbitrary register, here I used "e
xs cut the closing parenthese then cut the opening one
and enter insert mode
[<C-r>e] insert the opening bracket, followed by the content of
register e, followed by the closing bracket
But yeah, use Tim Pope's Surround.
You can use visuall mode for this. For example you have string. ^ will be cursor positioning. Start in normal mode
1. string # press viwc(your word will be selected and deleted to unnamed register)
^
2. # press " and then <C-r>"(this will paste your selected text) and then press again "
This method can be with any surrounding parenthesis or brackets and with any number of words. you just need to change your selection in visual mode
Something that I find myself doing often is yanking the text between two parenthesis and pasting that over another pair of parenthesis. For example:
foo(int a, int b, int c)
bar(int d, int e)
becomes
foo(int a, int b, int c)
bar(int a, int b, int c)
Is there a quick way in Vim to yank the text from foo and paste it over the text in bar?
Yank the content of the first pair of parentheses:
yib
Visually select the content of the second pair of parentheses and put:
vibp
One way would be yi) inside foo's arguments and "_di)P within bar's arguments.
yi) yanks the text inside the parentheses
"_di)P uses the null register to delete the text inside the parentheses and pastes the text, vi)p also works and avoids the null register
The only thing changing is the function name though, so you could also just yank the line and use cw (change word) to change foo to bar.
Cursor over the first paren of foo, then use y% to yank all the text until the matching paren. (You can also use v%y if you prefer to visually see the text you're yanking.)
Then cursor over the first paren of bar, then use v%p. It selects the text up until the matching paren then pastes over it.
Use this to go to last parenthesis shift + 5.
Press 5 twice for the first parentheses.
I use vim-scripts/ReplaceWithRegister.
Copy as usual with
yi(
Paste with gri(
I know how to copy to the clipboard but how can I append to it?
I use this in my code:
let #+ = my_expression
but that overwrites the clipboard.
I know that I can use the registers a-z to append to:
let #B = my_expression
which appends to register b, but what do I when I want to append to the clipboard?
use:
let #+ = #+ . my_expression
or shorter:
let #+ .= my_expression
Reference: :help :let.=
If you're not macro-ing, it's probably worth checking out registers as well. :help registers was mind-blowing.
In an oversimplified nutshell, there are 26 additional "customizable clipboards", called registers, where you can store text, starting with a and going through z.
You add text to a register in command mode by hitting ", naming the register (say f), and then typing the "motion" you want to select text.
NOTE: We're using the named f register here b/c it's probably under your left index finger. That is, f is picked just b/c it's handy. You could replace f with a or u or z or whatever throughout if you wanted.
Copying with a register (cursor at [T]):
Initial File State
This is my first line.
[T]his is my second line.
This is my third line.
Type "fyy in command mode to fill the register with one line (yy).
Type p (* see below) to immediately paste it from the default register.
Type "f to pick the f register and then p to paste from the f register directly. Right now f and default as the same.
So the result of typing "fyyp is exactly the same as having typed yyp with the default clipboard.
Result
This is my first line.
This is my second line.
[T]his is my second line.
This is my third line.
Appending to a register:
Use the capital letter to append to your existing register.
In the above example after pasting, press j to go down a line and then "Fyy. Then type p to paste. You've appended "This is my third line." to f's contents.
Result
This is my first line.
This is my second line.
This is my second line.
This is my third line.
This is my second line.
[T]his is my third line.
(Using a lower case f would have cleared out f's contents and ended up with it only holding "This is my third line.")
Why does p paste what's in register f immediately after you yanked into f? Because your default register holds a pointer to the last selection, and apparently doesn't simply hold what you added to f, but pulls everything that's in f when you append. It might be more expository to say, in the first case, "the result of typing "fyy"fp is exactly the same as having typed yyp with the default clipboard."
But if you were now to yy a new line into the default register, you can hit "f to select the f register and then p to paste that previous value.