101

Vim comes with an exhaustive and fully indexed documentation that contains the answers to most of the questions you may have on using Vim. But the documentation is huge and may look to the neophyte as an impenetrable maze. Here are a few guidelines to help you find what you need… The :help command is your gateway to Vim's documentation. Read the first ...


84

The `. command will bring you to your last change. The ` goes to a mark, and . is a "special" mark which is automatically set to the position where the last change was made. See :help `. for some more information. There is also `` which will bring you back to where the cursor was before you made your last jump. See :help `` for more information. Another ...


66

Actually this is not a bad practice. A lot of people (including Vim's doc as @B Layer's answer shows) argue that you shall not use arrow keys because it makes your hands move from the home row (the second line of your keyboard where you have hjkl) and that make you less efficient. This is (IMO at least) a bad result from vim "purists" circle jerk: new ...


44

Vim ships with a macro called matchit that does this for you; all you need to do is activate it with runtime macros/matchit.vim in your vimrc. This will enable you to jump from, eg, a <div> to its </div>. Note that your cursor will have to be inside the angle brackets; if you're on the angle brackets, % will jump from one bracket to the other as ...


40

I don't know if this is really relevant any more, but I'm an old-timer so here's a bit of history. In the old days, VT100 terminals had arrow keys, but pressing one transmitted an escape sequence like <ESC>[A for up, <ESC>[B for down, etc. Also, being a serial terminal communicating at 9600 baud, it was possible for the user to press the keys ...


34

You can use 0D this will go to the first character on the line and delete until the end of the line. Note that you can use ^D if you want to leave any preceding whitespace alone. Also, these operations cannot be repeated by using .. If you want an operation which can be repeated with the . command, refer to David Lord's answer. Help topics: :help 0 :...


33

Yes, you can use the } and { paragraph motions to move a paragraph forwards or backwards. From :help paragraph: A paragraph begins after each empty line, and also at each of a set of paragraph macros, specified by the pairs of characters in the 'paragraphs' option. The default is IPLPPPQPP TPHPLIPpLpItpplpipbp, which corresponds to the macros .IP, ...


29

:-d cuts the line above the current line. :-5d cuts the 5th line above the current line (but moves the cursor). :-5,-d cuts the 5 lines above the current line. :+,+5d cuts the 5 lines below the current line.


28

You can change the behaviour of n and N to search consistently downwards/upwards by useing the following: nnoremap <expr> n 'Nn'[v:searchforward] nnoremap <expr> N 'nN'[v:searchforward] Update: How does it work? This uses expression-maps, which maps the lhs of the map to an expression that will be evaluated, everytime the lhs is encountered. ...


28

I would use d]) which means delete (d) to the next unmatched ')' (])). See :help ]).


28

While I agree with @statox that no one should be chastised, shunned, excommunicated, or tarred-and-feathered if they choose to use the arrow keys...use your software however you see fit...I wouldn't exude as much of a laissez-faire attitude if I were giving guidance to an open-minded new user. I'd pretty strongly recommend that they start with following best ...


26

Type H (capital H) to move to the top of the screen. Type L (capital L) to move to the bottom of the screen.


25

The main difference here is that parenthetical statements (targeted by ci() can be nested, while quoted strings (targeted by ci") cannot. If your cursor is outside of some parenthetical statement, it could still be inside of some other one. For example (* marks the cursor position): $foo = array( 'x' => ar*ray(1, 2, 3), ); The line the cursor is on ...


24

In case you, or someone else reading this topic, just wants to disable the key movements without the text warning enter the following lines in .vimrc noremap <Up> <Nop> noremap <Down> <Nop> noremap <Left> <Nop> noremap <Right> <Nop> The commands will only disable the key movement in normal mode. They still ...


23

Looks like a case for ex copy (:help :co or :help :t): :1t4 or, using your cursor's position: :1t. This does not copy the line to the yank register, which might or might not be what you want.


22

You can jump between tags using visual operators, in example: Place the cursor on the tag. Enter visual mode by pressing v. Select the outer tag block by pressing a+t or i+t for inner tag block. Your cursor should jump forward to the matching closing html/xml tag. To jump backwards from closing tag, press o or O to jump to opposite tag. Now you can either ...


22

You can install the hardmode plugin and in your .vimrc put in let g:HardMode_level = 'wannabe' let g:HardMode_hardmodeMsg = 'Don''t use this!' autocmd VimEnter,BufNewFile,BufReadPost * silent! call HardMode() If you don't want to use a plugin (which may be a better choice, as you get to customise everything yourself!), use nnoremap, vnoremap and inoremap ...


21

You can use g series of commands to move to the boundaries of the visible screen area. For example, g$ moves to the right edge of the screen (which is not necessarily the end of the line). gj moves the cursor down one line as it appears on your screen (which is not necessarily one logical line down). Perhaps you could rebind the arrow keys: nnoremap <...


21

Here's another approach that fits your given scenario, and will jump to where you were immediately prior to the gg (not to the last changed line). Use CtrlO When you press gg, your old cursor position is pushed onto the jump list. From :help jumplist: Jumps are remembered in a jump list. With the CTRL-O and CTRL-I command you can go to cursor ...


21

In Vim there are no multi-cursors like in Sublime Text (but there are some plugins as I remember). But it seeems not so important stuff for vim because there is another way to achieve this. For example you have this line Spam and Eggs is all you need for a healthy breakfast I would doing something like this: place cursor in normal mode on the first letter ...


20

cc <Esc> will do as you ask, but is more keystrokes than 0D. If you want to put something into that line afterwards, cc may be best.


19

To add to dnetserr's answer and Peter Rincker's comment, Vim maintains a list of changes, and has some commands associated with this. :changes will list the changes, showing you where they were and what they were. For example: change line col text 2 8 17 #include <stdio.h> 1 3 0 #include "stm32f407.auto.h" > The line with ...


19

You should use getpos(): To save you position in a variable: let save_pos = getpos(".") getpos() takes as argument a mark, here "." represents the current position of your cursor. And to restore it: call setpos('.', save_pos) Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position)...


19

You can use vi"p when inside "". This can replace text in yank register, so it matters when you want to use original yanked text more than once. EDIT: Additional info from Octaviour comment regarding making it reusable more than once: In order to do that I would yank to a named register, which makes the two commands: "ayi" and and vi""ap if you use ...


18

I agree with Alexander here. In day to day editing I would probably use the . command as well, but if you want to execute just one command I would use the substitute command: :s/S\|E/organic_&/g whereas the & is the matched pattern from /S\|E/.


17

You can use the matchit plugin. This is included in modern vim distributions, so all you have to do to use it is add the following to your vimrc: runtime macros/matchit.vim You can also get it packaged as a plugin if you prefer. It recognizes many keywords by default (including def and end) and can be extended to recognize more.


17

These are just a few of the movements that vim is capable of, these should get you going for now. TL;DR Use Vim's built in help feature on navigation or better still usr_03.txt Search google for vim advanced movements Navigate with/to words go here All the right moves Movements There are many ways of getting around in vim I have listed some that I found ...


17

Maybe ctrld and ctrlu could be what you are looking for. By default they move half of the screen. From :h CTRL-D: Scroll window Downwards in the buffer. The number of lines comes from the 'scroll' option (default: half a screen). If [count] given, first set 'scroll' option to [count]. Which means that the first time you want to scroll in a ...


16

I don' know any builtin way to do this, however, you can easily have a map to do it: nnoremap µ :exe 'normal! '.(virtcol('$')/2).'\|'<cr> @Ingo Karkat proposed the following variant that also works in VISUAL-mode. noremap <expr> gM (virtcol('$') / 2) . '<Bar>' Unfortunately, it's also triggered in SELECT-mode. As a consequence, I prefer ...


16

I'm not 100% clear if this is what you're asking, but I think you might be looking for: ]) This jumps to the nearest enclosing ). So if your cursor was on the / in the below: (2 / (3 + 5) * 9) ^ It would skip past the first ) and jump to here: (2 / (3 + 5) * 9) ^ Alternatively, for your more simple case, %% would also work. The ...


Only top voted, non community-wiki answers of a minimum length are eligible