VIM

:%!fold -w 60

Ex commands

delete all text

:%d

copy all text

:y+

paste all text

:put+

Pipe shell

https://superuser.com/questions/157987/pipe-output-of-shell-command-into-a-new-buffer-in-vim

Print context

%g/rule.*count/z#.3

Get the name of the current file

https://vim.fandom.com/wiki/Get_the_name_of_the_current_file

Search and replace

https://vim.fandom.com/wiki/Search_and_replace

Append output of an external command

https://vim.fandom.com/wiki/Append_output_of_an_external_command

https://www.youtube.com/watch?v=PUsHbsLqI00

What does `` mean?

https://vi.stackexchange.com/questions/2286/what-does-bar-mean

https://unix.stackexchange.com/questions/282366/how-to-invoke-vim-editor-and-pipe-output-to-bash

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

https://vim.fandom.com/wiki/Redirect_g_search_output

https://renenyffenegger.ch/notes/development/vim/registers/index

http://zzapper.co.uk/vimtips.html

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#

pipeline

: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
:g/word/,+1d

How to run a series of vim commands from command prompt

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

https://stackoverflow.com/questions/23235112/how-to-run-a-series-of-vim-commands-from-command-prompt

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

https://www.linux.com/training-tutorials/vim-tips-working-external-commands/

https://vimways.org/2019/vim-and-the-shell/

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

https://stackoverflow.com/questions/48145696/how-to-replace-only-selected-visual-block-not-the-line-in-vim https://vim.fandom.com/wiki/Applying_substitutes_to_a_visual_block

:'<,'>s/data/backup/g.
:'<,'>s/\%Vdata\%V/backup/g

\%V only matches within the current visual area.


Execute selection from script in Vim

https://stackoverflow.com/questions/40289706/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)

:bnext

run, last ex command

@:

repeat, last ex command

@@

https://vim.fandom.com/wiki/Repeat_last_change

Modifiers

Examples, when the file name is "src/version.c", current dir
"/home/mool/vim":
  :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
:ls
  1 #h   "geographicAreaM49Code.py"     line 16
  3 %a   "~/PycharmProjects/caterina/geographicAreaM49Label.py" 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

https://vi.stackexchange.com/questions/6746/how-can-i-open-a-buffer-in-a-new-tab-leaving-the-current-window-and-buffer-intac

Comment and uncomment lines with //

45,67s#^#//
45,67s#^//#

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

%s/\s\+$//e

Setting up Vim to yank to clipboard on Mac OS X

http://www.markcampbell.me/2016/04/12/setting-up-yank-to-clipboard-on-a-mac-with-vim.html

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

https://github.com/tmux/tmux/issues/543

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.

https://stackoverflow.com/questions/253380/how-do-i-insert-text-at-beginning-of-a-multi-line-selection-in-vi-vim

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

11,30norm! @a

https://stackoverflow.com/questions/390174/in-vim-how-do-i-apply-a-macro-to-a-set-of-lines

Highlight number of occurrences for selected text

Shift #

Multiple commands at once

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

http://vim.wikia.com/wiki/Multiple_commands_at_once

Macros

qx
@x

where x is a register identifier to callback the recorded macro

http://vim.wikia.com/wiki/Macros

Open a new Tab with netrw and a right side windows

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

Cheat sheet

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

File explorer

https://shapeshed.com/vim-netrw/

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!
  autocmd VimEnter * :Vexplore
augroup END

https://www.youtube.com/watch?v=XA2WjJbmmoM&t=2070s

Save sessions

https://bocoup.com/blog/sessions-the-vim-feature-you-probably-arent-using

: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
:q
:bd

keep open Only this window

ctrl + w + o

Switch between windows

ctrl + w + w

Tabs management

commands

: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

vsp

Explorer

Ex: /Library/Webserver/

To return to the explorer window, press

Ctrl-^ 

See old commands

q:
 :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

https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

https://chromatichq.com/blog/working-vim-never-leave-your-terminal

http://vim.wikia.com/wiki/Using_tab_pages

https://sanctum.geek.nz/arabesque/buffers-windows-tabs/

https://www.sourceallies.com/2009/11/vim-splits-an-introduction/

http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

Tags