User Tools

Site Tools


linux:vim

VIM Tips and Tricks

Subpages

plague-nvim

I have decided to check out Neo-Vim some time ago. After few days of using it, I have become totally converted to Neo-Vim. There is a nice project Plague-nvim which I can highly recommend.

Moving around

Basic

keys action
h,j,k,l move cursor left, down, up, right
w, b word forward/backward
0, $ move to the beginning and to the ending of the line
^ move to the first non-blank character of the line
gg Go to the top of the file
<n>gg Go to the <n> line of the file (25G - takes you to line 25th)
G Go to the bottom of the file
g<leader> Go to previously edited place
H, M, L move to top, middle and bottom of screen
^f, ^b full screen forward and screen backwards
^d, ^u half screen forward and half screen backwards
zt “zoom top” scrolls screen so the current line is on top
zb “zoom bottom” scrolls screen so the current line is at the bottom
zz “zoom” scrolls screen so the current line in in the middle

Edition

keys action
<n>x, <n>X delete <n> characters to the right or to the left of cursor
<n>dd delete <n> lines
d0, d$ delete to the beginning or to the end of the line
. (dot) repeat last command
/<text> find next occurrence of <text>
?<text> find previous occurrence of <text>
n or N find next or previous occurrence of <text>
* quick-search all occurrences word under the cursor
<n> delete
change
word
} paragraph
delete or change <n> words, paragraphs, etc

Advanced tricks

keys action
yt“ Copy (yank) till ”
ci“ Change inside ”“ (or (), {}, [], etc.) excluding ”
da“ Delete around ”“ (or (), {}, [], etc.) including ”
dj delete joined (current line and consecutive one)
vi“ Visual select everything inside ”“ string
:e! Reopen file. Forget any unsaved changes.
:%j Join all lines into a single line.
n » Indent n lines
n « Un-indent n lines
:%s/search/replace/g Global search and replace
:g/^$/d Delete all blank lines in a file.
:v/./d Delete all blank lines in a file (even with only spaces)
d/serach delete from curson to first result of the search
:!% Run the currently edited file. Never leave VIM when developing scripts!
:set ft=<file-type> sets the file-type of the current document. (eg.: “set ft=javascript”)

Buffers and windows

^wH moves current window left
^wJ moves current window down
^wK moves current window up
^wL moves current window right
^w<n>+ increses height by <n> rows
^w<n>- reduces height by <n> rows
^w<n>> ^w5> increses width by <n> (5) colums
^w<n>< ^w5< reduces width by <n> (5) colums
^w= splits equaly.
:ls lists opened buffers
:bs<buf> splits <buf> buffers

Folding

zf<motion> creates a fold in <motion> (eg. zf5j creates a fold of 5 lines down)
zf% creates a fold in matching pairs (), {}, [], etc.
zo fold open
zc fold close
zd fold delete
zi toggle folding
za toggle code folding at the current line.
:set fdm=<type> set folding-method <type>
Available folding methods: 
  manual    manual      Folds are created manually.
  indent    indent      Lines with equal indent form a fold.
  expr      expr        'foldexpr' gives the fold level of a line.
  marker    marker      Markers are used to specify folds.
  syntax    syntax      Syntax highlighting items specify folds.
  diff      diff        Fold text that is not changed.

Macros

q<reg> record macro, and store it in <reg>
@<reg> play macro stored in <reg>

Markers

m<reg> saves marker in <reg>
'<reg> moves cursor to the marker stored in <reg>
'' (single quote twice) moves the cursor to the previously modified line

Modes

keys action
i, a insert or append mode in the cursor possition
I, A insert or append mode but move to beginning of the line or the end of the line
O, o open a new line before or after the cursor and switch to insert mode
v change to visual block mode
ESC back to commands mode

Switches

:set hlsearch Highlight searches
:set nohlsearch Switch off highlight searches
:nohlsearch or :nohl Switch off temporarily
:set paste Copy/paste will not be chopped any more
:set tw=0 wrap linebreak Switch of the linebreak
:set encoding=utf-8 We really should use utf-8
:set mouse=a If you want to use a mouse
set directory=/<dir>/<sdir>/<ssdir>/ Makes vim keep its temporary files in /<dir>/<sdir>/<ssdir>/

Tips and Tricks

Comment out multiple lines

  • Change to Visual Block Mode (CTRL+v)
  • Highlight first column of the desired block (<arrows> or j, k)
  • Insert mode (Shift+I)
  • Comment character (eg. ”#“)
  • Hit ESC

or just use NERDCommenter and hit 10<leader>c<space> to comment 10 lines.

Append to the end of multiple lines

  • Change to Visual Block Mode (CTRL+v)
  • Highlight 1 column of the desired block (<arrows> or j, k)
  • Go to the end of line (Shift+$)
  • Append mode (Shift+A)
  • Comment comment (eg. ”# This is a comment“ )
  • Hit ESC

Join all lines into a single one

:%j!

References

linux/vim.txt · Last modified: 2016/08/24 03:01 by abadonna