• Stars
    star
    103
  • Rank 333,175 (Top 7 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

vims - an improved CLI for vim, to use it like sed or awk

vims

CI Codecov branch

Demo

Ever wish you could simply apply a vim command to every line in a command output automatically? If you use vim as your primary editor, this is likely much much easier than having to look up sed commands every time you filter a command output.

E.g., cat file.txt | vims -l 'f|d$' will go through every line, and run "f|d$", which is the vim command for deleting every character after "|". See below for many other useful features.

Install

To install, put vims somewhere on your path, e.g., /usr/bin.

Usage

... | vims [...]

  • (default) -t [EX_CMD] Ex mode. Works as if you typed ":" in vim.
  • -s [CMD] Simple command mode. Starts on the first line in command mode (e.g., x deletes a char).
  • -l [CMD] Line command mode. Runs the command on every line.
  • -e [REGEX] [CMD] Exe mode. Runs the command on every line matching REGEX (uses vim regex).
  • -r [REGEX] [CMD] Inverse exe mode. Runs the command on every line not matching REGEX (uses vim regex).
  • -n quiet. Don't print lines to stdout. You will then have to use :p command to print manually.
  • -h help. Print this documentation.

Note that for commands, you can write \<esc> to hit the escape key, or \<c-o> to hit ctrl-O.

{command} | vims [-n|--quiet]
                 [-e|--exe-mode] [-r|--inverse-exe-mode]
                 [-s|--simple-mode] [-l|--line-exe-mode]
                 [-t|--turn-off-mode]
                 [-h|--help]
                 [ <args>... ]

Call vims on piped input, providing a list of arguments that you would use in vim command-line mode. All lines not deleted are printed by default, but you can turn this off with a -n|--quiet flag.

Discussion:

Trigger "exe" mode using the -e|--exe-mode flag, which creates macros for '%g/$1/exe "norm $2"' (see the power of :g), where $1 is the first arg of a pair, and $2 is the last arg of a pair. This lets you type non-text characters, like \<esc>, \<c-o>, etc.

Likewise, -l|--line-exe-mode translates to %g/.*/exe "norm$1", meaning it executes a command on ALL lines.

Inverse exe mode is done with the -r|--inverse-exe-mode flag, which does the same as exe mode, but only on lines NOT matching the regex.

Use simple mode with the -s|--simple-mode flag, which is as vanilla as it gets. This translates every passed argument to: exe "norm $1", meaning that you can run commands just like you opened the editor, starting at line 1. Use the same backslashes (\<enter>) as you do for exe mode.

Modes are activated for all the proceeding args. You can switch modes partway, by calling the flag for the other mode you want, or you can turn off any activated mode with -t|--turn-off-mode.

Example 1

Delete lines 10-15, and print the remainder:

cat myfile.txt | vims '10,15d'
  • 10,15 - A range from 10-15 - see :help :range in vim for a huge number of options.
  • d - The delete (from ex) command - see :help :d in vim.

Example 2

Delete blank lines, then lower-case everything:

cat mylog.log | vims -e '^\s*$' 'dd' '.' 'Vu'
  • -e - Turn on exe mode
  • ^\s*$ - Line only containing whitespace
  • dd - Delete it.
  • . - Line containing anything (Every pair of arguments triggers a new exe command)
  • Vu - Select the line, then lower-case all alphabetical characters

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

cat mylog.log | vims -e '^\s*$' 'dd' -l 'Vu'
  • -l - Turn off exe mode, turn on line exe mode

Example 3

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

cat script.sh | vims -r 'foo' 'A # Comment'
  • -r - Work on all lines not matching regex
  • foo - Match all lines with the word "foo"
  • A # Comment - At the end of the line, type " # Comment"

Example 4

Delete all modifications to files in a git repo:

git status | vims '1,/modified/-1d' '$?modified?,$d' -l 'df:dw' | xargs git checkout --
  • git status - View which files are modified
  • vims - Start vims in normal mode
  • 1,/modified/-1d - Delete all lines up to the first line with "modified"
  • $?modified?+1,$d - Delete all lines from below the last line with "modified"
  • -l - Turn on line exe mode (execute a command on each line)
  • df:dw - Delete until the ":", then delete the white space
  • xargs git checkout -- - Pass all the filenames to git checkout --

Example 5

Move all Python classes to the bottom of a file:

cat myscript.py | vims -e '^class' 'V/^\\S\<enter>kdGp'
  • '^class' 'V/^\\S\<enter>kdGp' becomes '%g/^class/exe "norm V/^\S\<enter>kdGp"'
    • %g/^class/ - Every line starting with "class"
    • exe - Execute the following, including escaped sequences (so you can call \<c-o> to mean Ctrl-o)
    • norm V/^\S\<enter>kdGp Enter normal mode, visual select to the next zero-indentation line, move up a line, delete, paste it at the bottom

Example 6

Only print the last 6 lines (just like tail)

cat txt | vims -n '$-5,$p'
  • -n - Don't print all lines automatically
  • $-5,$ - A range extending from 6th last line to the last line
  • p - Print

Example 7

Replace all multi-whitespace sequences with a single space:

cat txt | vims '%s/\s\+/ /g'

Which can also be done in exe mode:

cat txt | vims -e '.' ':s/\\s\\+/ /g\<enter>'

Note the double back-slashes needed (only in the second string of a pair in an exe command!) when you are typing a character like \s, but not like \<enter>.

Example 8

Resolve all git conflicts by deleting the changes on HEAD (keep the bottom code):

cat my_conflict.cpp | vims -e '^=======$' 'V?^<<<<<<< \<enter>d' -t '%g/^>>>>>>> /d'
  • -e - Turn on exe mode
  • ^=======$ - Match the middle bit of a git conflict
  • V?^<<<<<<< \<enter>d - Highlight the line, backward search to the top of the conflict, delete it.
  • -t - Turn off exe mode
  • %g/^>>>>>>> /d - Delete remaining conflict lines

Example 9

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

cat script.sh | vims -e '^\s*#' '^x'
  • ^\s*# - Work on lines with whitespace followed by a comment char, followed by anything
  • ^x - Go to the first non-whitespace character, and delete it

Example 10

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

cat script.sh | vims -e '^[A-Za-z]' '\"kdwA \<esc>\"kp'
  • ^[A-Za-z] - Only work on lines that start with an alphabetical character
  • \"kdw - Delete the word under the cursor and put it in register k
  • A \<esc> - Start insert mode at front of line, type a space, hit escape key
  • \"kp - Paste from the register k

Example 11

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

cat python.py | vims -s '/^class\<enter>O# This class broke\<esc>Go\<enter># This file broke'
  • /^class\<enter> - Find the first class, and go to it
  • O# This class broke - Type above it: "# This class broke"
  • \<esc>Go\<enter> - Back to normal mode, make two blank lines at end of file
  • # This file broke' - Write at the end of the file: "# This file broke"

Example 12

Reverse a file:

cat text.txt | vims '%g/.*/m0'
  • %g - Work on all lines that match a pattern
  • .* - Matches all lines
  • m0 - Move line to start of file

Example 13

Sort the output of ls -l by file size, using the unix command sort (which you can use inside vim):

ls -l | vims '1d' '%!sort -k5n'
  • 1d - Delete the first line of ls -l
  • %! - Call the following external command on all lines
  • sort - The unix sort command
  • -k5n - Sort by column 5, numerically

Credit

The heart of this script comes from a Google groups posting: posting, and then from an answer on SO

Thanks!

TODO

  • Find way around vim command limit (only can seem to launch ~8 commands at once to vims - see issue #1)

More Repositories

1

PySR

High-Performance Symbolic Regression in Python and Julia
Python
2,369
star
2

symbolic_deep_learning

Code for "Discovering Symbolic Models from Deep Learning with Inductive Biases"
Python
690
star
3

SymbolicRegression.jl

Distributed High-Performance Symbolic Regression in Julia
Julia
614
star
4

lagrangian_nns

Lagrangian Neural Networks
Python
453
star
5

awesome-ml-demos

Curated list of interactive ML demos
336
star
6

DispatchDoctor.jl

The dispatch doctor prescribes type stability
Julia
142
star
7

anki_science

Anki decks for physics, astronomy, computer science, machine learning, and statistics.
Jupyter Notebook
134
star
8

AirspeedVelocity.jl

Easily benchmark a Julia package over its commit history
Julia
94
star
9

gso

🏃 Google StackOverflow in Vim. Copy-pastes the code directly in your script.
Vim Script
79
star
10

pysr_paper

A paper describing the implementation of PySR and SymbolicRegression.jl
TeX
49
star
11

sympy2jax

Turning SymPy expressions into JAX functions
Python
42
star
12

python_citations

Bibtex for various Python science and machine learning software
TeX
31
star
13

pysr_scaling_laws

You should use PySR to find scaling laws. Here's an example.
Python
31
star
14

gnn_resource_allocation

Code for our paper on doing resource allocation with graph neural networks
Python
28
star
15

ArguMend.jl

Autosuggestions for function keywords
Julia
19
star
16

bnn_chaos_model

Model and training code for Bayesian neural network for compact planetary instability
Python
9
star
17

DeprecateKeywords.jl

Macro for deprecating keyword parameters
Julia
9
star
18

issue_aggregator

Shell
8
star
19

pysr_tutorial

Jupyter Notebook
7
star
20

rescue_time_statusbar

Show productivity pulse in macOS status bar
Python
7
star
21

pysr_interactive

Experiments in creating a PySR web app
TypeScript
7
star
22

showyourwork_julia_example

TeX
7
star
23

easy_normalizing_flow

Simple normalizing flow with a conditional variable of any size
Python
7
star
24

htm

Human Task Manager. A featureful script-based task manager for projects and to-do's.
Python
6
star
25

easy_distributed_hyperopt

Do distributed hyperparameter optimization using only a shared folder between processes.
Python
5
star
26

differentiable_quantile_transform

Quantile transform that is differentiable for PyTorch
Python
3
star
27

public_CMD_normalizing_flow

A repository accompanying the CMD normalizing flow paper
3
star
28

clean_detex

Use grammar checkers on your LaTeX paper
Shell
3
star
29

xd_vs_flow

XD vs normalizing flows demo
Jupyter Notebook
2
star
30

conferences

Generate a Google calendar of conference deadlines
Python
2
star
31

galago

GaLaGo - Gregory and Loredo algorithm, GPU optimized
C++
2
star
32

git-stats

Calculate number of words changed on an overleaf doc (or any git repo), excluding text that was just moved.
Shell
2
star
33

paper-style-checkers

Style checklists for research papers, implemented programmatically in vim
1
star
34

PySR_Docs

Stores images for the main PySR documentation site
1
star
35

dockers

Docker images and tricks 🐳
Shell
1
star
36

tape_snake

Snake implemented on blinky tape
Python
1
star
37

research_match

Filter a list of names by research interest
Python
1
star
38

gifs

1
star
39

pysr_wandb

Python
1
star