Practical Vim Edit text at the speed of thought - xZixz/TDIL GitHub Wiki

Before June 26 2017

  • >G increases 1 indentation
  • A squashes $a into a single keystroke: to the end of the line then switch to INSERT MODE
  • C squashes c$
  • s squashes cl: clear the letter then switch to INSERT MODE
  • S squashes ^C: go to the head of the line, clear the line then switch to INSERT MODE
  • I squashes ^i: go to the head of the line, switch to INSERT MODE
  • o squashes A<CR>
  • O squashes ko: go to the upper line, insert an empty line then switch to INSERT MODE
  • ; repeats last f/F{char} command
  • , reverse of ;
  • t{char} like f{char} but the cursor stands before the matched character
  • * perform a search for the word under the cursor
  • daw delete the word where the cursor stands in, aw is the text object for selecting current word
  • w moves the cursor to the first character of a word
  • b is w in reversed direction
  • e moves the cursor the end character of a word
  • ge is e in reversed direction
  • <C-a> increases the nearest ahead number by 1(default)
  • <C-x> same as <C-a> but with subtraction
  • dap delete an entire paragraph, p-{motion} is for paragraph
  • gU{motion} uppercase - gUU short for uppercase a line
  • gu{motion} downcase -guu short for downcase a line
  • g~{motion} reverse the case - g~~ reverse case of a line
  • when an operator command is invoked (with no motion), it acts upon the current line
  • == autoindent
  • <C-o> switch to Insert Normal mode
  • <C-h> delete back one character
  • <C-w> delete back one word
  • <C-u> delete to start of line
  • t{char} motion - till character
  • <C-r>{num} - in INSERT MODE - paste the register{num}
  • <C-r>= - in INSERT MODE - type in the expression for expression register
  • <C-v>{code} <C-v>u{code} - in INSERT MODE - insert special character
  • ga- show the info of the character the cursor is standing on, including character code
  • <C-k>{char1}{char2} - insert the character using Digraph
  • r R - switch to Replace Mode, r replace one single character, R replace multiple characters, back to Normal Mode by pressing <Esc>
  • gR - switch to Virtual Replace Mode, which treats tab character as though it consisted of spaces.

July 5 2017

  • v enable character-wise Visual Mode
  • V enable line-wise Visual Mode
  • <C-v> enable block-wise Visual Mode
  • gv reselect the last visual selection
  • o in VISUAL MODE go to other end of highlighted text
  • it the motion to choose inside a tag

July 6 2017

  • {num}G go to the line num
  • :{num} Command Line Mode: the line to be chosen
  • :{start},{end} Command Line Mode: range of lines to be chosen
  • :. Command Line Mode: current line
  • :$ Command Line Mode: line at the at of the file
  • :% Command Line Mode: all the file
  • :'<,'> Command Line Mode: selected text of Visual Mode
  • :/{regex}/,/{regex}/ Command Line Mode: choose text bases on matched patterns
  • :{range}{+/-}{num} Command Line Mode: offset from the range
  • zz: Scroll the screen with the cursor in the middle of the screen

July 7 2017

  • :[range]t{address} copy a range to the address
  • :[range]m{addresss} move a range the address

July 8 2017

  • :[range]normal {Normal Mode Command} execute Normal Mode Command in Command Line Mode
  • <C-d> in Command Line Mode shows suggestions, to move around selections
  • <C-r><C-w> to put the current word at the cursor to Command Line Mode
  • Put set history=200 to vimrc file to increase the number of command to be saved in Vim history
  • <C-f> in Command Line Mode switch into command-line window
  • !{shell-command} execute shell command in Command Line Mode
  • % in command line is represents the current file name
  • :shell to open an interactive shell session, $exit to return back to Vim
  • Can use Ctrl-z and fg bash shell instead of above, it's easier
  • :read !{shell-command} output the result of the command to buffer
  • :write !{shell-command} use lines in buffer as argument of of the shell command
  • !{motion} to set a selection then jump into Command Line Mode with ! after the selection to prepare for :[range]!{shell-command}

July 9 2017

  • :ls shows all the buffers loaded
  • :bprev, :bnext, :bfirst, :blast, :b{num} navigate to a buffer, :bp, :bn, :bf, :bl are for short respectively
  • :{num}bd delete a specific buffer
  • [b, ]b, [B, ]B are the same as above respectively if use unimpaired plugin
  • <C-^> toggles between current buffer and last viewed buffer

July 10 2017

  • :args {arglist} put files in argument list
  • :args show argument lists
  • ** wildcard used for recursing down the subfolers
  • :args \command`output the command result toargs`
  • :e read file from disk back into the buffer
  • :qa close all windows
  • :wa write all modified buffers to disk
  • <C-w>s splits the current window horizontally
  • <C-w>v splits the current window vertically
  • sp {file} splits the current window horizontally, loads {file} into the new window
  • vsp {file} splits the current window vertically, loads {file} into the new window
  • <C-w>w cycles between windows
  • <C-w>{h,j,k,l} Focus the window with directions
  • cl or <C-w>c closes current active window
  • on or <C-w>o keeps only current window
  • <C-w>= equalizes width and height of all windows
  • <C-w>_ maximizes the height of current active window
  • <C-w>| maximizes the width of current active window
  • {n}<C-w>_ sets current active window height to n rows
  • {n}<C-w>| sets currents active window width to n columns
  • Watch screencast on Vimcasts.org: http://vimcasts.org/e/7 to know more about the possibilities of rearranging windows
  • :tabe {filename} opens file in a new tab
  • <C-w>T moves current window into a new tab
  • :tabc closes current tab
  • :tabo keep only current tab
  • :tabn {n} or {n}gt switches to tab page number n
  • :tabn or gt switches next tab page
  • :tabp or gT switches previous tab page
  • :tabmove {n} move current tab to the n position

July 11 2017

  • :pwd same usage as pwd in bash shell
  • %:h <Tab> in Command Line Mode is expanded to current relative buffer path
  • %% same effect as %:h <Tab> if set a cnoremap in .vimrc as in Vim-tips.md
  • :set path+={path} add more paths for :find, ** can be used for all subfolders
  • :find {filename} open file searched in paths set above
  • rails.vim is the plugin for RoR development: https://github.com/tpope/vim-rails
  • :e. opens file explorer for current working directory
  • :E opens file explorer for the directory of the active buffer
  • :Sexplore :Vexplore opens file explorer in a horizontal and vertical split window respectively
  • When writing a buffer to nonexistent directories (use :e {filename} to create a file), make the directory by :!mkdir -p %:h first, then :w
  • When editing a file, then save but you don't have permission (need root permission), user this command in Command Line Mode: :w !sudo tee % > /dev/null

July 12 2017

  • g{motion} applies motions on display lines, motions can be j, k, 0, ^
  • ^ goes to first non blank character of the line
  • W, B, E, gE same effects as w, b, e, ge but on WORD-wise, while the downcase ones effect on word-wise
  • t{char} places the cursor before the occurrence of the {char} - often used with d{motion} or c{motion}

July 13 2017

  • /{chars} searches for the chars, a special motion, is an exclusive motion - the character the cursor standing on is excluded from the operation
  • a, i in text object stands for all, inside of the range of selection using text object.
  • Coming after a, i in text object are ), {, ], >, \, ". They are the delimiters of the text object.
  • {a,i}t text object of data in a xml tag
  • {a,i}b same for {a,i})
  • {a,i}B same for {a,i}{
  • {a,i}w text object for current word
  • {a,i}W text object for current WORD
  • {a,i}s text object for current sentence
  • {a,i}p text object for current paragraph
  • m{a-zA-Z} issues a mark, lowercase marks used for local buffer, uppercase marks used for global.
  • '{mark} jumps to the marked position
  • `{mark} jumps to the marked line, stands the first non-blank character
  • `` positon before the last jump within current file
  • `. location of the last change
  • `^ location of the last insertion
  • `[ Start of last change or yank
  • `] End of last change or yank
  • `< Start of last visual selection
  • `> End of last visual selection
  • % jumps between sets of parentheses: (){}[]...
  • Use matchit.vim to have more matches using %, like def/end in Ruby
  • Use surround.vim (https://github.com/tpope/vim-surround) to:
    • S{parenthese}in Visual Mode wrap a selection in parenthese
    • cs{parenthese}{parenthese} stand cursor in side a block wraped by a pair of parentheses, replace by new pair or parenthese

July 14 2017

  • <C-o>, <C-i> jumps back and forth between two files
  • (, ) jumps to start of previous/next sentence
  • {, } jumps to start of previous/next paragraph
  • H, M, L jumps to top/middle/bottom of screen
  • gf jumps to the file name under the cursor
  • <C-]> jumps to definition of keyword under the cursor
  • '{mark}, `{mark} jumps to a mark
  • <C-i> and <Tab> are the very same, map one key will effect the other key

July 15 2017

  • g;, g, moves cursor back/forth to the last change in the change list
  • gi moves cursor back to the last INSERT MODE buffer and switchs to INSERT MODE
  • ?{pattern} searches pattern backward (reverse of /)
  • # is reverse of *
  • gf opens the file with the name under the cursor
  • :set suffixesadd+={filesuffix} to specify one of more file extensions for gf command
  • Installs bundler.vim(https://github.com/tpope/vim-bundler) for Ruby project paths
  • Installs vim-ruby(https://github.com/vim-ruby/vim-ruby) for for Ruby file-type which handles the setup of suffixesadd and path.
  • Mark mV for the ~/.vimrc file
  • Mark mM for the file we stand before travel far
  • Try to get into a habit of setting global mark before using any commands that interact with the quickfix list, and commands that interact with buffer and arguments lists

July 16 2017

  • xp this combo can swap position of two characters standing next to each other
  • ddp combo swaps two consecutive lines
  • yyp combo duplicates a line and place after current line
  • "{register} to choose a register to y - yank, d - cut, ... and p - put
  • "" is the unname register
  • "0 is the yank register that contains the text using y - yank
  • When use an uppercase for register name, it appends text to the specified register, not overwrite
  • "_ is the black hole register which returns nothing, which can be helpful for d command with the purpose of not overwrite the unname register
  • P puts the content of the register before the cursor
  • "+ register is synced with OS's clipboard, write data to it, data will go to OS's clipboard; get data from it, the data is from Os's clipboard
  • "* register is synced with X11 windowing system, its name is xterm_clipboard which can see its availability in :version with a + before its name, otherwise -
  • These are read-only registers whose values are set implicitly by Vim:
    • "% Name of the current file
    • "# Name of the alternate file
    • ". Last inserted text
    • ": Last Ex command
    • "/ Last search pattern
  • p a selected region in VISUAL MODE will replace the selected region with the content in register, also write selected region to unname register
  • Use <C-r>{register} in INSERT MODE to consistently p - puts the text in the register after the cursor position
  • gp and gP put the cursor at the end of pasted text instead of at the beginning as p and P do
  • When pasting the text using system paste command with Vim started with no setups (original state), Vim acts as though each character has been typed by hand. It may mess up the text in many ways, like if autoindent is on, each line of text may wander further and further to the right. It will not happen if we set paste ption.
  • We can toggle the paste option by set a key like <f5> to it by the Ex command: :set pastetoglle=<f5>
  • The best way is to avoid toggling paste option by using "+ register instead. Pasting from system clipboard can be done by "+p

July 17 2017 Fucking do nothing

July 18 2017 Another day fucking do nothing

July 19 2017 Another fucking day fucking do nothing

July 20 2017 YOU 'VE JUST DOMINATED THE FUCKING DO NOTHING DAYS STREAK

July 21 -> 23 2017 K. LET CELEBRATE A WEEK OF DO NOTHING, LOSER.

July 24 2017

  • q{register} records a macro, saves macro to {register}
  • @{register} executes the macro at {register}
  • @@ reexecutes the last most recent invoked macro
  • {num}@{register} executes the macro num times.
  • When a motion fails, like j at the last line of the file or n when already at the last matched word, the macro aborts. Take this as a feature, not a bug. Put a big number before {num}@{register} so that we do not need to care how many times the macro repeats but just know it's a number that can cover all the file.
  • You can record the {motion}. as a convenient macro for reapeating. The most nicely register to use for this purpose is q. It can be translated as qq to record, @q to repeat and 22@q to fast repeat with a big number.
  • When you use {num}@{register}, the macros are executed by sequence while using VISUAL MODE then apply the register, the macros are executed in parallel

July 25 2017

  • q{uppercased-register} to append keystrokes to register
  • :source or :so to source a .vim file
  • :argdo normal @{register} applies the macro to all files. When record the macro the first time on a buffer, resist the urge to save the file with :w for turning back the original state of the file (before we record the change) by using :e! (:edit!). Then the command to apply the macro to all files will not apply to the first buffer twice
  • To execute the macro on files in series, append :next command in the macro. Then use {big-enough-number}@{register}. In applying the macro on files, in most cases, it's better to apply in series so that you can identify the error quickly
  • wall: apply :w to all files
  • :let {variable}={value} assigns the value to the variable, then the variable's value can be insert to the document through expression register
  • :echo {variable} to print out the value of variable in COMMAND LINE MODE
  • :put {register} to print out the content of the register. It always prints the content below the cursor and in a new line
  • Macro in a register can be printed out, then editted and yanked back to the register.
  • Setting ignorecase for enabling Vim's search patterns case insensitivity
  • Enable smartcase will override ignorecase. When smartcase is set, if your search pattern got any uppercase character, your search pattern will be case sensitive, if your search pattern is all lowercase character, your search pattern will be case insensitive.
  • Appending \c, \C at anywhere of your search pattern will override the two options above. \c is for case insensitive, \C is for case sensitive
  • Start a search pattern with \v to treat regular expression almost same way as Perl, Python or Ruby which no need to escape special meaning character
  • \x in Vim regular expression is short for [0-9a-fA-F](hex character).

July 26 2017

  • \V - very nomagic- means that in the pattern after it, only the back slash has a special meaning
  • As a general rule, use \v for regular expression search, user \V for verbatim search
  • \_s is stand for space in Vim regex
  • < > in Vim regex are delemiters for word boundary. It matches the exact word of the pattern, not inside any longer word
  • To use parentheses in Vim regex without capturing it, add % before the parentheses
  • g* and g# perform searches like * and # but without word delimiters
  • zs and ze are used for "crop" the match from a pattern, zs stands for start, ze stands for end stands for start, ze stands for end
  • Even for \V search, there are characters that needs to escape: \, / - in forward search, ? - in backward search
  • In expression register prompt, we can use escape({string}, {char}) to programmatically escape the char. To serve the purpose above, we can use escape({register}, '/\') for forward searching, and escape({register}, '?\') for backward searching

July 27 2017

  • N traverse search in inversion of n
  • ? same function as / but backward
  • Vim uses the last search pattern if we do this /<CR> or ?<CR>
  • Disable wrapscan to search to the end of the document without wrapping around
  • To 'unset' an option, use :set no{option}, add no prefix to option or :set {option}!, append suffix ! to option
  • :noh in short of :nohlsearch will turn off hightlight temprorarily until the next search
  • <C-r><C-w> autocompletes the searc field using the remainder of the current preview match
  • :%s///gn to echo out the number of matches. %s actually is substitue but the option /n simply makes it ignore the replace and count only
  • Append /e after a pattern to put the cursor at the end of the match after each move - it's an search offset
  • q/ to open a buffer window of history of search patterns, you can use all the power of editing of Vim there

July 28 2017 - no progress this day

July 29 2017 - no progress this day too

July 30 2017 - no progress this fucking day too

July 31 2017

  • Install vim-visual-star-search: Github for using * to search for selected word

August 1 2017

  • :[range]s[ubstitute]/{pattern}/{string}/[flags] is the syntax of subtitute command
  • g flag is globally, causing it to change all matches within a line rather than a single occurence
  • c flag is confirm, to ask for rejecting each change
  • n flag is for showing the number of ocurrences
  • e flag is to silence the error when matching like Pattern not found. error
  • & flag tells Vim to reuse the flags from the previous substitute
  • Special characters used in search patterns:
    • \r carriage return
    • \t tab character
    • \\ single backslash
    • \1 first submatch
    • \2 second submatch (and so on, up to \9)
    • \0 entire matched pattern
    • & entire matched pattern
    • ~ string from previous invocation of :substitute
    • \= evaluate expression, use result as replacement
  • To search all file use %s and \g flag, % is the range for entire file
  • <C-e> scrolls the screen up
  • <C-y> scrolls the screen down
  • When use \c flag in substitution, Vim will ask us to respond to each of the matches:
    • y - substitute this match
    • n - skipp this match
    • q - quiting substituting
    • l - substitutes this match then quit
    • a - substitues this and any remaining matches

August 2 2017

  • <C-r>/ at the command line pastes the contents of the last search register if you want to complete the search command instead using blank search pattern for the latest search pattern.
  • In the replacement field, the \= item tells Vim to evaluate a Vim script expression. In Vim script, @{register} refer to content of a register.
  • :let @{register}= assign the {register} value
  • In combination, we can use this: :%s//\=@a/g to use the pattern in \ register for search pattern and content in a register as the replacement field
  • g& repeat the last search with % appending at the start, means repeat the last search across the entire file
  • gv reselects the last selected block, switch to Visual Mode
  • :& repeats the last substitute without flags
  • :&& makes a complete combo for redo a substitute command
  • Substitute command can be used do move submatches in match pattern using capture - \{num}
  • submatch({register}) returns the value of register in Vim script expression
  • Abolish.vim is a terrific plugin should read
  • :vimgrep is a built-in search engine in Vim, the output will go into a quickfix list, which can be viewed by :copen, usage: `:vimgrep {pattern} {file_range}
  • Install qargs.vim to use :Qargs to populate file names from :vimgrep command to argument list
  • Use :argdo to apply substitution on all file in argument list

August 3 2017 Fucking do nothing again - loser needs to double it tomorrow

August 4 5 6 2017 - you were forgiven

August 7 2017

  • :[range] global[!] /{pattern}/ [cmd] is the syntax of :global command
  • Default range of :global command is entire file (%)
  • :g is short for :global, :v is short for :vglobal which applies command the lines which do no match the pattern

August 8 2017

  • :g/{pattern}/[range][cmd] is the general form of the command of :g/{start}/ .,{finish} [cmd] which will apply the cmd for each block from {start} to {finish}
  • :g/{pattern}/sil - add sil as silent to mute all the messages echoed by cmd
  • :set tags? inspects where Vim looks for tags files
  • :!ctags -R generates tags file in Vim
  • Create a mapping for invoke ctags: :nnoremap <f5> :!ctags -R<CR>
  • :autocmd BufWritePost * call system("ctags -R") to setup autocommand that invokes ctags every time saving changes to a file
  • Also must visit Effortless Ctags with Git to set up hooks for post-commit, post-merge and post-checkout
  • <C-]> makes cursor jump from the keyword under the cursor to the definition
  • <C-t> back to the previous definition in tag history
  • g<C-]> shows a list of choices form the tag match list then enter the number(destination) to jump to the definition
  • :tnext, :tprev, :tfirst, :tlast for jumping around matching tags
  • ]t, [t are recommended to use than above, umimpaired.vim is needed
  • :tselect prompts user to choose an item for the tag match list
  • :tag {keyword} to jump to the first tag that matches
  • :tjump {keyword} prompts user to select from multiple matches for {keyword}
  • 2 above can use regular expression in {keyword}, and can use tab for autocompletion, sometime it's faster than moving the cursor to the text then use <C-]>
  • :pop reverses through the tag history
  • :tag advances through the tag history

August 9 2017 do nothing day AGAIN, you should be ashamed of yourself

August 10 2017

  • :make to compile C code using Vim Command Line, if there are errors, Vim will jump to the first error.
  • :make! will keep the cursor where it is, and not jump to the error
  • :cnext - :cn jump to next item in quickfix list
  • :cprev - :cp jump to previous item in quickfix list
  • :cfirst - jump to first item in quickfix list
  • :clast - jump to last item in quickfix list
  • :cnfile - jump to first item in next file
  • :cpfile - jump to last item in previous file
  • :cc N - jump to nth item
  • :copen - open quickfix window
  • :cclose - close quickfix window
  • :colder - revert to an older version of quick fix list
  • :cnewer - revert form and older version to a newer one

August 11 2017 August 12 2017 August 13 2017 August 14 2017

August 15 2017

  • :grep wraps grep of bash shell, write the result to the quickfix list
  • grepprg setting specifies what to run in the shell when Vim's :grep command is executed
  • grepformat setting tells Vim how to parse the output from the command in grepprg
  • vim-fugitve is a plug in with useful grep-like program
    • Example: :set grepprg=ack\ --nogroup\ --column\ $*, :set grepformat=%f:%l:%c:%m
  • vim[grep] uses the regular expression to search in the files
  • Leaving the search field of :vim[grep] empty will make the search matches every line in every file
  • <C-n> and <C-p> commands invoke generic keyword autocompletion
  • Autocompletion prefixed with <C-x>
    • <C-x><C-n> current buffer keywords
    • <C-x><C-i> included file keywords
    • <C-x><C-]> tags file keywords
    • <C-x><C-k> dictionary lookup
    • <C-x><C-l> whole line completion
    • <C-x><C-f> filename completion
    • <C-x><C-o> Omni-completion
  • While the auto-complete dropdown is being shown, these keystrokes can be used to interact with it
    • <C-n> uses the next match from the word list
    • <C-p> uses the previous match from the word list
    • <Down> selects the next match from the list
    • <Up> selects the previous match from the list
    • <C-y> accepts current selected match - only needed when using <Down>/<Up> to navigate
    • <C-e> reverts to originally typed text
    • <C-h>/<BS> deletes one character from the current match
    • <C-l> undoes <C-h>
    • <{char}> stops completion and insert {char}
    • <C-n><C-p> combo to open the auto-complete list

August 16 2017

  • complete option holds a comma-separated list of one-letter flags, whose presence enables scanning of a particular place. The default setting is complete=.,w,b,u,t,i
  • :set complete-=i to disable scanning of included files, += for enabling the flag
  • :set spell for flagging out bad spell words
  • [s jumps to previous spelling error
  • ]s jumps to next spelling error
  • z= open window which shows a list of spell suggestion
  • {num}z= to choose the numbered suggestion without showing window
  • :set spelllang=en_us will check spelling on American way. Ex: moustache will be flagged as misspelled
  • If the spelllang is not supported yet, Vim will offer to fetch and install it
  • zg add the word under the cursor to a spell file
  • zw remove the word under the cursor to a spell file
  • zug undoes zg or zw
  • Add file for spell check (add vocabulary), add setlocal spellfile+=~{filepath}
⚠️ **GitHub.com Fallback** ⚠️