Vim commands

In notebook:
Article Notes
Created at:
2018-04-05
Updated:
2018-10-12
Tags:

Vim commands

#Learn Vim Progressively i → Insert mode. Type ESC to return to Normal mode x → Delete the char under the cursor
:wq → Save and Quit (:w save, :q quit)
dd → Delete (and copy) the current line
p → Paste

instead of writing Ctrl-λ, I’ll write <C-λ>.

Inserting

a → insert after the cursor
o → insert a new line after the current one
O → insert a new line before the current one
cw → replace from the cursor to the end of the word

Basic moves

0 → go to the first column
^ → go to the first non-blank character of the line
$ → go to the end of line
g_ → go to the last non-blank character of line
/pattern → search for pattern

Copy/Paste

P → paste before, remember p is paste after current position.
yy → copy the current line, easier but equivalent to

Undo/Redo

u → undo
<C-r> → redo

Load/Save/Quit/Change file (buffer)

:e <path/to/file> → open
:w → save
:saveas <path/to/file> → save to <path/to/file>
:x, ZZ or :wq → save and quit (:x only save if necessary)
:q! → quit without saving, also: :qa! to quit even if there are modified hidden buffers.
:bn (resp. :bp) → show next (resp. previous) file (buffer)


Repeating commands

. → (dot) will repeat the last command, N<command> → will repeat the command N times.

More moves

NG → Go to line N gg → shortcut for 1G - go to the start of the file G → Go to last line

To jump to the beginning of a C code block (while, switch, if etc), use the [{ command.

To jump to the end of a C code block (while, switch, if etc), use the ]} command.

The above two commands will work from anywhere inside the code block.

To jump to the beginning of a parenthesis use the [( command.

To jump to the end of a parenthesis use the ]) command.

Word moves

w → go to the start of the following word, e → go to the end of this word. b → go to the beginning of this word

W → go to the start of the following WORD, E → go to the end of this WORD.

% : Go to the corresponding (, {, [. * (resp. #) : go to next (resp. previous) occurrence of the word under the cursor

Combining moves with commands

<start position><command><end position>

ye → yank from here to the end of the word y2/foo → yank from here to the second occurrence of the word "foo"

Repeat the last movent (f,t,F,T)

; and , (backwards)

Move on current line

0 → go to column 0 ^ → go to first character on the line $ → go to the last column g_ → go to the last character on the line fa → go to next occurrence of the letter a on the line. , (resp. ;) will find the next (resp. previous) occurrence. t, → go to just before the character ,. 3fa → find the 3rd occurrence of a on this line. F and T → like f and t but backward.

Zone selection

<action>a<object> and <action>i<object>

Where action can be any action, for example, d (delete), y (yank), v (select in visual mode). The object can be: w a word, W a WORD (extended word), s a sentence, p a paragraph. But also, natural character such as ", ', ), }, ].

Suppose the cursor is on the first o of (map (+) ("foo")).

vi" → will select foo. va" → will select "foo". vi) → will select "foo". va) → will select ("foo"). v2i) → will select map (+) ("foo") v2a) → will select (map (+) ("foo"))

Select rectangular blocks

<C-v>

Typically: 0<C-v><C-d>I-- [ESC]

^ → go to the first non-blank character of the line <C-v> → Start block selection <C-d> → move down (could also be jjj or %, etc…) I-- [ESC] → write -- to comment each line

Macros : qa do something q, @a, @@

qaYp<C-a>qqa start recording. Yp duplicate this line. <C-a|x>increment the number. q stop recording. @a → write 2 under the 1 @@ → write 3 under the 2 Now do 100@@ will create a list of increasing numbers until 103.

Visual selection: v,V,<C-v>

We saw an example with <C-v>. There is also v and V. Once the selection has been made, you can:

J → join all the lines together. < (resp. >) → indent to the left (resp. to the right). = → auto indent

Splits: :split and vsplit.

:split → create a split (:vsplit create a vertical split) <C-w><dir>: where dir is any of hjkl or ←↓↑→ to change the split. <C-w>_ (resp. <C-w>|) : maximise the size of the split (resp. vertical split) <C-w>+ (resp. <C-w>-) : Grow (resp. shrink) split

To change two vertically split windows to horizonally split

<Ctrl-w t> <Ctrl-w K>

Horizontally to vertically:

<Ctrl-w t> <Ctrl-w H>

Explanations:

Ctrl-w t makes the first (topleft) window current Ctrl-w K moves the current window to full-width at the very top Ctrl-w H moves the current window to full-height at far left

https://stackoverflow.com/questions/1269603/to-switch-from-vertical-split-to-horizontal-split-fast-in-vim


Scrolling stack overflow

zz - move current line to the middle of the screen (Careful with zz, if you happen to have Caps Lock on accidentally, you will save and exit vim!) zt - move current line to the top of the screen zb - move current line to the bottom of the screen

Ctrl-y Moves screen up one line Ctrl-e Moves screen down one line Ctrl-u Moves cursor & screen up ½ page Ctrl-d Moves cursor & screen down ½ page Ctrl-b Moves screen up one page, cursor to last line Ctrl-f Moves screen down one page, cursor to first line

