Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JadedEvan
Created September 22, 2011 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadedEvan/1235573 to your computer and use it in GitHub Desktop.
Save JadedEvan/1235573 to your computer and use it in GitHub Desktop.
Vim Tips - a catalog of useful Vim commands

Buffer Settings

:set scrolloffset=30
:set scrolloffset=999
:set so=0

Sets the offset when scrolling through a document. A value of 30 will keep a marginal amount of space while scrolling, a value of 999 will keep the cursor centered in the screen, a value of 0 restores Vim's default behavior. All of the above can be added to your ~/.vimrc file to set editor defaults. Vim wikia - scroll centering

Additional buffer settings

  • :set list - show invisible characters
  • :set nolist - hide invisible characters

Functions

:let i=1 | %g/fixture_\d\=$/s/fixture_\d\=$/\=i/ |let i=i+1

Matches a pattern throughout the document, increments the number.

File Manipulation

:norm jdd

Delete every other line

12,21s/^\(\<\w\+\>\).*$/\1/g

Delete all text in the line after matching first word in lines 12-21.

File Conversion

:update
:e ++ff=dos 	
:setlocal ff=unix
:w

Useful command for removing the ^M characters that come from Windows/DOS file encodings. #1 saves the file, #2 edits file again, using dos file format ('fileformats' is ignored). #3 - buffer will use LF-only line endings when written. #4 write buffer using unix (LF-only) line endings.

Alternately, to remove ^M characters:

:%s/<ctrl-v><enter>/\r/g

Substitutions and Files

:arg some/path/**/*.mdown
:argdo %s/some_expression_here/some_replacement_here/g | update

Searches recursively sub directories in some/path for files named *.markdown, then performs the substitution command provided in the second line. You can add the /gc flag if you want to confirm your substitutions. Will perform operation on all matching files.

$ vim "+%s/foo/bar/g" "+wq" foo.txt

Command line invocation of vim. Any argument passed in with a plus in front of the arguments will be run in Vim's ex mode. The command above will replace all occurances of "foo" with "bar" and write the changes. Note that any valid Vim ex mode command can be run this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment