Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Starting Vim

Starting Vim

If you've never played with Vim's startup flags, I don't blame you; the coolest stuff happens after you're inside Vim. But, there are some interesting ways Vim can be interacted with from the command line, and I'll explore a few of those in this talk.

This talk was initially prepared and presented at the March 14, 2016 Baltimore Vim meetup.

3ed7378b8a83d2952488d4edcd22c406?s=128

Stephen Belcher

March 14, 2016
Tweet

Transcript

  1. Starting Vim

  2. vim my_file.txt

  3. Questions?

  4. Questions? Hahaha, just kidding

  5. Startup Modes • Different than NORMAL, INSERT, VISUAL, etc. •

    Can be triggered in several ways: • Command-line flags • Settings in ~/.vimrc, COMMAND mode, etc. • NORMAL mode commands • The name of the program being executed
  6. What's in a Name? • Vim starts differently when it

    is named differently • Ever typed ex instead of exit by accident? • Must be symlinks or duplicates of the binary • Aliases will not work here
  7. What's in a Name? • vi, vim: Regular Vim* •

    gvim: GUI Vim • view: Read-Only • ex: "Ex" mode • exim: "Improved Ex" mode • vimdiff: "Diff" mode • rvim: "Restricted" Vim • evim: "Easy" Vim
  8. What's in a Name? • vi, vim: Regular Vim* •

    gvim: GUI Vim • view: Read-Only • ex: "Ex" mode • exim: "Improved Ex" mode • vimdiff: "Diff" mode • rvim: "Restricted" Vim • evim: "Easy" Vim Some OS vendors build in different behaviors for vi than for vim. By default Vim will treat them the same. CentOS: vi is a copy with compatible set OS X: Built-in aliases can cause Homebrew/ MacPorts to get confused
  9. "Read-Only" Sets readonly option, meaning you have to execute :set

    noreadonly or write with :w! to save
  10. "Read-Only" Sets readonly option, meaning you have to execute :set

    noreadonly or write with :w! to save view vim -R :set readonly
  11. "Read-Only" Sets readonly option, meaning you have to execute :set

    noreadonly or write with :w! to save Use Vim as a pager: export PAGER='view -' view vim -R :set readonly
  12. "Ex" Behave like the ex line editor of old; few

    visual components, edit only with COMMAND-mode
  13. "Ex" Behave like the ex line editor of old; few

    visual components, edit only with COMMAND-mode ex vim -e Q
  14. "Ex" Behave like the ex line editor of old; few

    visual components, edit only with COMMAND-mode Return to "Normal" Vim with :visual ex vim -e Q
  15. "Improved Ex" Like "Ex", but add various Vim improvement goodies

    like tab-completion of COMMAND-mode commands
  16. "Improved Ex" Like "Ex", but add various Vim improvement goodies

    like tab-completion of COMMAND-mode commands exim vim -E gQ
  17. "Restricted" Cannot use shell commands from inside of Vim (many

    plugins will fail)
  18. "Restricted" Cannot use shell commands from inside of Vim (many

    plugins will fail) rvim vim -Z
  19. "Diff" Displays two or more files side-by-side for visual comparison.

  20. "Diff" Displays two or more files side-by-side for visual comparison.

    vimdiff vim -d :diffthis
  21. "Diff" Displays two or more files side-by-side for visual comparison.

    vimdiff vim -d :diffthis Use Vim to resolve merges in git: git config --global merge.tool vimdiff
  22. "Easy" Unmaps many common-but-confusing commands, keeps Vim inside of INSERT

    mode
  23. "Easy" Unmaps many common-but-confusing commands, keeps Vim inside of INSERT

    mode evim :source $VIMRUNTIME/evim.vim vim -y
  24. "Easy" Unmaps many common-but-confusing commands, keeps Vim inside of INSERT

    mode evim vim -y :source $VIMRUNTIME/evim.vim Return to NORMAL mode with <Ctrl-L>
  25. Multiple Files • Vim can be started with multiple filenames

    • vimdiff requires at least two • Use :prev and :next to navigate back and forth • Alternately, use flags: • -o to open in horizontal splits • -O to open in vertical splits
  26. Multiple Files • Vim can be started with multiple filenames

    • vimdiff requires at least two • Use :prev and :next to navigate back and forth • Alternately, use flags: • -o to open in horizontal splits • -O to open in vertical splits Will probably need to save before navigating
  27. Running Commands Quickly • Use -c to run any Ex

    command before ~/.vimrc loads • Any Ex commands with spaces or shell metacharacters must be properly quoted • Can be used to set contextual variables for ~/.vimrc to behave differently • Can use multiple times on the same command • But only 10 total
  28. Loading Script Files • -S script.vim is an alias for

    -c "source script.vim" • So you can only have 10 total of -S and -c commands together • Leaving the argument off defaults to assuming "session.vim" as the script name
  29. Controlling Scripts • Use -u to load an alternate to

    ~/.vimrc • Where nocompatible comes in handy • Use -u NONE to skip any ~/.vimrc • Useful for doing quick processing
  30. Running Commands Slowly • --cmd runs any Ex command after

    all script files finish loading • Only 10 are allowed, similar to -S and -c • But a different 10 (can use 10 -S and also 10 --cmd) • An alias is +
  31. Some Examples Switch a class name vim -u NONE +"s/OldClass/NewClass"

    +wq script.pl
  32. Some Examples Switch a class name vim -u NONE +"s/OldClass/NewClass"

    +wq script.pl Jump to a specific line in a file vim +128 script.pl
  33. Some Examples Switch a class name vim -u NONE +"s/OldClass/NewClass"

    +wq script.pl Jump to a specific line in a file vim +128 script.pl Remove the __DATA__ section from a script vim -u NONE +"g/^__DATA__/,\$d" +wq script.pl
  34. Some Examples Switch a class name vim -u NONE +"s/OldClass/NewClass"

    +wq script.pl Jump to a specific line in a file vim +128 script.pl Remove the __DATA__ section from a script vim -u NONE +"g/^__DATA__/,\$d" +wq script.pl Copy a list of perl subs after "use" statements vim +'$' +"?^use " +'normal o' \ "g/^sub /co ''" script.pl
  35. Questions? For real this time http://baltimorevim.org/submit-a-talk.html http://dcbpw.org/2016/