CLI options
This chapter discusses some of the options you can use when starting Vim from the command line. A Unix/Linux distribution is assumed for the examples shown in this chapter. Syntax and features might vary for other platforms like Windows.
Documentation links:
- :h vim-arguments — reference manual for Vim arguments
Recall that you need to add
-prefix for built-in help on CLI options, :h -y for example.
Default
gvimopens a new unnamed buffer when a filename is not specifiedgvim script.pyopensscript.py- creates a blank buffer if
script.pydoesn't exist, file will be created only after you explicitly issue write commands
- creates a blank buffer if
gvim report.log power.log area.logopens the specified files- first file (
report.loghere) will be the current buffer
- first file (
gvim -- *.txtif filenames can start with-, use--to prevent such files from being treated as an option
Help
gvim -hbrief description of the options- not all options are discussed in this chapter, so you can use this to view the full list
Tabs and Splits
gvim -p *.logopens the specified files as separate tab pages- by default, you can open a maximum of
10pages, use thetabpagemaxsetting if you want to change this number
- by default, you can open a maximum of
gvim -o *.logopens the specified files as horizontal splitsgvim -O *.logopens the specified files as vertical splits
You can append a number to each of these options to specify how many tabs or splits you want. For example,
gvim -p3 *.pyopens three tabs irrespective of the number of input files. Empty buffers will be used if there aren't enough input files to satisfy the given number.
Easy mode
gvim -yopens in Insert mode and behaves like a click-and-type editor- useful for those who just want a simple text editor
- or, perhaps you can prank Vim users by setting
alias vim='vim -y' - use Ctrl+l or Ctrl+o if you want to use Normal mode commands
See also novim-mode plugin, which aims to make Vim behave more like a normal editor.
Readonly and Restricted modes
gvim -RReadonly mode- changes can still be made and saved despite warning messages
- for example, by using :w!
gvim -Mstricter Readonly mode- changes cannot be made unless :set modifiable is used
- file cannot be saved until :set write is used
gvim -ZRestricted mode- commands using external shell are not allowed
- for example, you won't be able to use :!ls
Cursor position
gvim + script.pyopensscript.pyand the cursor is placed on the last linegvim +25 script.pyopensscript.pyand the cursor is placed on the 25th line- if the number goes beyond the available lines in the file, the cursor will be placed on the last line
gvim +/while script.pyopensscript.pyand the cursor is placed on the first line containing the given pattern- if the pattern is not found, the cursor will be placed on the last line
- use
gvim +1 +/patternto force the search to start from the first line, otherwise cursor position stored inviminfowill be used (if applicable)
Execute command
gvim -callows you to execute the Command-line mode command passed as an argumentgvim -c '%s/search/replace/g' script.pyopensscript.pyand performs the given substitute operationgvim -c 'normal =G' script.pyopensscript.pyand auto indents the entire file content
As per :h -c, "You can use up to 10
+or-carguments in a Vim command. They are executed in the order given. A-Sargument counts as a-cargument as well"
![]()
--cmdoption is similar to the-coption, but executes the command before loading anyvimrcfiles.
Quickfix
gvim -q <(grep -Hn 'search' *.py)interactively edit the matching lines fromgrepoutput-Hand-noptions provide filename and line number prefix for the matching lines- use :cn and :cp to navigate to the next and previous occurrences respectively
- Command-line area at the bottom will show the number of matches and filenames
- you can also use
gvim -q fileif you had saved thegrepoutput to that file
gvim -q error.logedit source code based on compiler output containing filenames and line numbers for the error locations- here, the
error.logis assumed to be the filename used to save the error messages
- here, the
See Vim and the quickfix list and stackoverflow: How do you use Vim's quickfix feature? to learn more about this feature.
See :h quickfix for documentation.
Vimrc and Plugins
gvim -u fileuses the given file for initialization instead ofvimrcfiles- useful to test plugins, apply a different
vimrcbased on which project you are working on, etc
- useful to test plugins, apply a different
gvim -u NONEall initializations are skippedgvim -u DEFAULTSsimilar toNONE, butdefaults.vimis loadedgvim -u NORCsimilar toNONE, but plugins are loadedgvim --nopluginonly plugins are not loaded
Here's a neat table from :h --noplugin:
| argument | vimrc | plugins | defaults.vim |
|---|---|---|---|
| (nothing) | yes | yes | yes |
-u NONE | no | no | no |
-u DEFAULTS | no | no | yes |
-u NORC | no | yes | no |
--noplugin | yes | no | yes |
Session and Viminfo
gvim -S proj.vimrestore a session using the previously saved session file- see :h Session for more details
gvim -i proj.viminforestore Viminfo from the given file- this file will also be used instead of the default
viminfofile to save information - see :h viminfo-read-write for more details
- this file will also be used instead of the default