Skip to content

Instantly share code, notes, and snippets.

@cirias
Last active February 15, 2016 09:56
Show Gist options
  • Save cirias/e425e8313e524c04e64f to your computer and use it in GitHub Desktop.
Save cirias/e425e8313e524c04e64f to your computer and use it in GitHub Desktop.
vim-tips

using vim with a file watcher

:set backupcopy=yes

You have to tell vim how it should save the file. On most systems it is set to "auto" letting vim decide the saving strategy. Sometimes it writes directly to the file, sometimes it renames the old file and write a new one. The latter one causes watching problems.

http://vimdoc.sourceforge.net/htmldoc/options.html#'backupcopy'

debug syntastic

  • Active / enabled checkers: :SyntasticInfo
  • Debugging information: :let g:syntastic_debug = 1, run the checker, run :mes
  • Debugging information with checker's output: :let g:syntastic_debug = 3, then same as above

Normal Mode

| <C-a> | | <C-x> |

Trigger
c
d
y
g~
gu
gU
>
<
=
!

Insert Mode

Trigger
<C-[>
<C-o>
<C-r>{rigister}
<C-r>={exp}<CR>
<C-v>{123}
<C-v>u{1234}
<C-v>{nondigit}
<C-k>{char1}{char2}

Visual Mode

gv reselects the last text that was selected in visual mode.

Keystrokes
o

Command-Line Mode

Command
:[range]delete [x]
:[range]yank [x]
:[line]pu [x]
:[range]copy {address}
:[range]move {address}
:[range]join
:[range]normal {commands}
:[range]substitute/{pattern}/{string}/[flags]
:[range]gobal/{pattern}/[cmd]
:!{cmd}
:[range]!{filter}
:read !{cmd}
:write !{cmd}
Keystrokes
<C-r>{rigister}
<C-r><C-w>
Symbol
1
$
0
.
'm
'<
'>
%

To change two vertically split windows to horizonally split

    ^Wt^WK

Horizontally to vertically:

    ^Wt^WH

where ^W means "hit Ctrl-W". Explanations:

    ^Wt     makes the first (topleft) window current
    ^WK     moves the current window to full-width at the very top
    ^WH     moves the current window to full-height at far left

see :help window-move-cursor :help window-moving


Reload current file config (.vimrc)

:so %

Setup tab in .vimrc

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab

Ctrl+] - go to definition Ctrl+T - Jump back from the definition. Ctrl+W Ctrl+] - Open the definition in a horizontal split

Add these lines in vimrc

map <C-> :tab split:exec("tag ".expand("")) map <A-]> :vsp :exec("tag ".expand(""))

Ctrl+\ - Open the definition in a new tab Alt+] - Open the definition in a vertical split

After the tags are generated. You can use the following keys to tag into and tag out of functions:

Ctrl+Left MouseClick - Go to definition Ctrl+Right MouseClick - Jump back from definition


Ctrl W gives you the "windows command mode", allowing the following modifiers:

Ctrl W + R - To rotate windows up/left.

Ctrl W + r - To rotate windows down/right.

You can also use the "windows command mode" with navigation keys to change a window's position:

Ctrl W + L - Move the current window to the "far right"

Ctrl W + H - Move the current window to the "far left"

Ctrl W + J - Move the current window to the "very bottom"

Ctrl W + K - Move the current window to the "very top"

Check out :help window-moving for more information


To turn off highlighting until the next search:

:noh

Or turn off highlighting completely:

set nohlsearch

Or, to toggle it:

set hlsearch!

nnoremap <F3> :set hlsearch!<CR>

To clear the last used search pattern:

:let @/ = ""

Replace multiple files

:args `ack -l pattern`
:argdo %s/pattern/replacement/gce | update
:windo %s/pattern/replace/ge

Copy to clipboard

vim-X11

.vimrc

set clipboard=unnamedplus

Record marco

To enter a macro, type:

q<letter><commands>q

To execute the macro <number> times (once by default), type:

<number>@<letter>

refreshing the file

:set autoread

vim key map
:imap jj <Esc>

:let mapleader = " "
:let maplocalleader = ","

foldlevel

fold and unfold in normal mode zo and zc change foldlevel zm, zr


Selected Start and End

`< and `>


Remove unwanted white space
:g/^$/d

:%s/\s\+$
:%s/^\s\+
Insert content
Use the :read command to insert a file, or the output from a system command, into the current buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment