My collection of vi/vim tips, tricks and miscellaneous info.

Table of Questions:

  1. How do I get vim to stop pestering me with syntax and other annoying highlighting?
  2. How do I stop the annoying auto-comment and auto-indenting when I paste into vim?
  3. How do I turn off vim's annoying auto-comment "feature"?
  4. How do I change the EOL file format characters used in vim?
  5. In a windowing environment, how can I cut/copy from vim so that I can paste in other windows?
  6. How do I make vi do the right thing with tabs?
  7. How do I make vim do the right thing with tabs?
  8. What can I put in my .vimrc to make python coding easier?
  9. What are modelines?
  10. What are my favourite modelines?
  11. What does my .vimrc look like?
  12. How do I figure out what a vim command does?
  13. How can I get vim to show line numbers when I print my source code?

Table of Answers:

  1. How do I get vim to stop pestering me with syntax and other annoying highlighting?

    You want to turn off the following highlighting features:
    
    Syntax Highlighting
    Search Highlighting
    Matching Parentheses, Brackets, Etc. Highlighting
    
    You can do the following from within vim:
    
    :set syntax=off
    :set nohlsearch
    :NoMatchParen
    
    Or, if you'd rather do it permanently, you can add the following to
    your .vimrc file:
    
    syntax off
    set nohlsearch
    :let loaded_matchparen = 1
    
          
  2. How do I stop the annoying auto-comment and auto-indenting when I paste into vim?

    From within vim, you can do:
    
    :set noai
    
    Another way to do it is, just before you do the paste, do a:
    
    :set paste
    
    Then immediately afterword do a:
    
    :set nopaste
    
    An alternative method would be to do a:
    
    :r! cat /dev/tty
    [paste here]
    ^D
    
    But, keep in mind that /dev/tty must be your current tty device.
    (In other words, it works on Linux, your mileage may vary. :)
    
          
  3. How do I turn off vim's annoying auto-comment "feature"?

    You can do the following from within vim:
    
    :setlocal formatoptions-=ro
    
    Or, if you'd rather do it permanently, you can add the following to
    your .vimrc file:
    
    autocmd FileType * setlocal formatoptions-=ro
    
          
  4. How do I change the EOL file format characters used in vim?

    From within vim, do the following:
    
    :set fileformat=XYZ
    
    Where XYZ is one of "unix", "dos", "mac".
    Unix EOL = LF (linefeed, newline, \n)
    DOS  EOL = CRLF (carriage-return+linefeed, return+newline, \r\n)
    Mac  EOL = CR (carriage-return, return, \r)
    
          
  5. In a windowing environment, how can I cut/copy from vim so that I can paste in other windows?

    Of course, you can use the mouse.  But if you're a touch-typist who prefers
    using the keyboard like I do, you can still do it.
    
    In command mode, an uppercase V will start the visual highlighting mode, which
    allows you to highlight one or more lines, using command mode movement keys.
    Once highlighted, you can yank them into the clipboard using:
    
    "+y
     
          
  6. How do I make vi do the right thing with tabs?

    The short answer is: You can't.
    
    This excerpt from O'Reilly's "Learning the vi Editor" points out the grim
    reality of tab stops in vi:
    
    vi attempts to be smart when doing indenting. Usually, when you see text
    indented by eight spaces at a time, vi will actually insert tab characters into
    the file, since tabs usually expand to eight spaces. This is the UNIX default;
    it is most noticable when you type a tab during normal input, and when files
    are sent to a printer -- UNIX expands them with a tab stop of eight spaces.
    
    If you wish, you can change how vi represents tabs on your screen, by changing
    the tabstop option. For example, if you have something that is deeply indented,
    you might wish to have use a tab stop setting of every four characters, so that
    the lines will not wrap. The following command will make this change:
    
        :set tabstop=4
    
    NOTE: Changing your tab stops is not recommended. Although vi will display
    the file using an arbitrary tabstop setting, the tab characters in your files
    will still be expanded using an eight-character tab stop by every other UNIX
    program. Eight-character tab stops are one of the facts of life on UNIX, and
    you should just get used to them.
          
  7. How do I make vim do the right thing with tabs?

    From the vim doc:
    
    			*'tabstop'* *'ts'*
    'tabstop' 'ts'		number	(default 8)
    			local to buffer
    	Number of spaces that a <Tab> in the file counts for.  Also see
    	|:retab| command, and 'softtabstop' option.
    
    	Note: Setting 'tabstop' to any other value than 8 can make your file
    	appear wrong in many places (e.g., when printing it).
    
    	There are four main ways to use tabs in Vim:
    	1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
    	   (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
    	   will use a mix of tabs and spaces, but typing Tab and BS will
    	   behave like a tab appears every 4 (or 3) characters.
    	2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
    	   'expandtab'.  This way you will always insert spaces.  The
    	   formatting will never be messed up when 'tabstop' is changed.
    	3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
    	   |modeline| to set these values when editing the file again.	Only
    	   works when using Vim to edit the file.
    	4. Always set 'tabstop' and 'shiftwidth' to the same value, and
    	   'noexpandtab'.  This should then work (for initial indents only)
    	   for any tabstop setting that people use.  It might be nice to have
    	   tabs after the first non-blank inserted as spaces if you do this
    	   though.  Otherwise aligned comments will be wrong when 'tabstop' is
    	   changed.
    
    			*'expandtab'* *'et'* *'noexpandtab'* *'noet'*
    'expandtab' 'et'	boolean	(default off)
    			local to buffer
    			{not in Vi}
    	In Insert mode: Use the appropriate number of spaces to insert a
    	<Tab>.	Spaces are used in indents with the '>' and '<' commands and
    	when 'autoindent' is on.  To insert a real tab when 'expandtab' is
    	on, use CTRL-V<Tab>.  See also |:retab| and |ins-expandtab|.
    	NOTE: This option is reset when 'compatible' is set.
    
    			*'shiftwidth'* *'sw'*
    'shiftwidth' 'sw'	number	(default 8)
    			local to buffer
    	Number of spaces to use for each step of (auto)indent.	Used for
    	|'cindent'|, |>>|, |<<|, etc.
    
    			*'smarttab'* *'sta'* *'nosmarttab'* *'nosta'*
    'smarttab' 'sta'	boolean	(default off)
    			global
    			{not in Vi}
    	When on, a <Tab> in front of a line inserts blanks according to
    	'shiftwidth'.  'tabstop' is used in other places.  A <BS> will delete
    	a 'shiftwidth' worth of space at the start of the line.
    	When off a <Tab> always inserts blanks according to 'tabstop'.
    	'shiftwidth' is only used for shifting text left or right
    	|shift-left-right|.
    	What gets inserted (a Tab or spaces) depends on the 'expandtab'
    	option.  Also see |ins-expandtab|.  When 'expandtab' is not set, the
    	number of spaces minimized by using <Tab>s.
    	NOTE: This option is reset when 'compatible' is set.
    
    			*'softtabstop'* *'sts'*
    'softtabstop' 'sts'	number	(default 0)
    			local to buffer
    			{not in Vi}
    	Number of spaces that a <Tab> counts for while performing editing
    	operations, like inserting a <Tab> or using <BS>.  It "feels" like
    	<Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
    	used.  This is useful to keep the 'ts' setting at its standard value
    	of 8, while being able to edit like it is set to 'sts'.  However,
    	commands like "x" still work on the actual characters.
    	When 'sts' is zero, this feature is off.
    	'softtabstop' is set to 0 when the 'paste' option is set.
    	See also |ins-expandtab|.  When 'expandtab' is not set, the number of
    	spaces is minimized by using <Tab>s.
    	The 'L' flag in 'cpoptions' changes how tabs are used when 'list' is
    	set.
    	NOTE: This option is set to 0 when 'compatible' is set.
    
    My NOTE: When you set expandtab, all tabs entered will be converted to the
    number of spaces tabstop is set to.  However any tabs entered prior to setting
    expandtab will be left as tab characters unless you use the :retab command.
    Also, to enter a true tab character, you will have to use ^V (Ctrl-V).
    
          
  8. What can I put in my .vimrc to make python coding easier?

    autocmd FileType * set tabstop=2|set shiftwidth=2|set noexpandtab
    autocmd FileType python set tabstop=4|set shiftwidth=4|set expandtab
    set softtabstop=4   " makes the spaces feel like real tabs
    
    OR
    
    autocmd BufEnter *.py set ai tw=79 sw=4 ts=4 sta et fo=croql 
    
    Meaning:
      ai       = AutoIndent
      tw=79    = Set textwidth=79 (Auto-wrap lines greater than textwidth)
      sw=4     = Set shiftwidth=4 (Number of spaces a shift will use)
      ts=4     = Set tabstop=4 (Number of spaces a Tab uses)
      sta      = Does ":tag[!] [tagname]" and splits the window for the found tag
      et       = Set expandtab (Expands Tabs to spaces - see ts=4 above)
      fo=croql = Set formatoptions to croql
                   c = Auto-wrap comments with textwidth, auto-insert comment char
                   r = Auto-insert comment char after <Enter> in Insert mode
                   o = Auto-insert comment char after Opening in Normal mode
                   q = Allow formatting comments with "gq"
                   l = Long lines aren't broken in Insert mode
    
    NOTE: Alternately, you can use the following modeline in your Python code files:
    
    # vim: set ai tw=79 sw=4 ts=4 sta et fo=croql:
    
          
  9. What are modelines?

    Modelines are a potentially dangerous feature that allows vi/vim settings to be
    listed inside a text file by putting them within the first or last five lines
    of the text.
    
    From the vim doc:
    
    			*modeline* *vim:* *vi:* *ex:* *E520*
    There are two forms of modelines.  The first form:
    	[text]{white}{vi:|vim:|ex:}[white]{options}
    
    [text]		any text or empty
    {white}		at least one blank character (<Space> or <Tab>)
    {vi:|vim:|ex:}	the string "vi:", "vim:" or "ex:"
    [white]		optional white space
    {options}	a list of option settings, separated with white space or ':',
    		where each part between ':' is the argument for a ":set"
    		command
    
    Example:  
       vi:noai:sw=3 ts=6
    
    The second form (this is compatible with some versions of Vi):
    
    	[text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]
    
    [text]		any text or empty
    {white}		at least one blank character (<Space> or <Tab>)
    {vi:|vim:|ex:}	the string "vi:", "vim:" or "ex:"
    [white]		optional white space
    se[t]		the string "set " or "se " (note the space)
    {options}	a list of options, separated with white space, which is the
    		argument for a ":set" command
    :		a colon
    [text]		any text or empty
    
    Example:  
       /* vim: set ai tw=75: */
    
    The white space before {vi:|vim:|ex:} is required.  This minimizes the chance
    that a normal word like "lex:" is caught.  There is one exception: "vi:" and
    "vim:" can also be at the start of the line (for compatibility with version
    3.0).  Using "ex:" at the start of the line will be ignored (this could be
    short for "example:").
    
    My NOTE: Bear in mind that to enable modeline support in vim, you must use 'set
    modeline' (or ml for short) in your vimrc or from within a vim session.
    
    Other Examples:
    
    /* 
     * vi:set sw=4 ai:
     * vi:set showmatch: 
     */
    
    ---
    
    # vim: set expandtab:
    
    ---
    
    // vim: noai:ts=4:sw=4
       -or-
    /* vim: noai:ts=4:sw=4
    */
       -or-
    /* vim: set noai ts=4 sw=4: */
       -or-
    /* vim: set fdm=expr fde=getline(v\:lnum)=~'{'?'>1'\:'1': */
    
    ---
    
    /* vim: set ts=8 sw=4 tw=0: */
    
    ---
    
        /* vim: set filetype=c shiftwidth=4 expandtab textwidth=80 : */
    
    OR
    
        /* vim: set ft=c sw=4 et tw=80 : */
    
          
  10. What are my favourite modelines?

    For vim:
    
    /* vim: set noai nosm tw=76: */
    
    Meaning:
      noai   = No AutoIndent
      nosm   = No ShowMatch, don't show matching ([{
      tw=76  = Set text width to 76 columns
    
          
  11. What does my .vimrc look like?

    ".vimrc (or /etc/vim/vimrc.local, etc.)
    
    "JRWZ: I *use* modelines!
    set modeline
    set modelines=5
    
    "JRWZ: autoindent sucks!
    set noai
    
    "JRWZ: so does highlight searching!
    set nohlsearch
    
    "JRWZ: and likewise with parentheses matching!
    :let loaded_matchparen = 1
    
    "JRWZ: I live in the Americas, so I prefer US Letter paper size.
    "JRWZ: I also prefer no duplex printing
    "JRWZ: And I prefer and no line numbers on printouts
    set printoptions=paper:letter,duplex:off,number:n
    
    "JRWZ: Turn off the annoying auto-comment "feature"!
    if has("autocmd") 
      autocmd FileType * setlocal formatoptions-=ro
    endif
    
          
  12. How do I figure out what a vim command does?

    :help holy-grail
    
          
  13. How can I get vim to show line numbers when I print my source code?

    From within a vim session...
    
    First, check the contents of the 'printoptions' variable:
    
    :set printoptions
    
    If there is no 'number:' entry, add it:
    
    :set printoptions+=number:y