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.

Stephen Belcher

March 14, 2016
Tweet

More Decks by Stephen Belcher

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. "Read-Only" Sets readonly option, meaning you have to execute :set

    noreadonly or write with :w! to save view vim -R :set readonly
  6. "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
  7. "Ex" Behave like the ex line editor of old; few

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

    visual components, edit only with COMMAND-mode ex vim -e Q
  9. "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
  10. "Improved Ex" Like "Ex", but add various Vim improvement goodies

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

    like tab-completion of COMMAND-mode commands exim vim -E gQ
  12. "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
  13. "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>
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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 +
  20. 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
  21. 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
  22. 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