Last active
May 24, 2017 11:15
-
-
Save xanthalas/4514119 to your computer and use it in GitHub Desktop.
Vim Tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| VIM Tips: | |
| Vim command to seperate filenames copied from Winmerge onto individual line | |
| --------------------------------------------------------------------------- | |
| \(\..\{3,4}\) | |
| Append String to every line in a selection | |
| ------------------------------------------ | |
| 1. Select using CTRL-V (or CTRL-Q) and then j to select a column. | |
| 2. Press $ to complete block selection with ragged edge | |
| 3. To add to start of each line press I type your text then press ESC | |
| 4. To add to end of each line press A type your text then press ESC | |
| Get a list of all matches for word under cursor | |
| ----------------------------------------------- | |
| Press [I with the cursor on the word you wish to search for | |
| Replace Carriage Returns | |
| 1. replace all extraneous ^M: | |
| :%s/^M$//g or :%s/\r$//g | |
| BE SURE YOU MAKE the ^M USING "CTRL-Q CTRL-M" (CTRL-V CTRL-M in Linux) NOT BY TYPING "CARET M"! | |
| This expression will replace all the ^M's that have carriage returns after them with nothing. | |
| (The dollar ties the search to the end of a line) | |
| 2. replace all ^M's that need to have carriage returns: | |
| :%s/^M/ /g or :%s/\r/ /g | |
| Once again: BE SURE YOU MAKE the ^M USING "CTRL-Q CTRL-M" (CTRL-V CTRL-M in Linux) NOT BY TYPING "CARROT M"! | |
| This expression will replace all the ^M's that didn't have carriage returns after them with a carriage return. | |
| Delete all lines containing a word | |
| ---------------------------------- | |
| Delete all lines containing "profile": | |
| :g/profile/d | |
| to delete all the blank lines in your file. | |
| :g/^$/d | |
| Delete all lines NOT containing a word | |
| ---------------------------------- | |
| Use :v instead of :g | |
| Delete all lines which don't contain "profile": | |
| :v/profile/d | |
| Copy all lines containing pattern to register a | |
| ----------------------------------------------- | |
| :g/<pattern>/y A | |
| Note the uppercase A is important as using lower case a will only yank the first occurance. | |
| Use backreferences in a Search-and-Replace | |
| ------------------------------------------ | |
| To specify the match which will be backreferenced enclose it in \( and \) | |
| To refer to the matching part in the replace use \1 for the first match, \2 for the second, etc... | |
| For example: | |
| :%s/\(\d\d\) 23:59/\1T23:59/g | |
| Will replace all SQL datetime which have no "T" between the date and the time parts with the same version including the "T". | |
| Clear the contents of a register (in this example register a) | |
| -------------------------------- | |
| 0"ay0 | |
| Convert all Tabs within document to spaces | |
| ------------------------------------------ | |
| :retab | |
| Searching for either/or combinations | |
| ------------------------------------ | |
| To highlight occurances of more than one word at the same type use: | |
| /foo\|bar\|xyzzy | |
| This will highlight all three words wherever they appear in the document. | |
| More advanced Searching Regular Expressions | |
| ------------------------------------------- | |
| Another thing useful in searches and substitutions is to omit some information, for instance, suppose we want | |
| to find every 'foo' with 'bar' somewhere else on the line, but we do not want to take the 'bar' part in the | |
| search (let's say not highlight it if the hlsearch is set), we can do: | |
| /foo\(.*bar\)\@= | |
| /foo.*\(bar\)\@= | |
| /foo.\{-}\(bar\)\@= | |
| The first one will highlight only 'foo' in lines containing both 'foo' and 'bar'. The second one will highlight | |
| 'foo' and everything up to the longest match where 'bar' appears on the line. The third one, will do the same | |
| thing, but with the shortest match (non-greedy). So, if there is more than one 'bar' on the same line, the | |
| search will stop right before the first occurrence of 'bar'. | |
| We can also do the opposite. Let's say for example finding all the 'foo' with 'bar' some place else without | |
| highlighting the 'foo', we can do: | |
| /\(foo.*\)\@<=bar | |
| /\(foo\)\@<=.*bar | |
| /\(foo\)\@<=.\{-}bar | |
| The first one will highlight only 'bar' in lines containing both 'foo' and 'bar'. The second one will highlight | |
| everything right after 'foo' up to the last 'bar' on the line. The third one will highlight everything right | |
| after 'foo' and up to the first 'bar' (non-greedy). | |
| For more information, you can type: | |
| :h /\@= | |
| :h /\@! | |
| :h /\@<= | |
| :h /\@<! | |
| :h /\@> | |
| :h /\zs | |
| :h /\ze | |
| :h /\%^ | |
| :h /\%$ | |
| Replace all instances of a string with the contents of a Register | |
| ----------------------------------------------------------------- | |
| :s/fred/<c-r>a/g substitute "fred" with contents of register "a" | |
| VIM Configuration files | |
| ----------------------- | |
| The config file for the GUI version is .gvimrc | |
| The config file for the console version is _vimrc | |
| To see what environment variables are set to | |
| -------------------------------------------- | |
| :echo $<C-D> | |
| To simplify regular expressions and make them closer to PERL & Ruby | |
| Start the regex with \v which switches on "Very Magic" mode. | |
| See :help magic for more info including a table of how characters are treated in the various modes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment