:%!fold -w 60

Ex commands

delete all text


copy all text


paste all text


Pipe shell

Print context


Get the name of the current file

Search and replace

Append output of an external command

What does `` mean?

nmap r :w !ls  mvim -
nmap r :w !ls \| mvim -

Redirect g search output

command! -nargs=? Filter let @a='' | execute 'g//y A' | new | setlocal bt=nofile | put! a

open new file

vim -c enew

process content in another buffer

vnew | r #1 | % ! sort -u

read output from command

tabnew | r ! docker ps -a 2>&1

Vim: Close All Buffers But This One

:w | %bd | e#


:w ! sort | uniq | tee %
:w ! docker ps -a 2>&1 | tee %

load tmux buffer in a new window

vnew|r!tmux capture-pane -pS -32768 -t 2

How to delete all lines matching a pattern and a line after in Vim?

:g/word/normal 2dd

How to run a series of vim commands from command prompt

vim -c "source script.vim" A.txt

Concatenate vim commands

execute("%!cat -s")|1,24d|g/mappin/d|g/target/d|%s/source /select count(*) from (/g|%s/^$/) as t;/g|$d|norm!$a) as t;
  • It uses execute at the beginning to return implicitly and pass the result to next step in the pipeline since the command was external cat
  • the command $d has to executed almost at the end because otherwise the cursor does not come back to the beginning.
  • the last command of the pipeline insert a string ) as t

regular expressions

%s/mappingId \(.*\)/select '\1' as t;/g

Using Your Buffer as Input to a Command

:[range]write !{cmd}
'<,'>write !python

How to replace only selected visual block not the line in Vim


\%V only matches within the current visual area.

Execute selection from script in Vim

More Vim page up and page down keys

[Control][b] - Move back one full screen
[Control][f] - Move forward one full screen
[Control][d] - Move forward 1/2 screen
[Control][u] - Move back (up) 1/2 screen

redo next command

first ex command (can be tabn)


run, last ex command


repeat, last ex command



Examples, when the file name is "src/version.c", current dir
  :p            /home/mool/vim/src/version.c
  :p:.                     src/version.c
  :p:~               ~/vim/src/version.c
  :h                       src
  :p:h          /home/mool/vim/src
  :p:h:h        /home/mool/vim
  :t                       version.c
  :p:t                     version.c
  :r                       src/version
  :p:r          /home/mool/vim/src/version
  :t:r                     version
  :e                           c
  :s?version?main?             src/main.c
  :s?version?main?:p    /home/mool/vim/src/main.c
  :p:gs?/?\\?       \home\mool\vim\src\version.c

    %       current file name
    %<       current file name without extension
    #       alternate file name for current window
    #<       idem, without extension

Open buffer in a new tab

  • list buffers
  1 #h   ""     line 16
  3 %a   "~/PycharmProjects/caterina/" line 1
  • using command sb (split and show buffer)with the id number chosen from ls (autocomplete also works)
sb 1
tab sb 1
  • using command sp with the id number from ls (the symbol # has to be prepended to the id number)
sp #1
tab sp #1

Comment and uncomment lines with //


Display output of a command

: vnew | r! git status
: r! git push gh --all
: r! git --no-pager log --oneline

Get result of find command up in a quickfix

:cexpr system('find . -name 'foo.txt' -print')

Open a buffer as a vertical split in VIM

:vsp | b1

display hidden characters

:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
:set list

Remove unwanted spaces


Setting up Vim to yank to clipboard on Mac OS X

Here's what I did:

brew install reattach-to-user-namespace

Ensure the following is set in .tmux.conf:

set -g default-shell $SHELL 
set -g default-command "reattach-to-user-namespace -l ${SHELL}"

In .vimrc

set clipboard=unnamed

How do I insert text at beginning of a multi-line selection in vi/Vim?

Press Esc to enter 'command mode'
Use Ctrl+V to enter visual block mode
Move Up/Downto select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line.

how do I apply a macro to a set of lines?

11,30norm! @a

Highlight number of occurrences for selected text

Shift #

Multiple commands at once

%s/htm/html/c | %s/JPEG/jpg/c | %s/GIF/gif/c



where x is a register identifier to callback the recorded macro

Open a new Tab with netrw and a right side windows

tabe /home/asanchez75/docker/docker-elk-sides/ | vsp | vertical resize 40

Cheat sheet

File explorer

let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 25
augroup ProjectDrawer
  autocmd VimEnter * :Vexplore
augroup END

Save sessions

:mks ~/.vim/sessions/rooster.vim
:source ~/.vim/sessions/rooster.vim

Set working directory to the current file

To change to the directory of the currently open file (this sets the current directory for all windows in Vim):

:cd %:p:h

You can also change the directory only for the current window (each window has a local current directory that can be different from Vim's global current directory):

:lcd %:p:h

In these commands, % gives the name of the current file, %:p gives its full path, and %:p:h gives its directory (the "head" of the full path).

Windows management

split horizontal

ctrl + w + s

split vertical

ctrl + w + v

close windows

ctrl + w + c

keep open Only this window

ctrl + w + o

Switch between windows

ctrl + w + w

Tabs management


: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
:tabn {i}     go to the tab number {i}
:tabp         go to previous tab
:tabfirst     go to first tab
:tablast      go to last tab
:tabnew filename  new tab
:q              close tab

Current windows directory (horizontal windows)

Sex = sp

Vertical windows



Ex: /Library/Webserver/

To return to the explorer window, press


See old commands

 :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