# vim ``vim`` is a text editor. # Navigation Use ``hjkl`` to move around. ``Ctrl-f`` and ``Ctrl-b`` moves forward and back a page. For line navigation, use ``0`` and ``$`` to go to the start and end. Move between **words** with ``b`` and ``w``. Move between **sentences** with ``(`` and ``)``. Move between **paragraphs** with **``{``** and **``}``**. Search for word at cursor with ``*`` and ``#``. Navigate quickjump list with `Ctrl-O` and `Ctrl-I`. # Copy/Paste ``d`` to cut, ``y`` to copy and ``p`` to paste. (``d``elete, ``y``ank and ``p``ut) Copy to clipboard from the top of the buffer to the cursor, press ``Space-h``. This is a custom leader binding, source [here](https://blog.dnmfarrell.com/post/how-to-copy-a-vim-buffer-to-the-clipboard/). To paste while keeping the current line's indentation, use ``]p.`` To copy to primary clipboard (alt c - alt v in terminal) use `"+y` # Marks and registers Vim has registers and marks. Registers are like boxes to store text in, and marks are pointers to a character in a file. To copy some text, use ``ma`` to mark the current position into mark ``a``, go to the other end and do ``y'a``. This copies the text from mark ``a`` to here. To access the exact marked character, use `` a`` You can use registers by prepending any operation with ``"r`` to use the ``r`` register. For example, you can copy the current line into the ``a`` register with ``"ayy`` and paste it somewhere with ``"ap``. # Changes You can change text inside certain selectors such as words, paragraphs or delimiting characters. Replace entire words with ``ciw``. This keeps the whitespace after the word. (``caw`` doesn't) To change text inside quotes, use ``ci"`` You can use other commands with selectors, like ``dw`` to delete a word, ``dip`` to delete a paragraph. Append text to the end of the current line with ``A``. Insert text at the beginning of a line with ``I``. Insert emtpy line below cursor with ``o``. Same but above with ``O``. Global replace: `:%s/this/that/g` `%` means to operate on every line. # Macros Record actions and replay them when needed. `qa` starts recording a macro in register `a`. Stop recording with `q`. `@a` replay macro from register `a`. # Buffers Buffers are files loaded into memory. View a list of them with ``:ls`` Switch between them with ``:b ``. This supports autocompletion with filenames. Custom leader binding: ``Space-b`` Some custom bindings allow me to move between buffers with ``Alt-`` as well. (remapped to ``:bp`` and ``:bn``) # Windows Use windows for having multiple views into a buffer. Create them with ``:new`` or any kind of split. Navigate between them with ``C-w-`` (just like you would normally move in vim) Move them with ``C-w-r`` to rotate them around, or with ``C-w-`` to move them in the vim directions. Resize windows equally with `C-w-=`. Change height with `C-w-{+,-}` and width with `C-w-{<,>}` # Splits Split the current view horizontally with ``:split`` or ``C-w-s`` and vertically with ``:vsplit`` or ``C-w-v``. # Sessions If you've been working on a project you probably have windows and splits just as you want them. To save a session, do ``:mksession``. This will save a Session.vim file in the current working directory. To save it somewhere else pass a location to the command. To load a session, do ``:source Session.vim`` # Text wrapping Set `tw` to maximum column width, then select what you want to format and type `gq` See https://blog.ezyang.com/2010/03/vim-textwidth/ for more info. # Folds You can fold sections with zf operator. Specify range visually or with regular movement commands. Fold/Unfold with `zo` and `zc`. # Diffs You can use vim as a mergetool with [git](git) for easier conflict resolving. Run `:diffget ` to choose which version to keep. # Hints Use `[i` to get the selected function's signature as a hint in the command window # Plugins You can manage plugins using Vim's native plugin support or with something like vim-plug: tpope * vim-commentary: This is for quickly commenting lines. Use `gc{motion}` to comment/uncomment and `gcc [count]` for count nr lines. * vim-fugitive: git wrapper for vim. G, Gclog, Git blame, Gdiffsplit are useful commands. vimwiki * vimwiki: note taking in vim junegunn * fzf.vim: fzf fuzzy file finder vim integration See also -------- * [Learn Vim](https://learnvim.irian.to/basics/starting_vim) -> intro to vim * [Mastering the Vim language](https://www.youtube.com/watch?v=wlR5gYd6um0&list=PL8tzorAO7s0jy7DQ3Q0FwF3BnXGQnDirs&index=10) -> video: vim basics * [Your problem with **Vim** is that you don't grok **vi**](https://stackoverflow.com/a/1220118) -> really informative micro-tutorial to vi(m) commands, and a little bit of history * [How to Do 90i% of What Plugins Do (With Just Vim) - YouTube](https://www.youtube.com/watch?v=XA2WjJbmmoM) -> video: setup vim to do useful stuff without external plugins * [Debugging in Vim - Yaroslav's blog](https://www.yaroslavps.com/weblog/debugging-in-vim/)