(redirected from Tools.Vim)

donated the 07th of May 2010

From Mastering Vim Understanding Vim's Lesser-Known Features for More Effective Editing by Damian Conway, O'Reilly Media 2014

  • go through the keyboard and invoke :help key on keys you do not know
  • use regex for starting and ending range for s///
    • e.g. /function/return/s/this/that/
  • branching through undo/redo
    • g-/g+ to go through undo different timelines
    • earlier 10m/later 10s
  • q: and q/
    • command and search editable history, press Enter to apply it
  • completion
    • Ctrl-X Ctrl-D for filename completion from the current directory
    • set define=def then Ctrl-X Ctrl-D will complete with Python function names
  • vip visually select a paragraph
    • works with function if they are not separated by empty lines, can be used from anywhere in it

From personal use

  • syn sync fromstart to force color syntaxing from the beginning of the file
    • useful on long Javascript files mixed with HTML
  • ci"/ciw/ci(/dit replace text inside quotes, word, parenthesis, tag
  • visualmode:action
    • e.g. visualmode:s/this/that/ will apply the regex to the selected text (instead of finding line numbers)
  • <iB/>iB : indent blocks of code around
  • t/T/f/F jump to target on current line
    • overall can be faster than / or ? but limited to the current line
    • e.g. t g in set output "tunkranking.png" jumpg to the last n
  • cmd | vi - : pipe the result of the command to a vi instance
    • e.g. man screen | vi -
  • \r : create a newline in a regex
    • e.g. s/this that/this\rthat/
  • :recover : recover content stored in the then current path after a crash
    • works even with OS crash and with unsaved files
  • %s/\~min\(\d\+\) /[[http:\/\/www.youtube.com\/watch?v=XYZ#t=\1m|\~min\1]] / : change a YouTube video time to a proper link
    • a macro could store the URL in a register then use it in the regex instead of having to manually add it
    • not that the trailing space allows to re-apply the macro on pages that have already been transformed
  • g/^/m0 : reverse all lines of the current buffer
  • :help : the MOST important command ;)
  • :set syntax=pmwiki : (if auto detection doesn't work)
  • :buffers : list the opened buffer
  • [N]b : jump to one of them
  • :h backup
  • the substitute command can use complex ranges, not just :3,9s/A/B/ or :%/A/B/ but also
    • patterns :Chapter1,Chapter3s/A/B/
    • marks like :'m,'hs/A/B/
    • number of lines :.,+4s/A/B/@@
    • used example :?!!!Reading?,/!!!See/s/ \([0]-9\)\. /* [[#FECN_Chapter\1]]\1 / , see FECN
    • more generally, most commands support this type of motion (patterns, marks, line number)
  • folding
    • zfzj create a fold up to the next existing fold
    • make a register to fold a pmWiki section of text
      • qfzf/^[[q
      • position on the beginning of a section and create the folding @q
      • repeat for every section @@
      • using zo, zc, ... and 'm to navigate through the file
      • zC closes all the folds defined
  • recording
    • qx to start recording in register x, q to stop recording
    • @x to execute it and @@ to execute the last executed register
    • :reg[isters] list used registers
      • note : register are not limited to recording/macros ...but are very useful for them
    • "xp insert the content of register x (note that special characters will not appear properly here)
    • "xy yank the selected text in register x
    • see my macros
  • :read !ls ~ inserts the result of the command in the current buffer
  • K over a word look it up in the help (equivalent of :help keyword)
    • for example ins-completion
  • # over a word searches for it backward (n to find the next occurence)
    • * same but forward (and this time, the first occurence is the current word)
  • the CURRENT file is represented by %
    • example : !ls >> %
  • splits
    • ^w to manage windows
      • ^ws for normal split
      • ^wv for vertical split
      • ^wc to close the current split
      • ^wn for normal split with new file
      • ^wo to keep only the current split
      • ^m(motion) thus classicaly hjkl
  • c% change up to the matching parenthesis or bracket
  • marks
    • mx to mark the current position to x
    • 'x to jump to the marked position x
    • '. to jump to the last edition line
  • syntax file
  • make addition in regex (useful to update page numbers in a text)
    • :help sub-replace-special
      • basically s/\(x\)/\= and an expression
      • ex: s/\d\+/\=submatch(0)+1
    • 18,39s/(p\(\d\+\)/\="(p[[{$:book}".(submatch(1)+18)."|".submatch(1)."]]"

I wonder if it's possible to measure the knowledge of Vim by the proportion of time or command spend in insert mode. Suggesting that the more you use the insert mode uniquely to type, not even to insert text, the more "Vim proficient" you are.

my macros

  • s = €khwi{-A-}j = delete the current line excluding the first word, supposedly a list
  • p = o€#3:€ku€kd?^$?,joinI**€ü "A€kb" (p)'akjjma€@7ia€kh?(p[0-9]€kr€kryw'a€@7€klp = paste quote
    • append and format the clipboard content (several sentences, including multiple lines) as a quote after mark a
  • v = mh'vO€#3'h = pasting text in memory (clipboard) in a specific section (previously marked v) and going back (supposing mark h to be unused), practical to paste new vocabulary in reading notes
  • f = zf/[[€kb€kb* [[# = fold up to the next [[# pattern
  • i = ea''bbi'' = put the current word in italic, cursor can be placed anywhere but the last character (easier to use after searching up for the targeted word)
  • z = c/^$:newpgg/#ly/]:w Person.":q = split the current file with empty line separated blocks into individual files named by the first pmWiki anchor met

Most of those macros can be simplified by replacing motion followed by action by proper motion/action, for example yanking in p does not require to move, yank and go back, but directly to yank the right position.

From Vimcasts

From "7 Habits For Effective Text Editing 2.0"

Habit 1: Moving around quickly

  • text hilighting - Use * to highlight all instances of a word in the current buffer
  • folding

Habit 2: Don't type it twice

  • mode completion - Ctrl+N (even for a classical text)
  • see also omni-completion for context dependent analysis

Habit 3: Fix it when it's wrong

  • spell checker
  • use abbreviation with :iabbr Miskate Mistake

Habit 4: A file seldom comes alone

  • use ctags (mostly for code) tags and quickfix
  • gf Goto File (on header file names, even url of libraries in GreaseMonkey script for example)

Habit 5: Let's work together

  • other editor are NOT text editor, even Word, it's just to make a nice presentation of text.
  • type in Vim BUT remove linebreak then copy/paste

Habit 6: Text is structured

  • ... sth to add here!

Habit 7: Sharpen the saw

  • analysis of the last task at hand and optimize

From "VI - VIM editor tutorial (part 1 - part 2)"

  • 1,$s/text/replacement/
    • ScopeCommand/SearchRegexp/RegexpReplacement/
    • using :set nu to facilitate scope defining
  • A - append at the end of the line
  • . - repeat last modification
  • cw - change word

From ViEmu (a commercial product to integrate Vi with Microsoft tools)

From ViEmu Why, oh WHY, do those #?@! nutheads use vi?

  • on the importance of . and how it can be combined with any executed command
  • block manipulation
    • % to move from a within a structure like (lisp function) [wiki link] or {javascript body}
      • which can be combined with ... c or d to change c% or delete d% the whole structure !
    • di> to delete the content within an xml <> block
      • obviously which can be changed to ci] to modify a pmwiki link let's say

From Vim Regular Expressions 101

  • remembering that it's \# to call group # not $# !
  • Tab can be used inside a regexp too : ^I
  • [^ ]\+ to match a "Word"
  • 0,$s/\(!\+\)/\1/ to change all pmWiki titles to one specific color

From Vim Decision Science tutorial

  • ( begin sentence
  • ) end of sentence
  • das deletes current sentence
  • I inserts at beginning of the line

Ideas for improvements

  • dataset based completion
    • generate a file for each wiki based on every page name
      • ls wikiname/wiki.d/ | grep -v pmwiki. > ~/vim_vocabularies/wikiname

To explore


Note

My notes on Tools gather what I know or want to know. Consequently they are not and will never be complete references. For this, official manuals and online communities provide much better answers.