diff --git a/notes/shell/unix_bash.md b/notes/shell/unix_bash.md index 66dacb3..ff548d0 100644 --- a/notes/shell/unix_bash.md +++ b/notes/shell/unix_bash.md @@ -219,91 +219,7 @@ commands operate on entire lines at once n For all lines matching the regex, run the command sequence. `\` separate lines -#### Batch editing with ex -Ex is vim's equivalent of `ed` and `ex` is symlinked as `vim -e` on many -systems. It's equivalent to vim's command line, similar to `ed`, though many -commands are different - - $ ex file - $ vim -Nes file -Roughly equivalent ways of entering ex-mode. `Q` also works from within vim - - :g/re/p Globally exectute a command on lines with /re/ - :v/re/p Inverse of :g. Executes on all lines without /re/ - :3,6co$ Copy lines [3,6] to the end of the document - :3m6 Move line 3 to line 6 - :z=3 Pretty print lines in [-2,+2] -Some infrequently used vim commands are very important in ex-mode - - :g/re/z3 -Prints all the lines and line numbers that contain regex `re` - - :g/bash/exe "normal! cfish\" | undo | nu -Changes every line with "bash" to "fish", undoes that, then prints the line - - :g/string/nu | g/num/nu -Does NOT print all the lines with `string` or `num`. This prints all the lines -with `string` then reprints them if they also have `num`. `:g` only uses a new -line to delimit its commands from the next set! - -Batch editing style (in bash): - 1. Here-string: For only one command, here-strings are a quick and easy choice - - $ for f in $(find ~/); do vim -Nes <<<"g/re/p"; done -Prints all lines with `re` in the home directory. Be careful chaining with `:g` - - 2. Here-ansi-c-string: Allows including c-style escape sequences - - $ for f in $(find ~/); do vim -Nes <<< $'g/re/nu\n3'; done -Prints all lines with `re` then moves to line 3. Often can be avoided with `|` - - 3. Here-documents: Probably the best choice for quick batch edits - - $ for file in $(fd -at type subs_) - do - vim -Nes $file <<'DOC' - g/^Stl/exe "norm! cStyle: new\\" - $ | a - # ex: ff=unix: - . - wq - DOC - done - -Changes The style lines and appends a modeline to files found by `fd` - - $ for file in ~/.bash*; do vim -Nes $file <\" +$ | a +# ex: ff=unix: +. +wq +EX +done +``` + +# Batch editing with ex +Ever needed to apply the same edit to several files? Well ex-mode is the only +generalized solution for this + +Ex-mode is vim's equivalent of `ed` and `ex` is symlinked as `vim -e` on many +systems. Use the `-N` flag for a more familiar experience. Enter this mode while +in vim with `gQ`. Using the `ex` executable is slightly different from neovim's +implementation, notably neovim doesn't echo back with `nu` and `p` + +```bash +ex file +vim -Nes file +``` + +Ex-mode uses vim's command-mode syntax, which is similar though different from +visual mode + +``` +:21 Goes to line 21. ^ and $ are for the first and last line +:10,20d Deletes lines 10 through 20, inclusive on both ends +:u[ndo] Undoes the last action +:g/re/p Globally exectute a command on lines with /re/ +:v/re/p Inverse of :g. Executes on all lines without /re/ +:3,6co$ Copy lines [3,6] to the end of the document +:3m6 Move line 3 to line 6 +:z=3 Pretty print lines in [-2,+2] +``` + +Several commands can be chained with `|`, similar to `;` in bash + +``` +:g/bash/exe "normal! cfish\" | undo | nu +``` + +Changes every line with "bash" to "fish", undoes that, then prints the line + + :g/string/nu | g/num/nu +Does NOT print all the lines with `string` or `num`. This prints all the lines +with `string` then reprints them if they also have `num`. `:g` only uses a new +line to delimit its commands from the next set! + +## Batch editing styles: + 1. Here-string: For only one command, here-strings are a quick and easy choice +```bash +for f in $(find ~/); do nvim -Nes <<<"%s/re/p | wq"; done +``` + + 2. Here-ansi-c-string: Allows including c-style escape sequences +```bash +for f in $(find ~/); do vim -Nes <<< $'%s/re/nu\nwq'; done +``` + + 3. Here-documents: The best choice for quick batch edits +```bash +for file in $(fd -at type subs_); do + nvim -Nes $file <<'DOC' +g/^Stl/exe "norm! cStyle: new\\" +$ | a +# ex: ff=unix: +. +wq +DOC +done +``` + +```bash +for file in ~/.bash*; do vim -Nes $file <