From 35e2c4153c62ac86bbaddf0f2d869257d1cd2ac2 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Mon, 18 Jan 2016 14:38:53 +0100 Subject: [PATCH] Basics: macros? --- CHANGELOG.md | 1 + README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62bf824..07cf676 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. - Basics: [Changelist? Jumplist?](README.md#changelist-jumplist) - Basics: [Sessions?](README.md#sessions) - Basics: [Folding?](README.md#folding) +- Basics: [Macros?](README.md#macros) - Commands: [:redir](README.md#redir) - Tips: [Saner command-line history](README.md#saner-command-line-history) - Tips: [Reload a file on saving](README.md#reload-a-file-on-saving) diff --git a/README.md b/README.md index 708fb53..4e6a35e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ added every day. Things about to be added can be found here: - [Autocmds?](#autocmds) - [Changelist? Jumplist?](#changelist-jumplist) - [Quickfix and location lists?](#quickfix-and-location-lists) +- [Macros?](#macros) - [Colorschemes?](#colorschemes) - [Folding?](#folding) - [Sessions?](#sessions) @@ -622,6 +623,64 @@ list. Assuming any files contained the string "foo", it should be shown now in the quickfix window. +#### Macros? + +Vim allows _recording_ typed characters into a [register](#registers). It's a +great way to automate certain tasks on the fly. (For more elaborate tasks, [Vim +scripting](#vim-scripting) should be used instead.) + +- Start recording by typing `q` followed by the register, e.g. `q`. (The + command-line will signify this via "recording @q".) +- Stop recording by by hitting `q` once again. +- Execute the macro via `[count]@q`. +- Repeat the last used macro via `[count]@@`. + +**Example 1:** + +Insert a line and repeat it 10 times: + +``` +qq +iabc +q +10@q +``` + +(The same could be done without registers: `oabc10.`) + +**Example 2:** + +For adding line numbers in front of all lines, start on the first line and add +"1. " to it manually. Increment the number under the cursor by using ``, +displayed as `^A`. + +``` +qq +0yf jP0^A +q +1000@q +``` + +Here we simply hope that the file doesn't contain more than 1000 lines when +using `1000@q`, but we can also use a _recursive macro_, which executes until +the macro can't be applied to a line anymore: + +``` +qq +0yf jP0^A@q +q +@q +``` + +Also see: [Quickly edit your macros](#quickly-edit-your-macros) + +Related help: + +``` +:h recording` +:h 'lazyredraw' +``` + #### Colorschemes? Colorschemes are the way to style your Vim. Vim consists of many components and