Ctrl-y and Ctrl-e only change the cursor position if it would be moved off screen.

Substack github

moving around

You can move all kinds of places quickly in command mode:

  • ^ or 0 - move to the start of the current line
  • $ - move to the end of the current line
  • gg - jump to the beginning of the file
  • G - jump to the end of the file

Delete

  • dG - delete from the current position to the end of the file
  • dgg - delete from the current position to the start of the file
  • dj - delete the current line and the line below
  • dk - delete the current line and the line above
  • 2dd, 3dd etc - delete the next N lines

Even dl and dh work!

Searching

  • /PATTERN - search forward for PATTERN
  • ?PATTERN - search backward for PATTERN

To clear the results

Press:

  • n - jump to the next match
  • N - jump to the previous match

You can combine searching with deleting too:

  • d/PATTERN - delete to the next match of PATTERN
  • d?PATTERN - delete to the previous match of PATTERN
  • dn - delete to the next already matched pattern
  • dN - delete to the previous already matched pattern

search and replace

:s/PATTERN/REPLACEMENT/FLAGS

Try these on a line with the string cats:

:s/cat/dog/
:s/cat/dog/g
:s/cat/dog/i

replace everything

:%s/cat/dog/ig

Replaces "cat" with dog everywhere in the entire file, case insensitively.


regex flags

  • i - case insensitive
  • g - global replace (per line)

visual select

Press v to go into visual select mode. Move the cursor around to select text.

Once you've selected a block, you can press:

  • y - "yank" the text into the paste buffer
  • x or d - delete the selected text
  • >> - indent the text right by shiftwidth
  • << - indent the text left by shiftwidth

paste

Once you've populated the paste buffer by yanking or deleting, press p to paste.


visual modes

  • v - select by characters
  • V - select by lines
  • ctrl-v - select in a block

fancy odds and ends

  • J - move the next line to the end of the current line
  • (backtick)+. - jump to the last edit

insert a file

You can insert a file at the cursor position with:

:r otherfile.txt

insert with a command in place

You can insert the output of a command at the cursor position with :r!.

For example, to insert the output of the pwd command:

:r!pwd

.vimrc

  • autoindento
  • expandtab
  • tabstop
  • shiftwidth (sw)

my vimrc:

https://gist.github.com/substack/7745bb6ff9ad58d4805d


Jumping to previous cursor locations

Fandom wiki

  • Ctrl-O to jump back to the previous (older) location.
  • Ctrl-I (same as Tab) to jump forward to the next (newer) location.

Code folding

stack overflow

set foldmethod=syntax|indent That's it. The most useful commands for working with folds are:

  • z o opens a fold at the cursor.
  • z Shift+o opens all folds at the cursor.
  • z c closes a fold at the cursor.
  • z m increases the foldlevel by one.
  • z Shift+m closes all open folds.
  • z r decreases the foldlevel by one.
  • z Shift+r decreases the foldlevel to zero -- all folds will be open.

via (https://www.linux.com/learn/tutorials/442438-vim-tips-folding-fun)

  • zf#j creates a fold from the cursor down # lines.
  • zf/string creates a fold from the cursor to string .
  • zj moves the cursor to the next fold.
  • zk moves the cursor to the previous fold.
  • zo opens a fold at the cursor.
  • zO opens all folds at the cursor.
  • zm increases the foldlevel by one.
  • zM closes all open folds.
  • zr decreases the foldlevel by one.
  • zR decreases the foldlevel to zero -- all folds will be open.
  • zd deletes the fold at the cursor.
  • zE deletes all folds.
  • [z move to start of open fold.
  • ]z move to end of open fold.

Viewports

linux.com

Vim viewport keybinding quick reference :sp will split the Vim window horizontally. Can be written out entirely as :split.

:vsp will split the Vim window vertically. Can be written out as :vsplit.

Ctrl-w Ctrl-w moves between Vim viewports.

Ctrl-w j moves one viewport down.

Ctrl-w k moves one viewport up.

Ctrl-w h moves one viewport to the left.

Ctrl-w l moves one viewport to the right.

Ctrl-w = tells Vim to resize viewports to be of equal size.

Ctrl-w - reduce active viewport by one line.

Ctrl-w + increase active viewport by one line.

CTRL-W > and CTRL-W < for horizontal resize (vertical split)

to make the window wider or narrower.

Ctrl-w q will close the active window.

Ctrl-w r will rotate windows to the right.

Ctrl-w R will rotate windows to the left.

Youtube video

<c-w><c-s> split horizontally

:tabnew new tab

Map resizing to shortcut

nmap + <c-w>+ nmap - <c-w>-


Open a file:

:sp filename

To set the division

:10 sp filename


Get path of current file

stack overflow

In insert mode Ctrl+R %

or Try this:

:let @" = expand("%") this will copy the file name to the unamed register, then you can use good old 'p' to paste it. and of course you can map this to a key for quicker use.

:nmap cp :let @" = expand("%")

Use the contents of the register

<C-r>" → this will insert the contents of the unnamed register fandom

To open a split with the file

The you can type: :sp <C-r>"

To map: :nmap cr :sp <C-r>"

This is of course related to Oni Vim: easily open a file with the fuzzy finder, copy it's path, move to other file and split the window.


Increment a number

<C-a|x>

Tabs (Oni Vim)

Oni wiki: Features

Oni features a buffer tab bar, like many common IDEs. VIM has its own definition of a "Tab", which is really a set of windows and buffers. By default, the tabs in Oni correspond to vim-defined tabs. You can override this, and show open files (buffers), by setting the tabs.mode setting to buffers

Commands

  • With tabs.mode set to tabs, you can use gt and gT to cycle through tabs
  • With tabs.mode set to buffers, [b and ]b will cycle through buffers, which has the effect of moving through tabs.

Options

  • tabs.mode - If set to tabs, shows vim tabs. If set to buffers, shows open buffers. If set to native show the native vim tab bar. Finally, if set to hidden, show no tab bar at all. (Default: tabs. Requires Neovim 0.2.1+)

Open in split

<C-v> (vertical split) <C-s> (horizontal split)

Search in open buffers

<C-/> it's almost like jump to symbol

Vim surround

GitHub

Finally, let's try out visual mode. Press a capital V (for linewise visual mode) followed by S<p class="important">.

<p class="important">
  <em>Hello</em> world!
</p>

Now press cs'<q> to change it to

<q>Hello world!</q>

To go full circle, press cst" to get

"Hello world!"

To remove the delimiters entirely, press ds".

Hello world!

Now with the cursor on "Hello", press ysiw] (iw is a text object).

[Hello] world!

Let's make that braces and add some space (use } instead of { for no space): cs]{

{ Hello } world!

Now wrap the entire line in parentheses with yssb or yss).

({ Hello } world!)

Revert to the original text: ds{ds)

Hello world!

Emphasize hello: ysiw<em>

<em>Hello</em> world!

Change an HTML tag: cst<p (leave the <p open to not override the other attributes)

Emmet

github

<c-y>,

Move between insertion points: C-y n|N


Installing plugins

Plugin repo and docs

It looks like he is using vim-plug which is a plugin manager. You install it by sticking the plug.vim file you can see here (https://github.com/junegunn/vim-plug) into ~/.config/nvim/autoload/ (you may need to make that autoload folder.) Then its just a case of following the instructions on that page, add the following to your init.vim with your plugins between: ❤1

call plug#begin('~/config/.nvim/plugged')

...

call plug#end()

:PlugInstall installs, :PlugUpgrade upgrades vim-plug, :PlugUpdate to update your plugins, and :PlugClean to delete any orphaned ones.

  Plug 'othree/yajs.vim'
  Plug 'othree/javascript-libraries-syntax.vim'
  Plug 'othree/es.next.syntax.vim'
  Plug 'othree/html5.vim'
  Plug 'othree/csscomplete.vim'
  Plug 'othree/jsdoc-syntax.vim'

Terminal

CTRL + R to search previous commands or

!! to replay previous command

If using bash, you can put bind Space:magic-space into ~/.bashrc, more: stackexchange

Vim folding commands

vim-tips-folding-fun

zf#j creates a fold from the cursor down # lines. zf/string creates a fold from the cursor to string. zj moves the cursor to the next fold. zk moves the cursor to the previous fold. zo opens a fold at the cursor. zO opens all folds at the cursor. zm increases the foldlevel by one. zM closes all open folds. zr decreases the foldlevel by one. zR decreases the foldlevel to zero -- all folds will be open. zd deletes the fold at the cursor. zE deletes all folds. [z move to start of open fold. ]z move to end of open fold.

More Oni Vim

go to file navigator (<C-w><C-h>)

  • C-f new folder
  • C-e new file
  • r to rename the file
  • d to delete the file
  • u to undo
  • y to yank, thenk p to paste the file into another folder
  • <C-r> refresh file tree

Rename HTML tag

use vim-surround. ex in normal mode : <div>te<CARRET>st</div> cst<p

Note: don't close the tag (<p>) if you don't want to lose the existing attribuntes

More moving and scrolling

Moving to the Top

Press H (“high”) to move the cursor to the top of the screen.

Moving to the Middle

Press M (“middle”) to move the cursor to the middle of the screen.

Moving to the Bottom

Press L (“low”) to move the cursor to the bottom of the screen.

Page Forward One Screen

To scroll forward (move down) one screenful, press Ctrl-F. (Hold down the Control key and press the F key.) The cursor moves to the upper left corner of the new screen.

Scroll Forward One-Half Screen

To scroll forward one half of a screen, press Ctrl-D.

Page Backward One Screen

To scroll backward (that is., move up) one screenful, press Ctrl-B.

Scroll Backward One-Half Screen

To scroll backward one half of a screen, press Ctrl-U.

##Sessions

:mksession!

:source Session.vim