Personal web page: Emmanuel Branlard

Useful vim commands

Motions


h j k l 0 $ ^          % basic motions. If multilines,  use gk gj g$ g0
12G   % go to line 12 or
:12   % go to line 12 (easier, you see it)
M     % go to the middle of the screen
w b e % word back end (i.e. next/previous word, end of word)
W B E % same but uses spaces as delimiters
fx Fx tx Tx ; % move to the next occurence of letter x (backward or forward, just before(t) or on it(f). f is usually the best. Repeat motion with ;
z stuff - combine it with: set scrolloff=3 for a smooother screen offset

zt  % will scroll so that the current cursor is at the top
zz  % will scroll so that the current cursor is at the middle
zb  % will scroll so that the current cursor is at the bottom
Scrolling (I like to combine with a :set scrolloff=4, I'd like also :set scroll=6, but it doesn't work)

CTRL-E CTRL-Y  % scroll up/down line by line (can be nice to boost it : nnoremap <C-e> 3<C-e>)
CTRL-B CTRL-F  % page backwards page forward
CTRL-U CTRL-D  % half page up half page down, or whatever you set with scroll (default scroll=0)
% jumplist
CTRL-o CTRL-i  % !!! go to older / newer position (in different buffers) counts: search jumps, buffer jumps, lines jumps (goto) etc
:jumps         % display the jumplist (not that useful)

Undo, and changelist


<C-r> % redo!!!
u U   % undo, undo all in line
% changelist (different than the jumplist)
:changes   % display content of the changelist
g;         % go to the last position of the cursor where something has been edited
g,         % - previous

I/O


:e filename   % edit (i.e. open)
:e    % edit current (reload it, same as :e %)
:e .  % open file-browser
:w    % write (i.e. save)
:w filename   % write to filename (but still don't open  it)
:q    % quit (close the window, if only only window close the tab, if only one buffer, quit vim)
:q!   % the question mark always forces stuff (quit without saving changes)
:wq   % save and quit
:ZZ   % save and close file (I don't like it)
:Sex  % split window and open file explorer
:browse e  % GUI for opening file

Going to insert mode - Basics - (don't use only i!!!)


a i o  %appen, insert, open a line below
A I O  %really convenient , append end of line, insert beginging of line, open above

Exiting insert mode


CTRL+C CTRL+[ or ESC to escape

Insert mode


% Completion
CTRL+N CTRL+P % Word/variable/command completion !!!
CTRL-X CTRL-L % LINE COMPLETION
CTRL+H % backspace

% Upercase Commands -> execute till the end
D  %does the same than d$  % delete till end of line $
%The same apply for c and C, v and V

Deletion/ Change / Selection / Copy - Same behavior


x
xp % reverse two letters, usefull for typos like: fro instead of for
dd % delete line
dj % delete current line and below
dw % delete the rest of the word and next space (good for full words)
de % delete rest of the word but not the space (good for the end of a word)

Smart replacements/selection deletion


di} or di{ % delete everything between brackets
dt} % delete toll the next braket but does not delete the }
df} % same but delete the bracket
vi} % select everything between braces (same as vi{)
va} % idem but includes braces
yi} % copy content between braces
ci} % etc ..

viw % select the current word
viwc %will select the current word and replace it
diw % will delete current word, similarly ciw
daw % will delete current word, similarly caw

Replacement


r  % replace one characters
R  % like pressing the insert key
cw % change word
bde % delete current word in totallity
bce % change current word in totallity
~ % switch uppercase lowercase
U % switch to uppercase
bveU % select current word and switch to upper case

Programming

Matching


