Skip to content

Instantly share code, notes, and snippets.

@awnion
Last active May 1, 2021 22:21
Show Gist options
  • Save awnion/0dec47accef6385d71d5da0e5202a882 to your computer and use it in GitHub Desktop.
Save awnion/0dec47accef6385d71d5da0e5202a882 to your computer and use it in GitHub Desktop.
Some VIM tricks

Apply some vim commands on a file without entering vim

E.g. given 1st line "what to edit" and 2nd line "what vim commands to execute"

  • -E Ex mode: vim will treat STDIN as if it's just list of commands
  • here ^E means key press, so here 2 strategies to deal with it
    • replace ^E with new_line + ":norm "
    • replace ^E with hex code of ESC which is \x1b
head -1 > 1.tmp
vim -E 1.tmp <<EOF
:norm `sed 's/\^E/\n:norm /g'`
:wq
EOF
cat 1.tmp

or print to the STDOUT instead of file

head -1>1
vim -e 1 <<<"norm `sed 's~\^E~\x1b~g'`
%p|q"
  • p = print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment