@@ -71,7 +71,7 @@ can turn off any activated mode with `-t|--turn-off-mode`.
71
71
## Example 1
72
72
Delete lines 10-15, and print the remainder:
73
73
74
- ```
74
+ ``` bash
75
75
cat myfile.txt | vims ' 10,15d'
76
76
```
77
77
@@ -81,7 +81,7 @@ cat myfile.txt | vims '10,15d'
81
81
## Example 2
82
82
Delete blank lines, then lower-case everything:
83
83
84
- ```
84
+ ``` bash
85
85
cat mylog.log | vims -e ' ^\s*$' ' dd' ' .' ' Vu'
86
86
```
87
87
@@ -93,7 +93,7 @@ cat mylog.log | vims -e '^\s*$' 'dd' '.' 'Vu'
93
93
94
94
Or, with line exe mode (a shorthand for ` .* ` ):
95
95
96
- ```
96
+ ``` bash
97
97
cat mylog.log | vims -e ' ^\s*$' ' dd' -l ' Vu'
98
98
```
99
99
@@ -103,7 +103,7 @@ cat mylog.log | vims -e '^\s*$' 'dd' -l 'Vu'
103
103
104
104
Add a comment (` # ` ) on every line NOT containing foo:
105
105
106
- ```
106
+ ``` bash
107
107
cat script.sh | vims -r ' foo' ' A # Comment'
108
108
```
109
109
@@ -115,7 +115,7 @@ cat script.sh | vims -r 'foo' 'A # Comment'
115
115
116
116
Delete all modifications to files in a git repo:
117
117
118
- ```
118
+ ``` bash
119
119
git status | vims ' 1,/modified/-1d' ' $?modified?,$d' -l ' df:dw' | xargs git checkout --
120
120
```
121
121
@@ -130,7 +130,7 @@ git status | vims '1,/modified/-1d' '$?modified?,$d' -l 'df:dw' | xargs git chec
130
130
## Example 5
131
131
132
132
Move all Python classes to the bottom of a file:
133
- ```
133
+ ``` bash
134
134
cat myscript.py | vims -e ' ^class' ' V/^\\S\<enter>kdGp'
135
135
```
136
136
@@ -143,7 +143,7 @@ cat myscript.py | vims -e '^class' 'V/^\\S\<enter>kdGp'
143
143
144
144
Only print the last 6 lines (just like tail)
145
145
146
- ```
146
+ ``` bash
147
147
cat txt | vims -n ' $-5,$p'
148
148
```
149
149
- ` -n ` - Don't print all lines automatically
@@ -154,13 +154,13 @@ cat txt | vims -n '$-5,$p'
154
154
155
155
Replace all multi-whitespace sequences with a single space:
156
156
157
- ```
157
+ ``` bash
158
158
cat txt | vims ' %s/\s\+/ /g'
159
159
```
160
160
161
161
Which can also be done in exe mode:
162
162
163
- ```
163
+ ``` bash
164
164
cat txt | vims -e ' .' ' :s/\\s\\+/ /g\<enter>'
165
165
```
166
166
@@ -170,7 +170,7 @@ when you are typing a character like `\s`, but not like `\<enter>`.
170
170
## Example 8
171
171
Resolve all git conflicts by deleting the changes on HEAD (keep the bottom code):
172
172
173
- ```
173
+ ``` bash
174
174
cat my_conflict.cpp | vims -e ' ^=======$' ' V?^<<<<<<< \<enter>d' -t ' %g/^>>>>>>> /d'
175
175
```
176
176
@@ -185,7 +185,7 @@ cat my_conflict.cpp | vims -e '^=======$' 'V?^<<<<<<< \<enter>d' -t '%g/^>>>>>>>
185
185
186
186
Uncomment all commented-out lines (comment char: ` # ` )
187
187
188
- ```
188
+ ``` bash
189
189
cat script.sh | vims -e ' ^\s*#' ' ^x'
190
190
```
191
191
@@ -197,7 +197,7 @@ cat script.sh | vims -e '^\s*#' '^x'
197
197
198
198
Delete the first word of each line and put it at the end:
199
199
200
- ```
200
+ ``` bash
201
201
cat script.sh | vims -e ' ^[A-Za-z]' ' \"kdwA \<esc>\"kp'
202
202
```
203
203
@@ -210,7 +210,7 @@ cat script.sh | vims -e '^[A-Za-z]' '\"kdwA \<esc>\"kp'
210
210
211
211
Run a super-vanilla long chain of commands in simple mode, starting from line 1 of a file:
212
212
213
- ```
213
+ ``` bash
214
214
cat python.py | vims -s ' /^class\<enter>O# This class broke\<esc>Go\<enter># This file broke'
215
215
```
216
216
@@ -224,7 +224,7 @@ cat python.py | vims -s '/^class\<enter>O# This class broke\<esc>Go\<enter># Thi
224
224
225
225
Reverse a file:
226
226
227
- ```
227
+ ``` bash
228
228
cat text.txt | vims ' %g/.*/m0'
229
229
```
230
230
@@ -237,7 +237,7 @@ cat text.txt | vims '%g/.*/m0'
237
237
Sort the output of ` ls -l ` by file size, using the
238
238
unix command ` sort ` (which you can use inside vim):
239
239
240
- ```
240
+ ``` bash
241
241
ls -l | vims ' 1d' ' %!sort -k5n'
242
242
```
243
243
@@ -246,6 +246,28 @@ ls -l | vims '1d' '%!sort -k5n'
246
246
- ` sort ` - The unix sort command
247
247
- ` -k5n ` - Sort by column 5, numerically
248
248
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
+
249
271
# Credit
250
272
251
273
The heart of this script comes from a Google groups posting:
0 commit comments