Matching square brackets in vim - vim

I am aware of the shortcut for flower braces i.e. Having cursor over { and pressing ]} gets me to the corresponding }. How to do the same for matching square brackets? []

In normal mode, try
vi[oh
vi[ should visually select the entire block between [], and o switch cursor to the front of visual selection, then h moves cursor onto [.
At the expense of more keystrokes, this works in more cases than % where you must hover over the bracket.

Related

Vim change & jump lists override behaviour

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.

How to multi-line the import functions in {} in VIM?

Sorry for the bad title but
With this code,,,
import { amethod, methodb, methodc } from '../../utils/mockData';
how do you make this
import {
amethod,
methodb,
methodc
} from '../../utils/mockData';
with VIM? I mean how can I do it fast?
What I do is to
go to the first method.
press Enter
go to the end of the method..
press Enter...
go to the second method.
6....
...
It's so slow with VIM. Can we do this fast in VIM? I think I can this much faster with my mouse :(
import { amethod, methodb, methodc } from '../...';
^
f ;r<CR>;.;.;.
doesn't strike me as particularly slow. A tad too repetitive, maybe?
Here is a slightly smarter (but probably not that smart) approach:
ciB " change in brackets
<CR><CR> " insert two carriage returns
<Up> " move up one line
<C-r>" " insert previous content of brackets
<Esc> " leave insert mode
:s/,/,\r/g<CR> " put each symbol on its own line
=iB " re-indent the content of brackets
that can be mapped for convenience:
nnoremap <key> ciB<CR><CR><Up><C-r>"<Esc>:s/,/,\r/g<CR>=iB
Or you can look for a proper plugin that handles corner cases gracefully.
#romainl had a good answer, but you can also use replace for this particular case:
:%s/\([,{]\)/\1\n/g
With the cursor on line1 and col1, you can press this in normal mode:
f 4#=';r^M'
then press ENTER.
Note: for the ^M you press Ctrl-v then Enter
I have got this
import {
amethod,
methodb,
methodc
} from '../../utils/mockData';
Using this:
:%s/\v\{\zs( \w+,?)+ \ze}/\=substitute(submatch(0), " ", "\n\t", "g")
\v ............ very magic regex (avoid many backslashes)
{ ............ literal {
\zs ........... vim trick that marks the start of the pattern
( ............ start of regex group 1
<Space> ....... literal space inside group 1
\w+ ........... one word or more
,? ........... optional coma
+ ............ quantifier for the group (at least one)
<Space>
\ze ........... end of our vim search
The substitute function has three parts like a normal vim substitution and the submatch(0) corresponds to our regex, hence we are substituting in our regex one space for one line breake and two tabs.

Vim: delete everything in a line after character without moving cursor in the first place

Suppose the following line and cursor position:
foo = some_func(1 +[ ]2)
^^^
cursor position
Using di + ( I could easily get rid of everything inside the brackets or delete everything to line start or end using d^ and d$ respectively, but what would I do if I would like to delete everything that comes after =?
The resulting line should be:
foo =[ ]
^^^
cursor position
dT+= deletes everything until (backwards) the character =, but it still leaves 2) in the line, ending up in:
foo =[2])
^^^
cursor position
I could, of course, jump to = first and then use d$ to delete everything until line ending, but I would prefer a simple shortcut based on current cursor position without the need to move the cursor (if such a shortcut exists).
T=D (Jump to after =, delete to end of line) is the shortest way, I believe.
You can do it without the movement first, but it is considerably more complex: :s/=\#<=.*// CR

What are the vim commands that start with g?

g is a prefix to several commands. e.g. goto to move the cursor, but also gqip to format a paragraph. Where is the reference for all commands that are prefixed with g?
Vim's documentation is http://vimdoc.sourceforge.net/. If you go for the HTML docs, you will find |reference_toc| More detailed information for all commands, which includes |index.txt| alphabetical index of all commands, which -- due to an unfortunate quirk with the doc file named index.txt and linked as index.html -- doesn't actually lead to where you would expect it to lead.
Long story short, http://vimdoc.sourceforge.net/htmldoc/vimindex.html#g is the documentation you are looking for ("Commands starting with 'g'").
Alternatively, type :help *g* in Vim.
(Sorry merlin2011 but your list is somewhat incomplete...)
Some reformatting applied:
2.4 Commands starting with 'g'
char note action in Normal mode
------------------------------------------------------------------
g CTRL-A only when compiled with MEM_PROFILE
defined: dump a memory profile
g CTRL-G show information about current cursor
position
g CTRL-H start Select block mode
g CTRL-] |:tjump| to the tag under the cursor
g# 1 like "#", but without using "\<" and "\>"
g$ 1 when 'wrap' off go to rightmost character of
the current line that is on the screen;
when 'wrap' on go to the rightmost character
of the current screen line
g& 2 repeat last ":s" on all lines
g'{mark} 1 like |'| but without changing the jumplist
g`{mark} 1 like |`| but without changing the jumplist
g* 1 like "*", but without using "\<" and "\>"
g0 1 when 'wrap' off go to leftmost character of
the current line that is on the screen;
when 'wrap' on go to the leftmost character
of the current screen line
g8 print hex value of bytes used in UTF-8
character under the cursor
g< display previous command output
g? 2 Rot13 encoding operator
g?? 2 Rot13 encode current line
g?g? 2 Rot13 encode current line
gD 1 go to definition of word under the cursor
in current file
gE 1 go backwards to the end of the previous
WORD
gH start Select line mode
gI 2 like "I", but always start in column 1
gJ 2 join lines without inserting space
["x]gP 2 put the text [from register x] before the
cursor N times, leave the cursor after it
gQ switch to "Ex" mode with Vim editing
gR 2 enter Virtual Replace mode
gU{motion} 2 make Nmove text uppercase
gV don't reselect the previous Visual area
when executing a mapping or menu in Select
mode
g] :tselect on the tag under the cursor
g^ 1 when 'wrap' off go to leftmost non-white
character of the current line that is on
the screen; when 'wrap' on go to the
leftmost non-white character of the current
screen line
ga print ascii value of character under the
cursor
gd 1 go to definition of word under the cursor
in current function
ge 1 go backwards to the end of the previous
word
gf start editing the file whose name is under
the cursor
gF start editing the file whose name is under
the cursor and jump to the line number
following the filename.
gg 1 cursor to line N, default first line
gh start Select mode
gi 2 like "i", but first move to the |'^| mark
gj 1 like "j", but when 'wrap' on go N screen
lines down
gk 1 like "k", but when 'wrap' on go N screen
lines up
gm 1 go to character at middle of the screenline
go 1 cursor to byte N in the buffer
["x]gp 2 put the text [from register x] after the
cursor N times, leave the cursor after it
gq{motion} 2 format Nmove text
gr{char} 2 virtual replace N chars with {char}
gs go to sleep for N seconds (default 1)
gu{motion} 2 make Nmove text lowercase
gv reselect the previous Visual area
gw{motion} 2 format Nmove text and keep cursor
gx execute application for file name under the
cursor (only with |netrw| plugin)
g#{motion} call 'operatorfunc'
g~{motion} 2 swap case for Nmove text
g<Down> 1 same as "gj"
g<End> 1 same as "g$"
g<Home> 1 same as "g0"
g<LeftMouse> same as <C-LeftMouse>
g<MiddleMouse> same as <C-MiddleMouse>
g<RightMouse> same as <C-RightMouse>
g<Up> 1 same as "gk"
note: 1 = cursor movement command; 2 = can be undone/redone
Open vim. Type :help g.
2.4 Commands starting with 'g' g
tag char note action in Normal mode
------------------------------------------------------------------------------
g_CTRL-A g CTRL-A only when compiled with MEM_PROFILE
defined: dump a memory profile
g_CTRL-G g CTRL-G show information about current cursor
position
g_CTRL-H g CTRL-H start Select block mode
g_CTRL-] g CTRL-] :tjump to the tag under the cursor
g# g# 1 like "#", but without using "\<" and "\>"
g$ g$ 1 when 'wrap' off go to rightmost character of
the current line that is on the screen;
when 'wrap' on go to the rightmost character
of the current screen line
g& g& 2 repeat last ":s" on all lines
g' g'{mark} 1 like ' but without changing the jumplist
g` g`{mark} 1 like ` but without changing the jumplist
gstar g* 1 like "*", but without using "\<" and "\>"
g+ g+ go to newer text state N times
g, g, 1 go to N newer position in change list
g- g- go to older text state N times
g0 g0 1 when 'wrap' off go to leftmost character of
the current line that is on the screen;
when 'wrap' on go to the leftmost character
of the current screen line
g8 g8 print hex value of bytes used in UTF-8
character under the cursor
g; g; 1 go to N older position in change list
g< g< display previous command output
The list above has been truncated for readability.

Vim - delete until (inclusive) character in multiple lines

I have this code:
def foo(c: Char) = c match {
case 'a': 'B'
}
My cursor is on the space after =. I want to delete everything until, including, the }. How can I do that?
Can I do the same where the cursor is anywhere on the first line? Anywhere in the block (and place the cursor after the =)?
d/}/e
does the job.
d/} deletes until the } but adding the /e flag moves the cursor on the last char of the match, effectively deleting everything between the cursor and the }, inclusive.
Using visual selection works too, in a slightly more intuitive way:
v/}<CR>d
Try with this: d%.
The d is for delete and the % moves between braces.
This should work:
d}
This deletes one paragraph forward.
You can achieve something like this with the EasyMotion plugin.

Resources