Differences

This shows you the differences between two versions of the page.

Link to this comparison view

vim [2012/08/06 13:51]
vim [2012/08/06 13:51] (current)
Line 1: Line 1:
 +====== vim ======
  
 +  * [[Vim Tips and Tricks]]
 +
 +Vim is a console text editor designed to make you feel like a noob.
 +
 +=== Modes ===
 +
 +Vim has two modes, which I'll call navigation and editing mode.  In navigation mode, you are not making any direct changes to the text by typing. ​ You can still perform actions such as copying, pasting, moving, deleting, etc. along with file operations such as opening and saving files.
 +
 +Editing mode is where anything you type is recorded.
 +
 +To enter '​edit'​ mode, you would need to tell vim to enter it.  ''​i''​ is the simplest keypress, meaning "​insert here.""​
 +
 +=== Keys ===
 +
 +A lot of keys in vim will do similar events but slightly different based on case.  ''​i''​ means insert here (current position). ​ ''​I''​ means insert above this line and start the cursor there. ​ ''​p''​ means paste here while ''​P''​ would paste above the current line.
 +
 +You can also prefix any commands with a number to do it $x amount of times. ​ So use ''​y''​ to yank a letter, or 5y to yank the next 5 lines.
 +
 +Some handy keys:
 +
 +^ Keypress ^ Action ^
 +| $ | Move to end of line |
 +| %%^%% | Move to start of line |
 +| w | next word |
 +| b | previous word |
 +| i | insert here |
 +| I | insert above this line |
 +| o | insert line and edit here |
 +| O | insert line above and edit there |
 +| p | paste here |
 +| P | paste above current line |
 +| / | find string |
 +| n | next instance |
 +| N | previous instance |
 +| :​i<​int>​| Go to line <int> (:1 goes to first line) |
 +| :<​up/​down arrow> | browse history |
 +| H | first line of the screen |
 +| L | last line of the screen |
 +| M | middle line of the screen |
 +| a | append to the end of the cursor |
 +| A | append to the end of the current line |
 +| r<​char>​ | replace cursor selection with <​char>​ |
 +| d | delete current selection |
 +| dd | delete current line |
 +| x | delete current character |
 +| v | highlight characters |
 +| V | highlight lines |
 +| u | undo last change |
 +| Ctl + R | undo last undo |
 +| y | copy character or selection to clipboard |
 +| yy | copy line to clipboard |
 +| # | highlight and search for current word |
 +
 +See also http://​www.tuxfiles.org/​linuxhelp/​vimcheat.html
 +
 +=== Keypress Combinations ===
 +
 +^ Combo ^ Fatality ^
 +| %%d^%% | delete to start of line |
 +| %%d$%% | delete to end of line | 
 +| %%dH%% | delete to first line of screen |
 +| %%dL%% | delete to last line of screen |
 +| %%15dd%% | delete 15 lines |
 +| %%yy5p%% | copy current line and paste it 5 times under current line |
 +| 3u | undo last 3 changes |
 +| 12b | go back 12 words |
 +| 2w | go forward 2 words |
 +
 +=== Rage Quit ===
 +
 +You can append ''​!''​ to any command to force it to happen.
 +
 +For example, if opening a read-only file, use :w! to save it and force writing to it.
 +
 +Or, use '':​q!''​ to force quit, ignoring any changes you may have made.
 +
 +You can also use '':​e!''​ to reopen the file and ignore any changes, essentially starting over.
 +
 +=== File Navigation ===
 +
 +When accessing the filesystem, you can use tab-completion with any of these commands when looking for a file.  For example: '':​e class.<​tab>''​ would cycle through all files starting with ''​class.''​
 +
 +^ Command ^ Action ^
 +| :e <​filename>​ | Open <​filename>​ |
 +| :e | reload file (fex: in case changed on filesystem) |
 +| :E | Open file navigation |
 +| :w | save file |
 +| :q | quit vim |
 +| :x | save and quit |
 +| :sav <​filename>​ | save current buffer as <​filename>​ |
 +
 +=== Multiple Screens ===
 +
 +You can use vim in split mode to edit multiple files at once.  Use :sp to split the screen horizontally,​ or :vsp to split the screen vertically.
 +
 +You can also open a file at the same time with :sp <​filename>​
 +
 +To navigate between panes, exit edit mode and use Ctl + W then an arrow key to move in the direction you'd like to go (up or down, left or right).
 +
 +To close a current split screen, you can use '':​clo''​
 +
 +=== Indentation ===
 +
 +After selecting some lines using ''​V'',​ use the ''>''​ and ''<''​ keys to increase or decrease indentation level. ​ Note that the cursor selection will reset after doing this once.
 +
 +=== Background Colors ===
 +
 +Vim supports color schemes. ​ Search the Internet to find some.
 +
 +You can use '':​set bg=light''​ or '':​set bg=dark''​ to change the current color scheme to match your terminal background color (black or white).
 +
 +=== Search ===
 +
 +Use ''/''​ to search the buffer. ​ Append the string with your search terms. ​ You will need to escape characters like spaces. ''/​the\ time''​
 +
 +=== Helpful Settings ===
 +
 +^ Command ^ Effect ^
 +| :set ruler | Displays line row and column number |
 +| :set laststatus=2 | Nice little bar that says what file you are editing |
 +| :set ai | Turn on auto-indenting |
 +
 +You can put all commands you want to startup on default in ~/.vimrc in the same format you would send a command when editing in vim.
 +
 +=== Other Cool Stuff ===
 +
 +Vim will save anything you put into the clipboard between sessions. ​ Highlight something in one file, exit vim.  Come back later, and paste will still work.
 +
 +You can also get syntax files for vim so that editing files in certain languages or formats (bash, ruby, php, c++, xml, etc.) will highlight everything in the file for you.