<p>What are the essential vim commands?  What does a new-user need to know to keep themselves from getting into trouble?  One command per comment, please.</p>

[{'answer_id': 69887, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>First of all you need to know how to close vi:\nctrl-c : q!</p>\n\n<p>Rest can be found from vimtutor. Launch vimtutor by typing vimtutor at your command line</p>\n'}, {'answer_id': 69888, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>One of my favourite commands is %G which takes to directly to the end of a file. Especially useful in log-files.</p>\n'}, {'answer_id': 69895, 'author': 'DJ Capelis', 'author_id': 10943, 'author_profile': 'https://Stackoverflow.com/users/10943', 'pm_score': 0, 'selected': False, 'text': '<p>How to switch between modes (i to enter insert mode (one of many ways), esc to exit insert mode, colon for command mode) and how to save and exit. (:wq)</p>\n'}, {'answer_id': 69896, 'author': 'perimosocordiae', 'author_id': 10601, 'author_profile': 'https://Stackoverflow.com/users/10601', 'pm_score': 1, 'selected': False, 'text': '<p>I use vi very lightly, and I only use the following commands:</p>\n\n<pre><code>a   - switch to insert mode (after the cursor)\nesc - return to command mode\n:wq - save and quit\n:q  - quit (no save, only without modification)\n:q! - force quit (no save, also with modification)\nx   - delete one character (in command mode)\ndd  - delete the whole line (in command mode)\n</code></pre>\n\n<p>I know there are many many more, but those are enough to get you by.</p>\n'}, {'answer_id': 69897, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Another useful command is to search something: /\ne.g. /Mon will search (and in case of vim highlight) any occurences of Mon in your file.</p>\n'}, {'answer_id': 69916, 'author': 'kementeus', 'author_id': 11273, 'author_profile': 'https://Stackoverflow.com/users/11273', 'pm_score': 2, 'selected': False, 'text': '<pre><code>:q -&gt; quit\n:w -&gt; save\n:q! -&gt; quit and don\'t save\n:x -&gt; save and quit\n:[number] -&gt; go to line number\nG -&gt; go to end of file\ndd -&gt; delete line\np -&gt; "put" line\nyy -&gt; "copy" line\n:s/[searchfor] -&gt; search\n</code></pre>\n\n<p>I guess those are the basic one to start from</p>\n'}, {'answer_id': 69954, 'author': 'Paul Wicks', 'author_id': 85, 'author_profile': 'https://Stackoverflow.com/users/85', 'pm_score': 0, 'selected': False, 'text': '<p>As a couple of other people have already mentioned, vimtutor is the way to go. It will teach you everything you need to know in vim. The one piece of general advice I would give you is to stay out of insert mode as much as possible. There is enormous power in the other modes, it just takes a little bit of practice to get used to it.</p>\n'}, {'answer_id': 70007, 'author': 'Agnel Kurian', 'author_id': 45603, 'author_profile': 'https://Stackoverflow.com/users/45603', 'pm_score': 2, 'selected': False, 'text': "<p>Use the 'J' (J for Join; upper-case) command to delete the newline at the end of a line. You'll find it tricky otherwise.</p>\n"}, {'answer_id': 70307, 'author': 'Sarien', 'author_id': 1994377, 'author_profile': 'https://Stackoverflow.com/users/1994377', 'pm_score': 3, 'selected': False, 'text': '<p>When you have some repetitive action to take Macros are usually faster than regex.\nJust type</p>\n\n<pre><code>q[0-9a-z] in normal mode\n</code></pre>\n\n<p>Many people use </p>\n\n<pre><code>qq\n</code></pre>\n\n<p>because it\'s fast.\nPress\n    q in normal mode\nagain to stop recording.\nThen just type</p>\n\n<pre><code>@[0-9a-z] in normal mode\n</code></pre>\n\n<p>to repeat what you just recorded.</p>\n\n<pre><code>@q\n</code></pre>\n\n<p>for the example like above.</p>\n\n<p>Edited to add: you can also repeat the macro. Let\'s say you programed a macro to jump to the head of a line, insert a tab, and then jump down one line. You then test your macro by typing "@q" to run it once. Then you can repeat the action nine more times by typing "9@q". </p>\n'}, {'answer_id': 70325, 'author': 'jbdavid', 'author_id': 6314, 'author_profile': 'https://Stackoverflow.com/users/6314', 'pm_score': 1, 'selected': False, 'text': "<p>alias vi nedit    :)</p>\n\n<p>all humor aside.. \nfor vi WHEN NOT using nedit.. </p>\n\n<ul>\n<li><code>i</code> (switch to insert mode) </li>\n<li><code>a</code> (append = move to end of line and switch to insert mode) </li>\n<li><code>esc</code> (exit insert mode)</li>\n<li><code>dd</code> delete a line </li>\n<li><code>x</code> delete a character </li>\n<li><code>:wq</code> (save and quit) </li>\n<li><code>/</code> start a search  </li>\n<li><code>n</code> find Next </li>\n<li><code>?</code> search  backwards..</li>\n<li><code>yy</code> (yank) copy a line to the buffer</li>\n<li><code>pp</code> (paste) paste it here</li>\n<li><code>r</code> (replace a character)</li>\n<li><code>&lt;N&gt; &lt;command&gt;</code> this is a neat - but aggravating feature that lets you type digits and then a command so</li>\n<li><code>5dd</code> will delete 5 lines </li>\n</ul>\n\n<p>but at this point you might as well\n  - <code>man vi</code> and refresh your memory </p>\n\n<p>While there are LOTS more, I switched from Vi to nedit several years ago, which I find has more features I can use on a regular basis more easily. Tabbed editing, incremental search bar, column select, copy and paste. sort selected lines, search and destroy within selection, whole doc or all open docs.. \ntear-off drop down menus.. \nand it supports syntax highlighting for all the languages I use.. (with pattern files I've used a long time over the years. VIM many now be equivalent, but It has to introduce a feature that Nedit doesn't and an easy way to migrate my pattern files before I switch again. </p>\n"}, {'answer_id': 70333, 'author': 'epatel', 'author_id': 842, 'author_profile': 'https://Stackoverflow.com/users/842', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s also good to run the <a href="http://www.linuxcommand.org/man_pages/vimtutor1.html" rel="nofollow noreferrer">vimtutor</a> when learning these commands</p>\n'}, {'answer_id': 70340, 'author': 'jonfm', 'author_id': 6924, 'author_profile': 'https://Stackoverflow.com/users/6924', 'pm_score': 0, 'selected': False, 'text': "<p>i - insert mode (escape to exit)\ndd - delete line\nshift-y - 'Yank' (copy) line\np - 'Put' (paste) line(s)\nshift-v - Visual mode used to select text (tryin 'yanking' this text and 'putting' it somewhere.\nctrl-w n - create new window (you can open a file or start new file here)\nctrl-w v - split existing window vertically\nctrl-n (in insert mode) - autocomplete (if supported)\n:! to run a shell command, usually with standard in as the file or a selection (shift-V)</p>\n\n<p>Useful plugins to look at:\n* Buffer Explorer - use \\be to view files in the buffer (and select to re-open)</p>\n"}, {'answer_id': 70354, 'author': 'metao', 'author_id': 11484, 'author_profile': 'https://Stackoverflow.com/users/11484', 'pm_score': 3, 'selected': False, 'text': '<p>I like this QRC!</p>\n\n<p><a href="http://www.fsckin.com/wp-content/uploads/2007/10/vi-vim_cheat_sheet.gif" rel="nofollow noreferrer">http://www.fsckin.com/wp-content/uploads/2007/10/vi-vim_cheat_sheet.gif</a></p>\n'}, {'answer_id': 70364, 'author': 'Ian', 'author_id': 4396, 'author_profile': 'https://Stackoverflow.com/users/4396', 'pm_score': 1, 'selected': False, 'text': '<p>My biggest tip: <strong><kbd>ctrl</kbd>+<kbd>q</kbd></strong> saves the day when you accidentally hit <kbd>ctrl</kbd>+<kbd>s</kbd> to save the file you are working on</p>\n'}, {'answer_id': 70384, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>NB vi is not vim! vim is rapidly turning into the emacs of the new century. nvi is probably the closest thing to the original vi. Here\'s a nice hint: "xp" will exchange two characters (try it).</p>\n'}, {'answer_id': 70395, 'author': 'gnobal', 'author_id': 7748, 'author_profile': 'https://Stackoverflow.com/users/7748', 'pm_score': 2, 'selected': False, 'text': '<p>This <a href="http://www.ibm.com/developerworks/aix/library/au-speakingunix_vim/index.html" rel="nofollow noreferrer">recent Vim tutorial from IBM</a> is pretty good</p>\n'}, {'answer_id': 70424, 'author': 'nikolay', 'author_id': 11505, 'author_profile': 'https://Stackoverflow.com/users/11505', 'pm_score': 0, 'selected': False, 'text': "<p>replace 'foo' with 'bar' everywhere in the file\n:%s/foo/bar/gc</p>\n"}, {'answer_id': 70438, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 4, 'selected': False, 'text': '<p>What I find irreplaceable (because it works in vi also, unlike vim\'s visual mode) are marks. You can mark various spots with <code>m</code> (lower case) and then a letter of your choice (eg x). Then you go elsewhere, and can go back with ``x<code>(backquote letter) to the exact spot, or with</code>\'x` (apostrophe letter) to go to the line.</p>\n\n<p>These movements can be used as arguments to commands (yank, delete, etc). For example, you want to delete 10 lines; instead of counting and then moving to the topmost line and entering <code>10dd</code>, you go to either the start or the end of the block, press <code>mm</code> (mark m), then go to the other end of the block, and press <code>d\'m</code> (delete apostrophe m). If you use backquote instead of apostrophe in this example, then the deletion will work character-wise, not line-wise. Try marking in the middle of the line with "mark m", moving to the middle of another line, then entering "d backquote m" and you will see what I mean.</p>\n'}, {'answer_id': 70479, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<pre><code>" ~/.vimrc\n" Turn on line numbering\nset nu\n" Turn on syntax highlighting\nsyntax on    \n" Set 4 space expanding tabs\nset tabstop=4\nset shiftwidth=4\nset softtabstop=4\nset expandtab\n"turn off line wrapping\nset nowrap\n" Map CTRL-N to create a new tab\n:map &lt;C-n&gt; &lt;ESC&gt;:tabnew&lt;RETURN&gt;\n" Map Tab and CTRL-Tab to move between tabs\n:map &lt;Tab&gt; &lt;ESC&gt;:tabn&lt;RETURN&gt;\n:map &lt;C-Tab&gt; &lt;ESC&gt;:tabp&lt;RETURN&gt;\n</code></pre>\n'}, {'answer_id': 71851, 'author': 'Dominic Dos Santos', 'author_id': 5379, 'author_profile': 'https://Stackoverflow.com/users/5379', 'pm_score': 3, 'selected': False, 'text': '<p>I was very happy the day I learned about using * or # to search, down or up respectively, for the word under the cursor. Make sure to <code>:set incsearch</code> and <code>:set hlsearch</code> first.</p>\n'}, {'answer_id': 71957, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>If you're using vim, the 'u' command (in command mode) will Undo the last command you typed.  You can use this command repeatedly to undo mistakes you may have made before saving the file.</p>\n"}, {'answer_id': 72509, 'author': 'JP Lodine', 'author_id': 8998, 'author_profile': 'https://Stackoverflow.com/users/8998', 'pm_score': 2, 'selected': False, 'text': '<p>See <a href="http://www.rayninfo.co.uk/vimtips.html" rel="nofollow noreferrer">http://www.rayninfo.co.uk/vimtips.html</a> for a great collection of Vim tips, from the basic can\'t-live-without to very sophisticated stuff that you might never have thought of trying.</p>\n'}, {'answer_id': 283612, 'author': 'Sergio Acosta', 'author_id': 2954, 'author_profile': 'https://Stackoverflow.com/users/2954', 'pm_score': 2, 'selected': False, 'text': "<p>Although this is a matter of personal preference I've found that one of the essential things to do is to remap <kbd>Esc</kbd> to something else.</p>\n<p>I find it very uncomfortable to reach for the <kbd>Esc</kbd> key to exit insert mode, but the beautiful thing about Vim is that allows key mappings.</p>\n<p>I'm currently using the following mapping using <kbd>Control</kbd> + <kbd>S</kbd>:</p>\n<pre><code>inoremap &lt;C-s&gt; &lt;Esc&gt;:w&lt;CR&gt;\n</code></pre>\n<p>This has the advantage of being a key mapping I have already committed to memory and has the added value of saving my work every time I go to normal mode. Yeah, I know it is crazy but I would be hitting the save command that frequently anyway. It's like a bad habit, you know.</p>\n"}, {'answer_id': 283659, 'author': 'Berzemus', 'author_id': 2452, 'author_profile': 'https://Stackoverflow.com/users/2452', 'pm_score': 1, 'selected': False, 'text': '<p>I like the <a href="http://www.risner.org/vim/" rel="nofollow noreferrer">Vim 5.6 Reference Guide</a>, by Bram Moolenaar and Oleg Raisky.<br>\nYou can directly print it in booklet form, easy to read, I always have it laying around.<br>\n   It\'s a tad old, but what are 8 years in Vi\'s lifespan ?</p>\n'}, {'answer_id': 283705, 'author': 'Jonas', 'author_id': 16047, 'author_profile': 'https://Stackoverflow.com/users/16047', 'pm_score': 2, 'selected': False, 'text': '<p>Lots of great commands are listed at the <a href="http://vim.wikia.com/wiki/Best_Vim_Tips" rel="nofollow noreferrer">Vim Tips Wiki</a>.</p>\n'}, {'answer_id': 540216, 'author': 'Jeff', 'author_id': 33775, 'author_profile': 'https://Stackoverflow.com/users/33775', 'pm_score': 1, 'selected': False, 'text': "<pre><code>:set ignorecase smartcase\n</code></pre>\n\n<p>Makes searching case-insensitive, unless your search includes a capital letter.  Not the <em>most</em> indispensable perhaps, but I find myself setting this option any time I'm editing in a new place.  It's in any vimrc file I own.</p>\n"}, {'answer_id': 540235, 'author': 'Steve Rowe', 'author_id': 40744, 'author_profile': 'https://Stackoverflow.com/users/40744', 'pm_score': 0, 'selected': False, 'text': '<p>The real power is in the searching.  Here are the essential commands:</p>\n\n<p>/Steve will find the first instance of "Steve" in the text.\nn will find the next "Steve" in the text.\n:%s//Stephen/g will replace all those instances of "Steve" you just searched for with "Stephen".</p>\n\n<p>Not to promote myself, but I wrote a <a href="http://blogs.msdn.com/steverowe/archive/2008/05/24/gvim-as-notepad.aspx" rel="nofollow noreferrer">blog</a> post on this subject.  It focuses on the critical parts of Vim for a beginner.</p>\n'}, {'answer_id': 834517, 'author': 'RedBlueThing', 'author_id': 73488, 'author_profile': 'https://Stackoverflow.com/users/73488', 'pm_score': 1, 'selected': False, 'text': '<pre><code>:%!xxd\n</code></pre>\n\n<p>View the contents of a buffer in hexadecimal. To revert:</p>\n\n<pre><code>:%!xxd -r \n</code></pre>\n'}, {'answer_id': 834571, 'author': 'Shashikant Kore', 'author_id': 77308, 'author_profile': 'https://Stackoverflow.com/users/77308', 'pm_score': 0, 'selected': False, 'text': '<p>My favorites:</p>\n\n<pre><code>%  find matching bracket/brace \n* and #  next/previous match\ngg top of page\nG end of the page\n&lt;Ctrl-v&gt; Change to visual mode and select column\n&lt;Ctrl-a&gt; increase current number by 1\n&lt;Ctrl-x&gt; decrease current number by 1\nRunning macros\n</code></pre>\n'}, {'answer_id': 837942, 'author': 'Brad Cox', 'author_id': 100321, 'author_profile': 'https://Stackoverflow.com/users/100321', 'pm_score': 0, 'selected': False, 'text': '<p>Nobody mentioned exuberant ctags? Google and install it; much better than the default ctags you probably have. To use it, cd to your project root and type</p>\n\n<p>:!ctags -R .</p>\n\n<p>Builds a database of everything in your project...java, c++, python, ruby, javascript, anything, in a file called tags.</p>\n\n<p>:help ctags for a host of commands, too many to summarize, for using the generated tags.\nPut the cursor on a function name, type CMD ], to open the file that defines it. Many more commands like that. Soon becomes second nature...almost as nice as an IDE (and VIM never lets you down they way eclipse often does.</p>\n'}, {'answer_id': 4726483, 'author': 'Pif', 'author_id': 455623, 'author_profile': 'https://Stackoverflow.com/users/455623', 'pm_score': 0, 'selected': False, 'text': '<p>I made my first steps using <a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html" rel="nofollow">The tutorial here</a>, and have used the reference cheatsheet for a few weeks. And, of course, there\'s vimtutor in vim/gvim/Macvim.</p>\n'}, {'answer_id': 4728331, 'author': 'Scott Saad', 'author_id': 4916, 'author_profile': 'https://Stackoverflow.com/users/4916', 'pm_score': 0, 'selected': False, 'text': '<p>Sometimes it\'s nice to reformat a buffer (i.e. re-tab, align braces, etc). I recently learned a time saver for this:</p>\n\n<pre><code>gg=G\n</code></pre>\n\n<p>For example... it would turn the following:</p>\n\n<pre><code>if ( !flag )\n{\n  // Do something special\n  }\nelse\n   {\n // Do another special thing\n}\n</code></pre>\n\n<p>into the following:</p>\n\n<pre><code>if ( !flag ) \n{\n    // Do something special \n}\nelse\n{\n    // Do another special thing\n}\n</code></pre>\n\n<p>Or if you had an xml file that you\'re hoping to re-indent because the format is all screwy you could run the above command and turn something like the following:</p>\n\n<pre><code>&lt;root&gt;\n&lt;addressBook&gt;\n    &lt;contact first="Frank" last="Tank"/&gt;\n        &lt;contact first="Foo" last="Man"/&gt;\n    &lt;/addressBook&gt;\n&lt;/root&gt;\n</code></pre>\n\n<p>into something a bit more human readable like the following:</p>\n\n<pre><code>&lt;root&gt;\n    &lt;addressBook&gt;\n        &lt;contact first="Frank" last="Tank"/&gt;\n        &lt;contact first="Foo" last="Man"/&gt;\n    &lt;/addressBook&gt;\n&lt;/root&gt;\n</code></pre>\n'}, {'answer_id': 7337917, 'author': 'Jack Kinsella', 'author_id': 286286, 'author_profile': 'https://Stackoverflow.com/users/286286', 'pm_score': 0, 'selected': False, 'text': '<p>I switched from Textmate to VIM a few months ago and wrote a guide on how to do 110 Textmate editing commands within VIM. It\'s organised into categories, such as managing files, auto-completing words and syntax highlighting. </p>\n\n<p><a href="http://www.jackkinsella.ie/2011/09/05/textmate-to-vim.html" rel="nofollow">Textmate to VIM</a></p>\n'}, {'answer_id': 9047380, 'author': 'kikuchiyo', 'author_id': 1040358, 'author_profile': 'https://Stackoverflow.com/users/1040358', 'pm_score': 0, 'selected': False, 'text': '<p>Please see this site for a fun way to learn the essential movement commands: <a href="http://kikuchiyo.org" rel="nofollow">http://kikuchiyo.org</a> .  I think most essential commands are covered in the thread, but I always like suggesting this for new-comers to vim.  click the train first link, which has a legend for basic movement commands and the insert command <code>i</code> for picking up rubies.  Good practice for moving around quickly.</p>\n'}, {'answer_id': 13023747, 'author': 'ivotron', 'author_id': 389188, 'author_profile': 'https://Stackoverflow.com/users/389188', 'pm_score': 0, 'selected': False, 'text': "<p><code>:g/&lt;pattern&gt;/t$</code></p>\n\n<p>will copy <code>&lt;pattern&gt;</code> to the end of the file. Useful when you want to extract lines but don't want to do it one by one.</p>\n"}, {'answer_id': 34129669, 'author': 'sudo bangbang', 'author_id': 3951782, 'author_profile': 'https://Stackoverflow.com/users/3951782', 'pm_score': 1, 'selected': False, 'text': '<p>I have this in my vimrc</p>\n\n<pre><code>set number\nset relativenumber\n</code></pre>\n\n<p>This gives me a line numbering system which makes <kbd>j</kbd>, <kbd>k</kbd> keys really productive.</p>\n'}]
['https://Stackoverflow.com/questions/69871', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3575/']