Thursday, 1 December 2016

VIM Tips and Tricks

QT/KDE is dropping X11 support, so time for Vim

This will be a dynamic entry, to paste iteratively the tricks I found through the web ..

----

http://vim.wikia.com/wiki/Vim_Tips_Wiki
http://vim.wikia.com/wiki/Best_Vim_Tips
http://usevim.com/

###################################################################

:help!
:h command

Files
=====

q: quit current window (close split but not file)
wq: write and quit
CtrlR+% : paste filename in commandline
x: write and quit
:w filename #write copy to filename
:sav filename   #Save As
:e! #back to saved file
q! quit without saving; it quits an split

Ctrl+Z: suspend, execute whatever and then fg to comeback

:vi filename +line #open filename

:Ex #file exploreer

Edition
=======

i: insert mode
o: insert at new line
dd: CUT line
dw: delete rest of word
x: delete under cursor
J: delete newline character (join lines)

u : undo last command
. : repeat last command
Ctrl+R : Redo
:history

Copy/Paste
----------

v (select) + y (copy): copy to register
d #cut

Y #Copy line
P #paste

Ctrl'r'+0 #paste
C: Cut line (dd)

:reg   #Display registers
"1p    #paste first register

v: select text visually
y: copy to register

Buffers/Splits
==============

:ls # show buffers
:bn #next buffer
:bp #previous buffer
:b1 #first buffer ...
:bd #delete buffer, close file and all windows where appears

:tabs #list of tabs
:tabe #new tab page
:tabm n #move tab to position
:tabn / tabp #switch tab
:tabonly #close all other tabs
gt            go to next tab
gT            go to previous tab
{i}gt         go to tab in position i

:sp filename #open filename in a split
:vsp filename #vertical split

Ctrl+W R "Swap top/bottom or left/right split
Ctrl+W T "Break out current window into a new tabview
Ctrl+W o #"Close every window in the current tabview but the current one

Navigate splits
Ctrl+w k – top
Ctrl+w j – bottom
Ctrl+w l – left
Ctrl+w h – right
Ctrl+w | or _ : maximize horizontal/vertical

 :e filename      - edit another file
 :split filename  - split window and load another file
 ctrl-w up arrow  - move cursor up a window
 ctrl-w ctrl-w    - move cursor to another window (cycle)
 ctrl-w_          - maximize current window
 ctrl-w=          - make all equal size
 10 ctrl-w+       - increase window size by 10 lines
 :vsplit file     - vertical split
 :sview file      - same as split, but readonly
 :hide            - close current window
 :only            - keep only this window open
 :ls              - show current buffers
 :b 2             - open buffer #2 in this window

:q #exit window

Moving
======

h,j,k,l : cursor movement

vi +269  : open file in a given line

Esc++G : Jump to line number?
: <number> 

Ctrl+G: shows current line and file size

H: go to first line on screen
M: go to middle of screen
L: go to lower line
zz: center screen

f , t : Moving in the line

^, $ : begin/end of string
w, e: move by word begin/ends

Searches ======== :/ for searching n find next * find next occurr of word under cursor Ctrl+O, Ctrl+I jumping back to where you’ve been: set search options :set incsearch :set ignorecase smartcase TAGS ==== http://andrewradev.com/2011/06/08/vim-and-ctags/

To generate a tags file in the directory:

> ctags *py  #it will generate the tags file

To tell VIM to find tags file in the project:

> cat .vimrc

set tags=./tags;,tags;



:ts /  # it will load a list with all matching tags

:ts /<expr> # and select where you want to go

:ltag  # it will set that list as location list

:lopen # show location list

Using it:
    vim -t  to open vim straight on the tag
    Ctrl+] to jump to tag when over a word
    Ctrl+T to pop back
    :ta   go to a tag
    :ta /exp   go to tags containing exp (/) iterates all
    :tselect : show a list with all tags (Esc+ to choose one)
    :lt,:ltag / and then :lopen will show a navigation tab for tags
    :lcl closes the location list
    :stselect to open tag in a new split
    :tnext,:tn :tprev,:tp to go to next/prev tag finding
    :help tags for more 🙂
    :ltag 

Other alternatives like cscope/pycscope have more dependencies

To have ctag always by default:

Install ctags: aptitude install exuberant-ctags
~/.ctags:
  --python-kinds=-i
  --exclude=*/build/*

rebuild tags: ctags -R -o ~/mytags ~/src
~/.vimrc: :set tags=~/mytags


----

Indentation:
------------

>j #indent right
 and <
  re-indent: =

MISCELLANEOUS
=============

:syntax on

CtrlN : word completion
CtrlP : same, from fragment
CtrlD : for files in current directory
:e /tmp/"CtrlD" : will show files in /tmp/
CtrlX+CtrlL : LINE completion

Substitution : :%s/text1/text2/g
Asking to confirm : :%s/text1/text2/gc

See git diff with VIM!?

git difftool --tool=vimdiff --no-prompt filename.py

#Vim has a diff mode (vimdiff)
vim -d file1 file2 [file3 [file4]]



Launch Shell commands
---------------------

  !dir launches
  !.dir launches and pastes