Skip to content

kaddkaka/vim_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Builtins

Increment numbers (incremental sequence)

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:

->

g_ctrl_a

(key sequence in video: jVG^A..uuugvg^A..uuugv10g^A)

Tip: This also works with rectangle selection (ctrl-v).

Replay macro on each line in visual selection

: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.

norm_q

(key sequence in video: qqIconst ^[A;^[qjVjjj:norm @q<enter>, q register content: Iconst ^[A;^[)

Navigate quickfix list

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 of en in all files in cwd (current working directory).
  • :copen is used to open the quickfix window to show the quickfix list.

qflist

Mapping suggestions:

nnoremap <a-j> <cmd>cnext<cr>
nnoremap <a-k> <cmd>cprev<cr>
  • :vimgrep /def test/ **/*.py find all matches of "def test" in all pythons files recursively from cwd.
  • :cdo executes a command at each entry in the quickfix list.

Use ! to run external command (e.g. sort lines)

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.

filter in selection

(key sequence in video: vip!sort -n<enter>)

Use filtering and awk to sum all numbers in a specific column

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 the END.

filter with awk

global: repeat a command for each line matching a pattern

With :global a command can be repeated for each line that matches a pattern. Default pattern is last used search pattern.

  • :g//d deletes all lines matching the last search pattern
  • :g//d E delete the same lines and appends each delete to register e which can be pasted with "ep

global delete, append to register

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

Replace only within selection

The search pattern atom \%V can be used to match inside visual area. This can be used to replace only within a (rectangle) selection.

sub_in_selection

(key sequence in video: wwww^VG$se/o<enter>)

Mapping suggestion:

xnoremap s :s/\%V

Delete to search motion

Normal 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 /e in d/en/e to delete to the end of the match.

d to search

(key sequence in video: d/en^G^G<enter>)

Select what was just pasted, reselect last selection

`[ 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.

gv

(key sequence in video: viByPgg`[v`]^[jjjgv)

Mapping suggestion:

nnoremap <leader>gv `[v`]

Repeat the last command line

@: 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>. @:

Retrieve the word under cursor

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 rebase edit-todo file) to show git information about that commit.

Recursive macros

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)

Recursive within line

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:

  1. qq ciw"<c-r>-"<esc>f l@q q: surround each "word" on the line in quotes
  2. qq gUiw2f l@q q: 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 qqqqq

Questions:

  • 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?

Repeat last change in all of file ("global repeat", similar to 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.

global repeat

Mapping suggestion:

nnoremap g. :%s//<c-r>./g<esc>

Ideas/TODOs

  • appending to registers ("Ayy)
  • inserting literal characters (ctrl-v)
  • pasting from register in insert mode (ctrl-r)
  • batch changes (:cdo, :bufdo, :windo, :argdo, :ldo)
  • changelist: go back to previous edit location (g;)
  • insert at previous insert location (gi)
  • normal commands in insert mode (ctrl-o)

About

Some examples of vim features captured from terminal with asciinema.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published