vimsheet - feliyur/exercises GitHub Wiki

Basic functions

Key Functionality
i/Esc Enter / exit edit mode
:x Save and exit.
:[range] s/replace/with/gc                          Search and replace with confirmation. Range can be a pair of start,end line or % for entire file. Drop c for no confirmation. Also see here.
:set tabstop=4
:set shiftwidth=4
:set expandtab
Set tab to 4 spaces. Following this post. Can add those commands to $HOME/.vimrc vertabim (without ':'). Can use :retab to update document to new setting.
h,j,k,l Movement, left, down, up, right respectively
`` or Ctrl+o, Ctrl+i Jump back, forward
Ctrl+d,u,e,y Move screen keeping cursor in place: half page down, up, one line down, up respectively.
:!<bash command> Executes shell command.
:/<search query string> Search document (case sensitive). Can use \c before any character for case-insensitive search, at the end of the search for the entire string.
Shift+u / u (in visual mode) to upper / lower
:set number, :set nonumber, :set number!, :set nu! Show / hide / toggle line numbers.
:set ro switch to readonly mode (deletes .swp file).
:e! :edit! Discard changes and reload current file.
q<register><sequence>q saves <sequence> as a macro in <register>. Macro can be invoked by @<register>. Last used macro can be repeated with @@
[<count>]ctrl+g Show current filename. If <count> > 1 is prepended, the entire path is shown.
shift+k Show manpage of word under cursor
q[:,/,?] Open command / search history. Ctrl+F command history from command mode. Ctrl+w,c to close. see [4]
:%!python -m json.tool Fix json file formatting
set foldmethod=<method> or set fdm=... For method one of syntax, indent and more. Fold using za unfold using zo. Fold all zM, unfold all zR, level by level zm, zr.
:set paste
:set nopaste
:set paste!
Toggle autoindenting to allow paste text preserving spaces. Can also set pastetoggle=<F3> in .vimrc

Go to definition / search word under cursor docs: https://vim.fandom.com/wiki/Go_to_definition_using_g

Tab pages and Windows

Compiled from here (also see for more commands) and here. Note that it's enough to write just enough command characters for vim to disambiguate, e.g. tabe or tabf instead of tabedit or tabfind.

-p argument when starting vim opens each given file in a separate tab
:tabedit {file} edit specified file in a new tab
:tabnew Open a new empty file (buffer) in a new tab
:tabfind {file} open a new tab with filename given, searching the 'path' to find it
:tabclose [{i}] Close current tab (if i not given) or i-th tab
:tabonly close all other tabs (shown only the current tab)
:sp {file} creates a new window (horizontal split) in the current tab editing the specified file. vsp creates vertical split
Ctrl+w,[s,v,q] Splits window horizontally, vertically, closes window
Ctrl+w,[h,j,k,l] Switches windows in direction of movement
Ctrl+w,Ctrl+w Switches windows
:set [windo] scb or :set [windo] scrollbind Sets synchronized scrolling. Can do for desired windows separately, or use windo which applies for all open windows. Unset with set no scb. Can toggle with scb!.
:tab sp {file} opens above split in a new tab
:enew Open a new empty buffer in the current window.
:tab split copy the current window to a new tab of its own
gt, gT, {i}gt Go to next / previous tab / tab in position i
tabs list all tabs including their displayed windows
Ctrl+w,c Closes current window
gf To open file under cursor. Ctrl+w, gf opens file in new tab. Ctrl+w, Ctrl+f opens in horizontal split. Ctrl+w, vgf opens in vertical split Reference

Unicode support

" switch to right-to-left mode 
:set rl
" switch to left-to-right mode
:set norl

Can also tell vim that the terminal is in charge of text direction, if the terminal supports bidi(rectional).

:set termbidi

When using inside tmux may need to run with tmux -u for unicode to display correctly.

vimdiff

:ls show pane numbers
do, dp diff obtain, diff put
2do, 2dp obtain/put to buffer 2
ctrl+w + ijkl move among panes
]c, [c move to next, previous diff
:wa, :xa, :qa write / save and exit / quit all
:diffupdate recompute diff
:windo diffthis and :windo diffoff Enter / exit diff mode (equivalent to (:diff, :diffopt, :scrollbind))

Set as git mergetool

git config --global merge.tool vimdiff

Color schemes

Default vimdiff colorscheme often shows lousily (e.g. diff text invisible). To change that, can add to ${HOME}/.vimrc (" is a comment):

if &diff
"   colorscheme github
"   colorscheme molokai
    colorscheme evening
endif

(or just run :colorscheme xxx inside vimdiff). Might need to download the theme first, e.g.

curl -fLo ~/.vim/colors/molokai.vim --create-dirs https://raw.githubusercontent.com/tomasr/molokai/master/colors/molokai.vim
curl -fLo ~/.vim/colors/github.vim --create-dirs https://raw.githubusercontent.com/endel/vim-github-colorscheme/master/colors/github.vim

(Much) more details on stackoverflow.

Troubleshooting

In terminator, can't copy text when editing with vim Use set mouse=v in vim command mode or inside .vimrc.
⚠️ **GitHub.com Fallback** ⚠️