Skip to content

Instantly share code, notes, and snippets.

@mcrmfc
Last active September 25, 2015 06:38
Show Gist options
  • Save mcrmfc/879362 to your computer and use it in GitHub Desktop.
Save mcrmfc/879362 to your computer and use it in GitHub Desktop.
VIM - My cheat sheet
search and replace
-------------------
s/foo/bar/g changes each 'foo' to 'bar' in the current line.
:%s/foo/bar/g changes each 'foo' to 'bar' in all lines.
:5,12s/foo/bar/g changes each 'foo' to 'bar' for all lines between line 5 and line 12.
:'a,'bs/foo/bar/g changes each 'foo' to 'bar' for all lines between marks a and b.
:.,$s/foo/bar/g changes each 'foo' to 'bar' for all lines between the current line (.) and the last line ($).
:.,+2s/foo/bar/g changes each 'foo' to 'bar' for the current line (.) and the two next lines (+2).
:%s is equivalent to :1,$s
:%s/ /\r&/g changes all spaces to line breaks
:%s/\r\+$//e remove windows line endings
:%s/\r//g remove f*ing windows line endings
search & replace multiple files (using arglist)
----------------------------------
vim *.xml
:arg - lists contents or arglist
:argdo %s/test_xml/acceptance\/test_xml/ge | update
search & replace + dlete lines
--------------------
:g/Delete Me/d
g says global
Delete Me is the pattern to delete
d says delete the line
:g!/Delete Me/d
delete all lines that don't include Delete Me
delete trailing whitespace
%s/\s\+$//g
delete leading whitespace
%s/^\s\s*//g
copy lines not starting with http to a buffer
:g!/http.*/y A
buffers
-------
:vert sbN - where N is the buffer number
:sp - normal horizontal split
:vsp - normal vertical split
:ctrl+w + - increase split size by one line
vimgrep
-------
:vimgrep /texttofind/gj ./**/*.feature - This says search for all occurrences of 'texttofind' and recurse through all child directories of current looking in files with .feature extension
so...
:vimgrep /texttofind/gj ./cucumber/features/*.feature - says search for all occurrences of 'texttofind' in files with .feature extension in the features directory
then when grep has finished do :cw to see the results
Note about 'gj'...'g' says return matches for every line that matches the pattern (i.e. not just the first match) and 'j' inhibits VIMs jumpiness and saves results to the quickfix list.
printing
--------
:hardcopy - generated postscript (also cmd+p)
:set printoptions=portrait:n,number:y - would set postscript to be generated as landscape and print line numbers
diff
--------
svn diff -c 647638 https://repo.dev.bbc.co.uk/ | vim -R -
whitespace
----------
" Show all tabs:
/\t
" Show trailing whitespace:
/\s\+$
" Show spaces before a tab:
/ \+\ze\t
autocomplete (or omnicomplete)
--------------------------------
In edit mode:
crtl+x, crtl+o
tags
------
create tags file...
ctags -R .
need to tell vim where to look for tags files....
set tags=./tags;
If using taglist plugin need to tell it where ctags lives if not /usr/bin or standard location...
let Tlist_Ctags_Cmd = '/usr/local/Cellar/ctags/5.8/bin/ctags'
to update tags file with new methods....
ctags -a
but this does not remove old methods....for this use AutoTag.vim...needs python support, not it has limits on tag file size so if you Bundle locally then this may be why you don't see your tag file being auto updated
Debugging Vim Scripts
---------------------
echom - this will debug so that :messages brings up logging
if you need to debug from python in vimscript - vim.command("echo 'hello from python'")
debug in python with variable interpolation - vim.command('echo "exception %s"' % (file))
@pete-otaqui
Copy link

You should put this in a dotfiles repo :)

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