Vim cheat sheet


BEWARE: Kite is an invasive software that is often bundled with autocomplete-python. It will block every other opensource software from doing autocomplete. In my case, I had no idea of its invasiveness until I installed YouCompleteMe and realized kite disables any other software doing autocompletes. I hunted down kite installs. Removed them from Pycharm and VSCode. It still kept blocking. Then had to ~/.local/share/kite/uninstall to completely remove it from my system.

YouCompleteMe

For installing YouCompleteMe plugin:
This one needs to be installed manually in multiple steps
Step 1: https://github.com/ycm-core/YouCompleteMe#linux-64-bit
Note, you will need Python3.6 or the default Python3 for all steps where python needs to compile any script (like install.py). IT WONT WORK WITH PYTHON3.7.
Install the plugin itself. After installation of all dependencies and the plugin itself

Step2: open any file via vim. If you see and error message flash, like YcmdServer shutdown. Check the log for it. In my case, I had to reinstall a module called watchdog manually. Per the instructions here: https://github.com/ycm-core/YouCompleteMe/wiki/Full-Installation-Guide
Search for “Install watchdog library”
IMPORTANT: watchdog is Python3 only. so install it like so:python3 setup.py build --build-base=build/3 --build-lib=build/lib3

Installing Powerline

sudo apt-get install powerline
sudo apt-get install fonts-powerline
After above two steps, follow the instruction in answer with title “On 14.04 with the latest version of Powerline”
https://askubuntu.com/questions/283908/how-can-i-install-and-use-powerline-plugin

Enable copy paste support across tmux panes and to system clipboard

Trick 1: Before copying to clipboard, the magic is to press shift in advance and draw the area first (which you wanna copy to system clipboard), the area gets grey colored text background.

Afterwards press the default hotkey to copy to system clipboard. I am using Konsole and there it is Shift-Ctrl-C.

Trick 2:
Since copying to system clipboard involves xterm_clipboard which is disabled in non-gui vim, you need to install vim-gtk to get that feature enabled inside vim (if you do :version inside vim, you will see -xterm_clipboard, meaning this feature is disabled in default non-gui vim).
sudo apt install vim-gtk
this will also enable X11 support (for mouse copy) and xterm_clipboard.
Now add this to .vimrc
set mouse=a
set clipboard=unnamedplus
select what you want using the mouse – then type to copy to clipboard:
"+y
to paste to vim from clipboard type:
"+p

NERDTree

h left vim pane
l right vim pane
j below vim pane
k up vim pane

Cheatsheet

Nice Link:https://stackoverflow.com/questions/5400806/what-are-the-most-used-vim-commands-keypresses/5400978#5400978

Managing several splits:
A window is a viewport on a buffer. In vim to manage windows it is CTRL+w the leading command, that you can follow with several options (in bold those that answer to your question):

CTRL+w, v: Opens a new vertical split

CTRL+w, c: Closes a window but keeps the buffer

CTRL+w, o: Closes other windows, keeps the active window only

CTRL+w, right arrow: Moves the cursor to the window on the right

CTRL+w, r: Moves the current window to the right

CTRL+w, =: Makes all splits equal size

Then, you need to switch the buffers in the windows:

:ls lists all opened buffers

:b5 switches to your 5th buffer

Moving within Line

  • First char of the line: 0
  • First non-blank char of the line: ^
  • First char of the line and put in insert mode: I
  • Last char of the line: $
  • Last non-blank char of the line: g_
  • Last char of the line and put in insert mode: A

Jumping Lines

  • top of the screen: H
  • middle of the sceen: M
  • bottom of the screen: L
  • (mnemonics are High, Medium, Low)

Shifting screen:

  • current line to top, middle and bottom: zz vs zt vs zb
  • scroll one line: ctrl+y and ctrl+e -> cursor does not move (with j and k, cursor moves)
  • scroll half-page: ctrl+u and ctrl+d
  • scroll full-page :ctrl+b and ctrl+f

Searching:

  • Search for word currently on: * -> this will search forwards
  • Search for word currently on: # -> this will search backwards
  • viw to visually select the word under the cursor.

Searching replacing copy pasting

    :%s/ipsum/lorem/g

  1. % means do this for every line
  2. s means sed, or search and replace
  3. g at the end means replace every ipsum with lorem; if you omit this, it only replaces the first.

ye (yank to end of word)

Substitute a word in-place

If you yank the first word with yiw, you can then replace another word with the first one by typing viwp.

If you leave out the i, yw will also yank the whitespace after your word and when pasting this you get too much spaces. Therefore, use i to select the inner word. viwp is also very intuitive. Visually select the inner word and then paste it with the yanked. iw meaning inner word as in word without surrounding spaces.

surround word with ” or “” -> ciw””EscP
Goto the start of the word. Type c i w ” ” Esc P without the spaces in between. This will surround the word with “”. Replace “” with ” for ”.

Word Wise navigation: e vs E, b vs B, w vs W
see here for a very nice implementation: https://github.com/tpope/vim-surround

setting marks

making vim macros