%    % go to matching brace, parenthesis, if/endif, etc
]) % find next non-matching closing parenthesis
]{ % find next non-matching opening brackets

Code Folding


zM      % fold all
zR      % unfold all levels
za      % toggle paragraph folding
zo      % open folded code
zf      % visual mode? to fold
zj zk   % Go from one fold to the other

% Not recommended method: (because leaves traces in the file)
set foldmethod=marker  % then by default {{{   }}}  open and close folds.
% But closing is quite repetitive, so {{{1  is enough (level one indent)
% You can custumize it, see my fortran example in after/ftplugin

Code indentation


% Indentation in normal mode:
< >    % indent , use . to repeat
>>< << % indent
]p     % like p but adjusts indent
==     % autoindent selection ?
=      % autoindent line
If on a first braket { then   =%    will indent everythin to the matchning bracket
If withing a code block then =a{ will work as well
% indentation in insert mode
<C-t> <C-d> % indent, deindent by one level

Copy paste cut


p      % put after  (uses the register %, i.e. equivalent to %%p)
P      % put before
yy p   % copy paste one line
dd p   % cut paste one line
v y P  % select copy paste
%+y and %+gp or %+gP   % copy to the %+ register (not usefull with set clipboard+=unnamed,unnamedplus  (though I havn't solved the issue completly yet))
%0p    % paste the register 0 (i.e. the one that has been yanked)
:reg   % display registers

Block mode


<C-V> % select in block mode
% in selected block mode, use c or I or R to change, insert, replace
% To paste in selected block mode, use the Register comand <C-R>
<C-V>  I <C-R> %
<C-V>  c <C-R> 0
% insert at beginning of line
Select beginning of line using CTR-V then SHIFT+I then type text to insert, then ESC (not CTRL+C!!!)
% at end of line:
:<,>s/$/TEXT/g    bla bla-$
% to insert something at end of line
<C-V> $ A      %$

Marks (usefull when copying smthg far away)


ma  % mark 'a', not a command
`a  % go to a

Search and replace


/            % search, moving with n and N
:s/aa/bb/g   % all in current line
:%s/aa/bb/g  % in all document
:l1,l2s/aa/bb/g % all between l1 and l2
:set hlsearch
:nohlsearch
:s/\v^(foo){1,3}(.+)bar$/\1/   % the \v makes the expression a normal regexp
% g /norm   % search for a pattern and then pass it , norm will go to normal mode on the match and apply a set of commands
:g/sometext/norm ddGp
/\< the 	% Search words beginning with (ex here: the, theatre or then)
/the\> 	    % Search words ending with (ex here: the or breathe
/\< the\> 	% Search only this word (here :the)
% Grep:
:grep expression *.php  % see also the arguments you can give to grep in your virmrc. If you specify -Insri then no need to specify files
:Ggrep expression   % search in your git repository (fugitive)

Buffers


%All files open in vim are listed as buffers. They can be opened, in tab or windows, it doesnt matter. This means that at any time you can access any buffer and put it in a tab or a window
:ls  % list
:bn  % next buffer in list
:bp  % previous
:b#  % last (toggles)  or Ctrl-^
:b number % go to buffer number
:bd    % closes current buffer
:sbl   % split with last buffer
:sball % open all buffers in splits..

Sourcing a file


:so filename
:so %         %: source the file you are editing. if it's .vimrc, then config is reloaded

Paragraph reformating


rENTER % split ..
J      % join with line below; whole file % use cmd paste
set textwidth=80     0 to cancel %
gqap   % reformat current paragraph
gggqG  % reformat all paragraphs in file

Windows


:sp        % opens in a split window
<C-W> s    % splits horizontally
<C-W> v    % splits vertically
<C-W> w    % loop through windows
<C-W> jkhl % move between windows
<C-W> _    % maximize window
<C-W> c    % close window
:on        % makes the current window the only one

Tabs


:tabnew  %new tab
:tabe  %open a file in a new tab
:tabc
:tabn or gT  %next tab
:tabN or gt %previous tab

Spell check


nnoremap ,ts :set invspell spell?<CR>     % toggle spell check
z=    % on a word, display list of suggestion
1z=   % change the word with first suggestion
:set spellsuggest=3  % to reduce the list of suggestion
<C-x><C-s>    % in insert mode, display a popup of suggestios

the g command


% on a current word
gd gD % go to variable definition
g*    % search this variable (starts immediately, use CTRL-O to go back, Cttrl-n/N like normal search). Note * alone search for the string (not the varaibel)
gf    % open file under cursor
% else
gv    % recalls previous selection !!!!!!!

Numbers !!


<C-A> %increment the number  (for visual mode, use scripts)
<C-X> %decrement the number
CTRL-R=12*12  % in insert mode will insert the result of the calculation

Recording


qa % record a macro in recording a
q  % stops recording
@a % repeat recording

Command and search history with vim like modifications


q/ or /Ctrl-f   % Seach history  -  Ctrl-f is called the cedit key
q: or /Ctrl-f   % Command history
Ctrl-c Ctrl-c   % exit the command history

% !!!! To just go back and forth in the list without displaying it use Ctrl-n and Ctrl-p, for instance %
:Ctrl-p  % use the privious command

Tags


% In a shell, one can generates a %table of contents% file containing all the tags in the files you've listed on the command. The name of this file is tags.  This file can be used by Vim to aide in the editing of a program. The command:
ctags *.c
% in vim
CTRL-]   % jump to definition even if in another file
CTRL-T   % Goes back (ctrl-o as well, no?)

Abbreviations


:ab #b /****************************************
:ab #e ^V^H*****************************************/
%These let use define boxed comments with ease. By typing %#b% we type the top of a boxed comment. Typing %#e% types the bottom line. (We put the ^V^H^V^H in the file to backup over the comment leader.)