Skip to content

Commit 84559a2

Browse files
authored
Merge pull request #6 from MilesCranmer/more-examples
Create example of parentheses matching
2 parents 7c5598c + 0ba35ec commit 84559a2

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ can turn off any activated mode with `-t|--turn-off-mode`.
7171
## Example 1
7272
Delete lines 10-15, and print the remainder:
7373

74-
```
74+
```bash
7575
cat myfile.txt | vims '10,15d'
7676
```
7777

@@ -81,7 +81,7 @@ cat myfile.txt | vims '10,15d'
8181
## Example 2
8282
Delete blank lines, then lower-case everything:
8383

84-
```
84+
```bash
8585
cat mylog.log | vims -e '^\s*$' 'dd' '.' 'Vu'
8686
```
8787

@@ -93,7 +93,7 @@ cat mylog.log | vims -e '^\s*$' 'dd' '.' 'Vu'
9393

9494
Or, with line exe mode (a shorthand for `.*`):
9595

96-
```
96+
```bash
9797
cat mylog.log | vims -e '^\s*$' 'dd' -l 'Vu'
9898
```
9999

@@ -103,7 +103,7 @@ cat mylog.log | vims -e '^\s*$' 'dd' -l 'Vu'
103103

104104
Add a comment (`#`) on every line NOT containing foo:
105105

106-
```
106+
```bash
107107
cat script.sh | vims -r 'foo' 'A # Comment'
108108
```
109109

@@ -115,7 +115,7 @@ cat script.sh | vims -r 'foo' 'A # Comment'
115115

116116
Delete all modifications to files in a git repo:
117117

118-
```
118+
```bash
119119
git status | vims '1,/modified/-1d' '$?modified?,$d' -l 'df:dw' | xargs git checkout --
120120
```
121121

@@ -130,7 +130,7 @@ git status | vims '1,/modified/-1d' '$?modified?,$d' -l 'df:dw' | xargs git chec
130130
## Example 5
131131

132132
Move all Python classes to the bottom of a file:
133-
```
133+
```bash
134134
cat myscript.py | vims -e '^class' 'V/^\\S\<enter>kdGp'
135135
```
136136

@@ -143,7 +143,7 @@ cat myscript.py | vims -e '^class' 'V/^\\S\<enter>kdGp'
143143

144144
Only print the last 6 lines (just like tail)
145145

146-
```
146+
```bash
147147
cat txt | vims -n '$-5,$p'
148148
```
149149
- `-n` - Don't print all lines automatically
@@ -154,13 +154,13 @@ cat txt | vims -n '$-5,$p'
154154

155155
Replace all multi-whitespace sequences with a single space:
156156

157-
```
157+
```bash
158158
cat txt | vims '%s/\s\+/ /g'
159159
```
160160

161161
Which can also be done in exe mode:
162162

163-
```
163+
```bash
164164
cat txt | vims -e '.' ':s/\\s\\+/ /g\<enter>'
165165
```
166166

@@ -170,7 +170,7 @@ when you are typing a character like `\s`, but not like `\<enter>`.
170170
## Example 8
171171
Resolve all git conflicts by deleting the changes on HEAD (keep the bottom code):
172172

173-
```
173+
```bash
174174
cat my_conflict.cpp | vims -e '^=======$' 'V?^<<<<<<< \<enter>d' -t '%g/^>>>>>>> /d'
175175
```
176176

@@ -185,7 +185,7 @@ cat my_conflict.cpp | vims -e '^=======$' 'V?^<<<<<<< \<enter>d' -t '%g/^>>>>>>>
185185

186186
Uncomment all commented-out lines (comment char: `#`)
187187

188-
```
188+
```bash
189189
cat script.sh | vims -e '^\s*#' '^x'
190190
```
191191

@@ -197,7 +197,7 @@ cat script.sh | vims -e '^\s*#' '^x'
197197

198198
Delete the first word of each line and put it at the end:
199199

200-
```
200+
```bash
201201
cat script.sh | vims -e '^[A-Za-z]' '\"kdwA \<esc>\"kp'
202202
```
203203

@@ -210,7 +210,7 @@ cat script.sh | vims -e '^[A-Za-z]' '\"kdwA \<esc>\"kp'
210210

211211
Run a super-vanilla long chain of commands in simple mode, starting from line 1 of a file:
212212

213-
```
213+
```bash
214214
cat python.py | vims -s '/^class\<enter>O# This class broke\<esc>Go\<enter># This file broke'
215215
```
216216

@@ -224,7 +224,7 @@ cat python.py | vims -s '/^class\<enter>O# This class broke\<esc>Go\<enter># Thi
224224

225225
Reverse a file:
226226

227-
```
227+
```bash
228228
cat text.txt | vims '%g/.*/m0'
229229
```
230230

@@ -237,7 +237,7 @@ cat text.txt | vims '%g/.*/m0'
237237
Sort the output of `ls -l` by file size, using the
238238
unix command `sort` (which you can use inside vim):
239239

240-
```
240+
```bash
241241
ls -l | vims '1d' '%!sort -k5n'
242242
```
243243

@@ -246,6 +246,28 @@ ls -l | vims '1d' '%!sort -k5n'
246246
- `sort` - The unix sort command
247247
- `-k5n` - Sort by column 5, numerically
248248

249+
## Example 14
250+
251+
Find matching parentheses for a function call (something `sed` and other regexp tools can't do),
252+
and replace only those parentheses with square brackets:
253+
254+
```bash
255+
> echo "0.9 * (sqrt(3.9 * (0.8 - 0.2)) / 20.0)" | vims -e 'sqrt(' '/sqrt(\<enter>f(lvh%hxhi[]\<esc>P' -t '%s/]()/]/g'
256+
0.9 * (sqrt[3.9 * (0.8 - 0.2)] / 20.0)
257+
```
258+
259+
- `-e 'sqrt('` - Only run this vim command on matching lines
260+
- `/sqrt(\<enter>` - In vim, hit `/` to start a forward search, then search for "sqrt(" and go to the first match
261+
- `f(` - From the first character of "sqrt", go to the open bracket
262+
- `lv` - Move right, so we are inside the "sqrt". Start visually selecting.
263+
- `h%` - Move back left to the open bracket, and use vim's `%` command to move to the matching closing bracket
264+
- `hx` - Move left, so we are inside the "sqrt" (but now on the far side). Delete the contents (which is saved to the clipboard!)
265+
- `hi` - Move left, so we are now at the open bracket again.
266+
- `[]` - Write out `[]` which will be our new square brackets
267+
- `\<esc>` - Back to normal mode
268+
- `P` - Paste the contents into the `[]`
269+
- `-t '%s/]()/]/g'` - This is a new command which simply cleans up the `()`
270+
249271
# Credit
250272

251273
The heart of this script comes from a Google groups posting:

0 commit comments

Comments
 (0)