Skip to content

Instantly share code, notes, and snippets.

@henriquemenezes
Last active February 18, 2020 04:22
Show Gist options
  • Save henriquemenezes/60950d8c5a5887cf2d82 to your computer and use it in GitHub Desktop.
Save henriquemenezes/60950d8c5a5887cf2d82 to your computer and use it in GitHub Desktop.
Vim

Vimtutor:

Tips & Tricks

	:source $MYVIMRC	Reload .vimrc without restart vim, after reload run :e
	:e 			Reload buffer and trigger FileType event

Modes:

	ESC 	: Normal Mode
	 i  	: Insert Mode
	 v  	: Visual Mode
	 V  	: linewise Visual Mode
	 ctrl+v : blockwise Visual Mode

Normal Mode Commands:

  • Exit
	:q           quit
	:q!          quit without save
	:qa          quit all
	:qa!         quit all without save
  • Save
	:w           save a file
	:w filename  save a file with filename
	:wq          save a file and quit
	:wqa         save all files and quit
	:up          save a file if there are unsaved changes
	:x           save a file if there are unsaved changes and quit
	:xa          save all modified buffers and quit
  • Edit
	:e filename  edit a file
	:e! filename edit a file and discard current unsaved changes
  • Switching between files
	$ vim f1 f2  edit multiple files

	:n           go to the next file
	:next        go to the next file
	
	:N           go to the previous file
	:previous    go to the previous file
	
	:first       go to the first file
	:last        go to the last file
  • Switching between buffers
	:e f1
	:e f2        edit multiple buffers
	
	:bn          go to the next buffer
	:bnext       go to the next buffer
	
	:bp          go to the previous buffer
	:bprevious   go to the previous buffer
	
	:bfirst      go to the first buffer
	:last        go to the last buffer
  • Buffers
	:ls		list all buffers
	
	:bN		go to buffer number N	
	:b N		go to buffer number N
	:buffer N	go to buffer number N
	
	:b <tab>	select the filename and go to buffer of file
	
	ctrl+^		switch to the previous edited buffer
	ctrl+6		switch to the previous edited buffer
	
  • Motion
	w            move to next word
	2w           move two words forward
	e            move to end of word
	3e           move to end of third word
	0            move to the start of the line
  • Delete
	x            delete character under the cursor
	dw           delete until next word
	de           delete until end of current word
	d$           delete until the end of line
	d2w          delete two words forward
	dd           delete the line
	2dd          delete two lines
  • Undo
	u            undo last command
	U            undo all commands a whole line
	ctrl+r       redo the commands (undo the undo's)
  • Paste/Put and Replace character
	p            put previously deleted text after the cursor
	rx           replace the character at the cursor with 'x'
  • Change
	ce           change until the end of word (deletes word and place in Insert Mode)
	c$           change until the end of line (deletes until eol and place in Insert Mode)
  • Change to Insert Mode
	i            change to insert mode under the cursor (Insert)
	I            change to insert mode at the first non white character on the line
	o            open a new line below the current line and enter insert mode
	O            open a new line above the current line and enter insert mode 
	a            aove point forward by one character and then enter insert mode (append)
	A            change to insert mode to end of line   (Append)
  • Cursor Location and File Status
	ctrl+g       show your location in the file and the file status.
	g ctrl+g     show other file status
	G            move to the bottom of the file
	gg           move to the start of the file
  • Copy, cut and paste

  • Copying and cutting in normal mode

	yy	  	yank the current line, including the newline character at the end of the line
	Y		yank the current line, including the newline character at the end of the line	

Pasting in normal mode

	p		paste after the cursor
	P		paste before the cursor
  • Copy, cut, and paste from the system clipboard
	"+y		copy to clipboard (Visual mode)
  • Search
	/text<enter>       search in forward direction for the phrase 'text'
	/\ctext<enter>     search in forward direction for the phrase 'text' and ignore case
	?text<enter>       search in backword direction for the phrase 'text'
	?\ctext<enter>     search in backword direction for the phrase 'text' and ignore case
	n                  search again
	N                  search again in opposite direction
	crtl+o             go back where you came
	ctrl+i             goes forward
	:set ignorecase    ignores case sensitive on / search
	:set noignorecase  no ignores case sensitive on / search
  • Match parentheses search
	move to any (,),[,],{ or } and type:
	%            move the cursor to the other matching bracket
  • Substitute
	:s/old/new      substitute 'old' for 'new' in the first occurrence in the line
	:s/old/new/g    substitute globaly in the line
	:%s/old/new/g   substitute every occurrence in the whole file
	:%s/old/new/gc  prompt every occurrence in the whole file to substitute or not
	:#,#s/old/new/g substitute globaly occurence between lines two lines '#'
  • Execute and external command
	:!cmd           execute 'cmd' command at the shell
  • Using Windows:
	ctrl+w w        move cursor to another window (cycle)
        ctrl+w ctrl+w   move cursor to another window (cycle)
  • Using Tabs:
	gt              go to next tab
	gT              go to previous tab
	{i}gt           go to tab in position i

	:tabe {file}    edit specified file in a new tab
	:tabedit {file} edit specified file in a new tab
	
	:tabclose       close current tab
	:tabclose {i}   close i-th tab
	
	:tabonly        close all other tabs (show only the current tab)
	
	:tabs           list all tabs including their displayed windows
	
	:tabm 0         move current tab to first
	:tabm           move current tab to last
	:tabm {i}       move current tab to position i+1
	
	:tabn           go to next tab
	:tabp           go to previous tab
	
	:tabfirst       go to first tab
	:tablast        go to last tab
  • NERDTree
	ctrl+n          open NERDTree
	
	o               open file in prev window or open & close directory node
	go              preview file (open file but leave cursor in the NERDTree)

	t               open file in new tab
	T               open file in new tab silently (keep the focus on the current tab)

	i               open file in a split window
	gi              preview file in a split window (leave the cursor on the NERDTree)
	
	O               open directory node recursively
	
	R		refresh current root
	
	m               display NERD tree menu

NERDTree Menu

        a       (a)dd a childnode
        m       (m)ove the current node
        d       (d)elete the current node
        c       (c)opy the current node
        l       (l)ist the current node

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment