Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active February 23, 2019 08:48
Show Gist options
  • Save tarasowski/af9ded4cd13ee30ab458089e3d411047 to your computer and use it in GitHub Desktop.
Save tarasowski/af9ded4cd13ee30ab458089e3d411047 to your computer and use it in GitHub Desktop.
Vim commands
## Some other goodies
# :e<space><CR> -> will reload the file you are working in.
# vmap -> is a mapping just for visual mode. That particular mapping is only valid in visual mode.
# nmap -> is a mapping for normal mode. Only available in normal mode. Recursive. It runs the mapped command from vim and the new command that is assigned together.
# nnoremap -> disables recursive key mapping for that map.
## Key comamnds
# <BS> Backspace
# <CR> Enter
# <Enter> Enter
# <Return> Enter
# <Esc> Escape
# <Space> Space
# <Up> Up arrow
# <Down> Down arrow
# <Left> Left arrow
# <Right> Right arrow
# <Insert> Insert
# <Del> Delete
# <Home> Home
# <End> End
# <PageUp> Page-Up
# <PageDown> Page-Down
# <Tab> Tab
# <bar> '| '
# <C-X> Ctrl-X
# <F1>-<F12> Function Keys
## some terminal goodies
# ls vim* -> only lists everything that starts with vim in the directory.
## register
# "acw -> puts the word in the register
# "ap -> paste the word from the register "a
## repetition
# 80i*<ESC> -> will insert 80 astericst into the line you are on
# 5o#<ESC> -> will create 5 new lines with #
## clipboard
# :%y+ -> copies all the file content to the clipboard
## multiple files
# vi file1.js file2.js file3.js -> opens all files in the buffers
# :buffers -> displays all open buffers in vim. A buffers is a representation of a file in-memory, if you quit without saving, nothing happens to the file.
# :ls -> does also shows the open buffers
# :b1 or :b2 or :b3 -> to switch the buffers b1 or b2 or b3 etc.
# :b<space> -> and type tab so you'll see all open buffer names, so you can switch between buffers.
# :bn! -> show next buffer (fore mode)
# :e<space><TAB> -> shows all the files in the current directory you can open
# :bd2 -> deletes the second buffer
# :bd -> move to the buffer and type :bd to delete the current buffer
# :on -> closes all the windows on the screen and make only the active window visible
## multiple screens
# :sp -> split screen command
# :sp<SPACE>b<TAB> -> shows all the files that start with b
# :sp<SPACE> -> shows all the files in the current directory
# :vs -> split screen vertically, you can use the same commands as with :sp
## window auto-resizing
### add this to ~/.vimrc
#--- mouse support ---
# set mouse=a
#--- automatic resizing ---
# set winheight=30
# set winminheight=5
## visual mode
# v -> enters the visual mode character wise
# V -> enters the visual mode line wise
# ^v -> enters the visual mode block wise
# d -> highlight the text on the visual mode and type d to delte highlighted text
# 23ggV -> d -> go to line 23 and enter the line wise visual mode, hightlight the text you want to delete -> press 'd'
# ^vjjj//<CR> -> puts you in a block wise visual mode, goes 3 lines down and adds # to each line
## textobjects
# {operator}{a}{object} -> daw -> delete a word (deletes everything around including delimeter and whitespaces)
# {operator}{i}{object} -> ciw -> change inner word (deletes only the object you are currenly in)
# das -> to delete a sentence including the whitespace
# dap -> to delete the entire paragraph
# ci[ or ci] -> const colors = ['white', 'red', 'green'] -> changes everything inside the bracket you get const color = []
# ca[ or ca] -> to delete the whole bracket, you'll end up with const colors =
# cit -> if you have <p>Some text here to change</p> becomes -> <p></p>
# ci{ or ci } -> if you have const c = {a: 'a'} -> const c = {}
# ci" -> if you have a string const s = "Hello World" -> const s = ""
## search & replace
# ff -> searches for the word that starts with f --> IMPORTANT: it searches only on the same line not in the whole file
# FF -> searches backwards for the word that starts with f
# * -> after you found the word, you can type asterics to find the next appearance of the word in the file
# /and -> searches for the word 'and' in the text -> typing `n` searches forwards, `N` searches backwards
# :%s/sat/laid/g<CR> -> looks in the whole file for the word sat and replaces it with laid
# :find filename -> to find and open a file from the working directory
# R -> will overwrite the existing text
# r -> will only overwrite one character
# cw -> change word c = operator w = motion
# c$ -> removes everything from the current position till the end of the line
# C -> does the same line c$
# cc -> changes the whole line.
## terminal mode
# :ter
# ^d -> exit terminal mode
### add this to ~/.vimrc
# set splitbelow -> add this to ~/.vimrc in order to get the split below and not above
## some other important things
# ^(carrot) is the same as Ctrl
## shebang in vim !
# q! -> to force an action -> force quit
# ! -> to execute commands e.g. terminal
# :set ruler! -> to toggle a command
## navigation
# * -> goes down the file line by line
## Thinking in vim
## operator{motion}
# dw -> deletes a word operator{motion}
## d -> the delete operation
## w -> the word motions
# dl -> deletes everything to left -> operator{motion}
## d -> does to delete it cuts the text and it's stored in the register!!!
## d -> the delete operation
## l -> the left motion
# dj -> deletes the current line and the line below -> operator{motion}
## d -> the delete operation
## j -> the move line below operation
## [count]operator{motion}
# 5dw -> 5 delte words
## Switching between screens
# C-w C-w -> Control w Control w -> switches between screens
## Help
# :h :q C-d -> will list all commands with :q
## Terminal commands
# source .vimrc -> reads all the commands from the .virmc file
# jobs -l -> lists all jobs
# kill %n -> kills the job that is running
# vi filename -> opens up a file in vi
# ctrl + Z -> stops the current job
# fg %n -> resumes the job
# ps -a -> show all current proceses
# fg -> opens up the last job
## Vi commands
# R -> go to the line/word you want replace and type R, after that everything that you type gets preplaced!
# r -> if you only want to replace only one word
# cw -> change word, move to the word you want to change. This is called command{motion}
# c$ -> change from the cursor till the end of the line.
# C -> going to replace everything from the cursor till the end of the line.
# cc -> replaces the whole line -> !!!! -> very useful.
# F[letter] -> type fa it searches backwards for a letter.
# f[letter] -> type fa it goes to the next a letter in the current line
# df( -> deletes the text that is inside parenthesis (x). The parenthesis and x will be deleted here.
# 80i*<CR> -> that will create a line with 80 asterics *******etc.
# 5o#<CR> -> creates 5 lines below with # quick way to create comments section
# z<CR> -> moves the text to top of the screen
# "bp -> if you want to paste a text from the "b register
# "byy -> puts a yanked line into the register under "b
# ^Jwordword -> ^J represents a new line in Vim
# :reg -> shows the register y, c, s, x, d
## -> note "" contains last operated on location
# 2dd -> delete 2 lines
# yy -> yanks the entire line
# 2wy -> copy 2 words [count]operation
# yw -> copy word
# 35gg -> move to line 35
# w -> saves your current file your are working on
# . -> repeats the previous command e.g. dd and . will always delete the current line
# dd -> delete the current line
# dw -> delete a word
# X -> deletes everything before the cursor
# 0 -> to jump to the beginning of the line
# $ -> to jump to the end of the line
# x -> deletes the character at the cursor position
# :set ruler -> to see the columns
# :set ruler! -> to toggle the ruler
# w -> move a word forward
# b -> move a word backwards
# gg -> go up to the beginning of the page
# G -> go to the bottom of the file
# ^ -> goes to the the beginning of the line
# :wq -> write & quit -> to save a file and leave vim
# :e filename.js -> opens up the file in vim
# :x -> saves the file
# :q! -> exits the file without saving
# :w !node % -> executes javascript in vim. Here % means run this file.
# % -> refers to particular file
# :w -> save file
# :wq -> save and quit
# :! -> appending the bang `!` means you are running the command in the terminal. If you say bang `!` something, vim will be backgrounded and that command will be ran.
# :! echo "foo bar" -> it will ran the command in the terminal. So after bang `!` you can run bash commands, it has nothing to do with vim
# :w filename.js -> saves the current content in the specified filename.
# shift + v -> highlights the line
# d -> deletes the highlightet line
# u -> undo last change
# ctrl + r -> redo changes which were undone
# y -> to copy highlights
# p -> pastes after the cursor position
# P -> pastes before the cursor position
# :map mu VdkP -> maps `,l` to highlight the stroke, deletes it, go one line up and paste it
# :map ,l :!clear && node %<cr> -> map this combination to file execution in the terminal. Here `<cr>` means like hitting Enter so the command is executed automatically.
# :map ,l -> if you want to see what is currently mapped to ,l - it doesn't run the mapped command, it just shows what is currently mapped to it.
# :map ,. || :map ,m || :map ,l -> often used for mappings, because the keys are near to itself on the keyboard
# I -> insert of the beginning of the line
# a -> insert the text after the cursor.
# o -> open a new line below where the cursor is
# O -> open a new line above the cursor
# :split filename -> opens the file and splits the window
# :%w !pbcopy -> copy the whole file
# d$ -> deletes the text from cursor to the end of the line
# :map ,l :w!<CR>:!node %<CR> -> runs the file in the terminal
# Unmap the arrow keys so you get used to the vim navigation
# vi ~/.vimrc
# map <up> <nop>
# map <down> <nop>
# map <left> <nop>
# map <right> <nop>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment