ctrl-a increments the number at or after the cursor, ctrl-x decrements the number. With visual selection an incremental sequence can be achieved with g ctrl-a
before -> after:
(key sequence in video: jVG^A..uuugvg^A..uuugv10g^A)
Tip: This also works with rectangle selection (ctrl-v).
:norm @q in visual mode will perform the normal command @q (play macro in register q) on each line in selection. qq is used to record the macro.
(key sequence in video: qqIconst ^[A;^[qjVjjj:norm @q<enter>, q register content: Iconst ^[A;^[)
The quickfix list can be populated with locations in several ways. A key to using it effectively is to have mappings for :cnext and :cprev.
:vimgrep en *is used to find all occurences ofenin all files in cwd (current working directory).:copenis used to open the quickfix window to show the quickfix list.
Mapping suggestions:
nnoremap <a-j> <cmd>cnext<cr>
nnoremap <a-k> <cmd>cprev<cr>:vimgrep /def test/ **/*.pyfind all matches of "def test" in all pythons files recursively from cwd.:cdoexecutes a command at each entry in the quickfix list.
Filters can be run for example by typing
! with a visual selection. All text will be filtered
through an external command. To sort all lines by numeric sort, use !sort -n.
(key sequence in video: vip!sort -n<enter>)
awk is a powerful tool, here used to sum all the fields of a specific column:
:%! awk '{print; s+=$2} END {print s}'sums the second field ($2) of each line and prints the total at theEND.
With :global a command can be repeated for each
line that matches a pattern. Default pattern is last used search pattern.
:g//ddeletes all lines matching the last search pattern:g//d Edelete the same lines and appends each delete to registerewhich can be pasted with"ep
key sequence in video: *:g//d<enter> u :g//d E<enter>p
:g/pattern/norm @q, play macro on each line matching pattern.:v/;$/ s/$/;, Add;to all lines that does not end in semicolon.
More g power at https://vim.fandom.com/wiki/Power_of_g
The search pattern atom \%V can be used to match inside visual area. This can be used to replace only within a (rectangle) selection.
(key sequence in video: wwww^VG$se/o<enter>)
Mapping suggestion:
xnoremap s :s/\%VNormal commands like d can take any motions for example a search /. When searching and current match is displayed, use ctrl-g to move to the next match.
d/en<c-g><c-g><enter>Will delete everything up to the 3rd match of search pattern "en".- Use a search-offset like
/eind/en/eto delete to the end of the match.
(key sequence in video: d/en^G^G<enter>)
`[ moves to the first character of the previously changed or yanked text, ]` moves to the last. This means that `[v`] will visually select what was just pasted. gv will reselect the previous selected area.
(key sequence in video: viByPgg`[v`]^[jjjgv)
Mapping suggestion:
nnoremap <leader>gv `[v`]@: can be used to repeat the last command line command. More convenient when mapped to a single key and I have currently chosen , as my repeat-command key. , is used to repeat the last fFtT motion in reverse direction, but I seldom use it so I have a mapping that shadows this builtin:
nnoremap , @:Alternative mapping suggestions might be the following, but I know I sometimes want to perform j repeat-command j repeat-command in fast succession.
nnoremap <a-.> @:
nnoremap <leader>. @:ctrl-r can be used in insert mode and the command line to insert contents of registers, but also some other stuff like the word under the cursor!
Using <c-r><c-w> will put the current word under cursor where you are at, but if you want to do this in a command you dont want this combination to "expand" immediately but rather when you execute the command. I asked a question about this at reddit, and got replies from Fantastic_Cow7272 and yegappanl that led me to this kind of command:
:!git show <cword>: Run this with the cursor on a commit hash (for example in a git rebaseedit-todofile) to show git information about that commit.
By playing the content of a register at the end of the recording, you can make
a macro recursive. (Make sure it has a stop condition or you will have to break
it with <ctrl-c>!)
If you forgot to do the macro recursive while recording it the first time, you
make it recursive with the sequence qQ@qq (assuming your macro is in the q
register)
A movement within a line such as f (f followed by space) will stop the
recursive macro at the end of a line. This can be used to modify each word on a
line in some way:
qqciw"<c-r>-"<esc>f l@qq: surround each "word" on the line in quotesqqgUiw2f l@qq: upper-case every 2nd "word" on the line
With both lines below visually selected, replaying macro 1 from above with
:norm e@q will turn:
Recursive over lines
Recursive over lines
Into:
"Recursive" "over" "lines"
"Recursive" "over" "lines"
Mapping suggestion:
nnoremap <leader>q qqqqqQuestions:
- What is actually different between
<ctrl-r>-and<ctrl-r>"? - Why doesn't this mapping seem to work:
nnoremap <leader>q qqqqq? - Is there a "within line" version of
:g?
ctrl-r in insert mode can be
used to insert the same text as last time with the dot (.) register. With this
kind mapping you can easily repeat a modification like ciw (change in word) in
all of the document.
Mapping suggestion:
nnoremap g. :%s//<c-r>./g<esc>