[Home] [Articles, Categories, Tags] [Books, Quotes]
Practical Vim 2E
Author:
Pub Year:
Source:
Read: 2018-06-01
Last Update: 2018-06-01

Five Sentence Abstract:

This is meant to be picked at, not read cover to cover (I read it cover to cover... and I agree, not all at once). It is set up in a less typical cookbook style, where each tips is grouped with other tips that can be construed in the same vein, but the tips are meaty enough to go into some detail while still providing the curious reader a jumping off point for deeper understanding. The examples are nice, not too contrived, and apparently available on either the website of github. Overall I say this book is highly recommended, no matter what level your vim proficiency might be at. The only word of warning, avoid the vimcasts; the audio is horrible.

Thoughts:

Right off the bat Drew tells you not to read the book linearly, to pick at it here and there. I read it linearly. It is set up like a cookbook, which I normally don't like at all, but there was enough elaboration with each tip and the similar tips were grouped together. Not a particularly easy task since there are so many ways you might get to the same solution with vim, none being the 'right' way.

Though I didn't take the advice and read it cover to cover, I did do it slowly, over about two weeks. I tried to pick a few things that I wanted to integrate and made a point of consciously using them for a few days; many of the habits stuck. This is the best way to learn vim, bit by bit. Learn one new thing a day, or a week, and, eventually, you'll have attained mastery, or at least competence.

On top of this strategy, I also have a list of all the stuff I'd eventually like to integrate as second nature. This prevents me from ever getting to the point where I become comfortable without having to be constantly looking for new tips. This book would be great to poke around in, there will always be something new, or at least will lead you to a more advanced understanding of something old, but I rather prefer my making one pass and compiling a TODO list. I'll probably go through the book again though, maybe even before my list is empty.

The other key thing to realize is, reading alone is not enough. You get good at vim the same way you get to Carnegie Hall; practice, practice, practice! You should always try out new and interesting ways of doing things. By having a scratch file to work with, which apparently are available online to follow the book exactly, though I simply made up my own examples, you can fiddle with new commands without wrecking a real project.

All in all, when you hear people online saying that this is THE vim book, I think they are right. This book is highly recommended; I was very pleased with it.

My list of random stuff I want to learn:

1
2
3
4
5
Keystrokes | Effect
-------------------
`<C-h>`    | Delete back one character (backspace)
`<C-w>`    | Delete back one word
`<C-u>`    | Delete back to start of line
1
2
3
Keystrokes | Effect
-----------+-------
`<C-o>`    | Switch to Insert Normal mode

:2,$!sort -t',' -k2

1
2
3
4
5
6
Keystrokes Current   | Keystrokes Current
---------------------+-------------------
`iw`       word      | `aw`       word plus space(s)           
`iW`       WORD      | `aW`       WORD plus space(s)           
`is`       sentence  | `as`       sentence plus space(s)       
`ip`       paragraph | `ap`       paragraph plus blank line(s) 
1
2
3
4
5
6
7
8
9
Keystrokes | Buffer Contents
---------- +-------------------------------------------
`“`        | Position before the last jump within current file
`‘.`       | Location of last change
`‘^`       | Location of last insertion
`‘[`       | Start of last change or yank
`‘]`       | End of last change or yank
`‘<`       | Start of last visual selection
`‘>`       | End of last visual selection
1
2
3
set nocompatible
filetype plugin on
runtime macros/matchit.vim
1
2
3
4
5
Keystrokes | Buffer Contents
-----------+----------------------------------------
`{start}`  | cities = ["London", "Berlin", New York]
`vee`      | cities = ["London", "Berlin", New York]
`S"`       | cities = ["London", "Berlin", "New York"]
1
2
3
4
5
Keystrokes              | Buffer Contents
------------------------+-----------------------------------------
`{start}`               | Match "quoted words"---not quote marks. 
`/\v"[^"]+" <CR>`       | Match "quoted words"---not quote marks. 
`/\v"\zs[^"]+\ze" <CR>` | Match "quoted words"---not quote marks. 
1
2
3
4
5
6
7
8
Command | Effect
--------+----------------------------------------------------
`n`     | Jump to next match, preserving direction and offset
`N`     | Jump to previous match, preserving direction and offset
`/<CR>` | Jump forward to next match of same pattern
`?<CR>` | Jump backward to previous match of same pattern
`gn`    | Enable character-wise Visual mode and select next search match
`gN`    | Enable character-wise Visual mode and select previous search match
1
2
3
:reg a
--- Registers ---
 "a
1
2
3
4
:g/TODO/yank A
:reg a
"a // TODO: Cache this regexp for certain depths.
// TODO: No matching end code found - warn!

Exceptional Excerpts:

Table 2. Vim’s Operator Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Trigger | Effect
----------------
`c`     | Change
`d`     | Delete
`y`     | Yank into register
`g~`    | Swap case
`gu`    | Make lowercase
`gU`    | Make uppercase
`>`     | Shift right
`<`     | Shift left
`=`     | Autoindent
`!`     | Filter {motion} lines through an external program 



If you’re curious about how to create your own custom operators, start by reading :help map-operator

If you’re curious about how to create your own custom motions, start by reading :h omap-info

From insert mode, command line, or bash:

1
2
3
4
5
Keystrokes | Effect
-----------+-------
`<C-h>`    | Delete back one character (backspace)
`<C-w>`    | Delete back one word
`<C-u>`    | Delete back to start of line



The expression register is addressed by the = symbol. From Insert mode we can access it by typing &lt;C-r&gt;=. This opens a prompt at the bottom of the screen where we can type the expression that we want to evaluate. When done, we hit &lt;CR&gt;, and Vim inserts the result at our current position in the document.

The effect of the :write !sh command is that each line of the current buffer is executed in the shell. Refer to :h rename-files

The basic pattern uses a common regex idiom: '[^']+' . The pattern begins and ends with a quote mark and then matches one or more characters in between that are anything but a quote.

Notes:

Table of Contents

Introduction
01: The Vim Way
02: Normal Mode
03: Insert Mode
04: Visual Mode
05: Command-Line Mode
06: Manage Multiple Files
07: Open Files and Save Them to Disk
08: Navigate Inside Files with Motions
09: Navigate Between Files with Jumps
10: Copy and Paste
11: Macros
12: Matching Patterns and Literals
13: Search
14: Substitution
15: Global Commands
16: Index and Navigate Source Code with ctags
17:Compile Code and Navigate Errors with the Quickfix List
18: Search Project-Wide with grep, vimgrep, and Others
19: Dial X for Autocompletion
20: Find and Fix Typos with Vim’s Spell Checker
21: Now What?
Appendix 1

Introduction

01: The Vim Way

page 39: - the >G command increases the indentation from the current line until the end of the file.

page 44:

1
2
3
4
5
6
7
8
con+cat+these+with+spaces

,f+s + <Esc>   # I've remapped f to ,f
;.             # so I can use f for easymotion without leader
;.
;.

con + cat + these + with + spaces

page 46: - Perform substitution :s/target/replacement

02: Normal Mode

page 60: - 10<C-a> adds ten to the current/next number in line.

page 61: - Number Formats

page 64:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Trigger | Effect
----------------
`c`     | Change
`d`     | Delete
`y`     | Yank into register
`g~`    | Swap case
`gu`    | Make lowercase
`gU`    | Make uppercase
`>`     | Shift right
`<`     | Shift left
`=`     | Autoindent
`!`     | Filter {motion} lines through an external program 

page 65: - when an operator command is invoked in duplicate, it acts upon the current line. So dd deletes the current line, while >> indents it. The gU command is a special case. We can make it act upon the current line by running either gUgU or the shorthand gUU.

03: Insert Mode

page 68:

1
2
3
4
5
Keystrokes | Effect
-----------+-------
`<C-h>`    | Delete back one character (backspace)
`<C-w>`    | Delete back one word
`<C-u>`    | Delete back to start of line
1
2
3
4
5
Keystrokes | Effect
-----------+-------
`<Esc>`    | Switch to Normal mode
`<C-[>`    | Switch to Normal mode
`<C-o>`    | Switch to Insert Normal mode

page 71: - In Insert mode, we can press <C-r>0 to paste the text that we just yanked at the current cursor position.

page 73:

page 74: - We can tell Vim to insert any arbitrary character if we know its numeric code. From Insert mode, we just have to type <C-v>{code}, where {code} is the address of the character that we want to insert. Vim expects the numeric code to consist of three digits. Suppose, for example, that we wanted to insert an uppercase “A” character. The character code is 65, so we would have to enter it as <C- v>065.

If you want to find out the numeric code for any character in your document, just place the cursor on it and trigger the ga command.

page 75: - Table 3. Inserting Unusual Characters

1
2
3
4
5
6
Keystrokes            | Effect
----------------------+-------
`<C-v>{123}`          | Insert character by decimal code
`<C-v>u{1234}`        | Insert character by hexadecimal code
`<C-v>{nondigit}`     | Insert nondigit literally
`<C-k>{char1}{char2}` | Insert character represented by {char1}{char2} digraph

page 76: - :h digraph-table

04: Visual Mode

page 82: - Visual modes:

1
2
3
4
5
6
Command | Effect
--------+-------
`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

page 83:

1
2
3
Command | Effect
--------+-------
`o`     | Go to other end of highlighted text

page 88: - If we want to set up the dot command so that it repeats something useful, then we’re better off staying out of Visual mode. As a general rule, we should prefer operator commands over their Visual mode equivalents when working through a repetitive set of changes.

page 90: - yyp then Vr{char} to underline a line to the exact length

page 93: - Append to end of ragged lines:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Keystrokes   | Buffer Contents
-------------+-----------------------
{start}      | var foo = 1
Normal mode  | var bar = 'a'
             | var foobar = foo + bar
-------------+-----------------------
<C-v>jj$     | var foo = 1
Visual-Block | var bar = a
             | var foobar = foo + bar
-------------+-----------------------
A;           | var foo = 1;
Insert mode  | var bar = 'a' 
             | var foobar = foo + bar
-------------+-----------------------
<Esc>        | var foo = 1;
Normal mode  | var bar = 'a';
             | var foobar = foo + bar;
-------------+-----------------------

05: Command-Line Mode

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
:[range]delete [x]
Delete specified lines [into register x]

:[range]yank [x]
Yank specified lines [into register x]

:[line]put [x]
Put the text from register x after the specified line

:[range]copy {address}
Copy the specified lines to below the line specified by {address}

:[range]move {address}
Move the specified lines to below the line specified by {address}

:[range]join
Join the specified lines

:[range]normal {commands}
Execute Normal mode {commands} on each specified line

:[range]substitute/{pattern}/{string}/[flags]
Replace occurrences of {pattern} with {string} on each specified line

:[range]global/{pattern}/[cmd]
Execute the Ex command [cmd] on all specified lines where the {pattern} matches

page 99: - :{number} = jumps to line number (useful to get to bugs in stack traces)

page 100: - :{range} = :{start},{end} = do something over the range

page 101: - If we press the : key now [after visually selecting], the command-line prompt will be prepopulated with the range :’<,’> . It looks cryptic, but you can think of it simply as a range standing for the visual selection.

1
2
3
4
5
6
= > :/<html>/,/<\/html>/p

< = 2 <html>
    3 <head><title>Practical Vim</title></head>
    4 <body><h1>Practical Vim</h1></body>
    5 </html>

page 105: - So we can read the :6copy. command as “Make a copy of line 6 and put it below the current line.”

1
2
3
4
5
6
7
Command    | Effect
-----------+-------
`:6t.`     | Copy line 6 to just below the current line                    
`:t6`      | Copy the current line to just below line 6                    
`:t.`      | Duplicate the current line (similar to Normal mode yyp )      
`:t$`      | Copy the current line to the end of the file                  
`:’<,’>t0` | Copy the visually selected lines to the start of the file     

page 106: - Repeating the last Ex command is as easy as pressing @:, then @@ for more repeats

page 108: - The :’<,’>normal . command can be read as follows: “For each line in the visual selection, execute the Normal mode . command.”

page 110: - :help @

page 117: - Press q: and meet the command-line window (see cmdwin)

page 118: - What if halfway through composing an Ex command at the prompt, we realize that we need more editing power? In Command-Line mode, we can use the mapping to switch to the command- line window, preserving a copy of the command that was typed at the prompt. This table summarizes a few of the methods for summoning the command-line window:

1
2
3
4
5
Command  | Action
---------+-------
`q/`     | Open the command-line window with history of searches
`q:`     | Open the command-line window with history of Ex commands
`ctrl-f` | Switch from Command-Line mode to the command-line window

page 119: - Vim’s :shell command to start an interactive shell session (see :shell

page 120: - Suppose that we’re running Vim inside a bash shell and we want to execute a series of shell commands. Pressing Ctrl - z suspends the process that’s running Vim and returns control to bash. The Vim process sits idle in the background, allowing us to interact with our bash session as normal. We can inspect the list of jobs by running this command:

1
2
=> $ jobs
<= [1]+ Stopped     vim

page 121:

1
2
3
4
first name,last name,email
john,smith,john@example.com
drew,neil,drew@vimcasts.org
jane,doe,jane@example.com

:2,$!sort -t',' -k2

1
2
3
4
first name,last name,email
jane,doe,jane@example.com
drew,neil,drew@vimcasts.org
john,smith,john@example.com

page 122: - The !{motion} operator command drops us into Command-Line mode and prepopulates the [range] with the lines covered by the specified {motion} (see !). For example, if we place our cursor on line 2 and then invoke !G, Vim opens a prompt with the :.,$! range set up for us.

1
2
3
4
5
6
7
Command                | Effect
-----------------------+-------------------------------------------------------
`:shell`               | Start a shell (return to Vim by typing exit)
`:!{cmd}`              | Execute {cmd} with the shell
`:read !{cmd}`         | Execute {cmd} in the shell and insert its standard output below the cursor
`:[range]write !{cmd}` | Execute {cmd} in the shell with [range] lines as standard input
`:[range]!{filter}`    | Filter the specified [range] through external program {filter}

page 124: - batch.vim

1
2
3
4
5
global/href/join
vglobal/ href/delete
%normal A: http:/ /vimcasts.org
%normal yi"$p
%substitute/\v^[^\>]+\>\s//g

page 125: :argdo source batch.vim

06: Manage Multiple Files

page 140:

1
2
3
4
Ex Command | Normal Command Effect
-----------+----------------------
`:clo[se]` | `<C-w>c` Close the active window
`:on[ly]`  | `<C-w>o` Keep only the active window, closing all others

page 142: - The :lcd {path} command lets us set the working directory locally for the current window.

1
2
3
4
5
6
Command                 | Effect
----------------------- +-----------------------------
`:tabe[dit] {filename}` | Open {filename} in a new tab                      
`<C-w>T`                | Move the current window into its own tab          
`:tabc[lose]`           | Close the current tab page and all of its windows 
`:tabo[nly]`            | Keep the active tab page, closing all others      

07: Open Files and Save Them to Disk

page 155: - save a read only file as root:

1
2
3
4
5
= > : w !sudo tee % > /dev/null
< = Password:
    W12: Warning: File "hosts" has changed and the buffer was
    changed in Vim as well
    [O]k, (L)oad File, Load (A)ll, (I)gnore All:

08: Navigate Inside Files with Motions

page 158: - :h motions

page 163: - Word motions:

1
2
3
4
5
6
Command | Move Cursor
--------+------------
`w`     | Forward to start of next word
`b`     | Backward to start of current/previous word
`e`     | Forward to end of current/next word
`ge`    | Backward to end of previous word

page 172:

1
2
3
4
5
6
Keystrokes   | Buffer Contents
-------------+-----------------
{start}      | This phrase [t]akes time but 
             | eventually gets to the point.
             |
`d /ge <CR>` | This phrase [g]ets to the point. 

page 177: - text objects:

1
2
3
4
5
6
Keystrokes Current   | Keystrokes Current
---------------------+-------------------
`iw`       word      | `aw`       word plus space(s)           
`iW`       WORD      | `aW`       WORD plus space(s)           
`is`       sentence  | `as`       sentence plus space(s)       
`ip`       paragraph | `ap`       paragraph plus blank line(s) 

page 178: - As a general rule, we could say that the d{motion} command tends to work well with aw, as, and ap, whereas the c{motion} command works better with iw and similar.

page 179: - Lowercase marks are local to each individual buffer, whereas uppercase marks are globally accessible.

page 180: - Table 10. Vim’s Automatic Marks

1
2
3
4
5
6
7
8
9
Keystrokes | Buffer Contents
---------- +-------------------------------------------
`“`        | Position before the last jump within current file
`‘.`       | Location of last change
`‘^`       | Location of last insertion
`‘[`       | Start of last change or yank
`‘]`       | End of last change or yank
`‘<`       | Start of last visual selection
`‘>`       | End of last visual selection

page 181: - The % command lets us jump between opening and closing sets of parentheses (see %). It works with (), {}, and [], <>

page 182:

1
2
3
4
5
6
7
8
Keystrokes | Buffer Contents
---------- +------------------------------------
`{start}`  | cities = %w{London Berlin New\ York}
`dt{`      | cities = {London Berlin New\ York}
`%`        | cities = {London Berlin New\ York}
`r]`       | cities = {London Berlin New\ York]
````       | cities = {London Berlin New\ York]
`r[`       | cities = [London Berlin New\ York]

page 183: - Even though matchit ships with the Vim distribution, it’s not enabled by default. This minimal vimrc would make Vim autoload the matchit plugin on startup:

1
2
3
set nocompatible
filetype plugin on
runtime macros/matchit.vim
1
2
3
4
5
Keystrokes | Buffer Contents
-----------+----------------------------------------
`{start}`  | cities = ["London", "Berlin", @N@ew York]
`vee`      | cities = ["London", "Berlin", New Yor@k@]
`S"`       | cities = ["London", "Berlin", "New York"]

09: Navigate Between Files with Jumps

page 190: - We can inspect the value of the path by running this command:

1
2
= > :set path?
< = path=.,/usr/include,,

10: Copy and Paste

page 201: - In addition to the Normal mode commands, Vim also provides Ex commands for delete, yank, and put operations. We could cut the current line into register c by running :delete c, and then we could paste it below the current line with the :put c command.

page 202: - When we use the y{motion} command, the specified text is copied not only into the unnamed register but also into the yank register, which is addressed by the 0 symbol (see quote0). As the name suggests, the yank register is set only when we use the y{motion} command.

page 203: - When we address a named register with a lowercase letter, it overwrites the specified register, whereas when we use an uppercase letter, it appends to the specified register.

page 204: - The System Clipboard ( "+) and Selection ("*) Registers

1
2
3
4
Keystrokes | Buffer Contents
-----------+----------------
`"+`       | The X11 clipboard, used with cut, copy, and paste
`"*`       | The X11 primary, used with middle mouse button

page 205:

1
2
3
4
5
6
7
Register | Contents
---------+---------
`"%`     | Name of the current file
`"#`     | Name of the alternate file
`".`     | Last inserted text
`":`     | Last Ex command
`"/`     | Last search pattern

11: Macros

page 236: - counting with simple vimscript

1
2
3
4
5
6
7
Keystrokes Buffer        | Contents
-------------------------+------------------
`:let i=1`               | [p]artridge in a pear tree    
`qa`                     | [p]artridge in a pear tree    
`I<C-r>= i <CR> )<Esc>`  | 1)[ ]partridge in a pear tree 
`:let i += 1`            | 1)[ ]partridge in a pear tree 
`q`                      | 1)[ ]partridge in a pear tree 

12: Matching Patterns and Literals

page 252: - We can reference the captured text as \1 . If our pattern contained more than one set of parentheses, then we could reference the submatch for each pair of () by using \1 , \2, and so on, up to \9. The \0 item always refers to the entire match, whether or not parentheses were used in the pattern.

page 256: - Matching within delimiters:

1
2
3
4
5
Keystrokes              | Buffer Contents
------------------------+-----------------------------------------
`{start}`               | Match "quoted words"---not quote marks. 
`/\v"[^"]+" <CR>`       | Match "quoted words"---not quote marks. 
`/\v"\zs[^"]+\ze" <CR>` | Match "quoted words"---not quote marks. 

page 259: - Escaping characters by hand is laborious, error-prone work. Fortunately, Vim script includes a library function that can do the hard work for us: escape({string}, {chars}) (see escape() i). The {chars} argument specifies which characters must be escaped with a backslash. If we’re searching forward, we could call escape(@u, ’/\’), which would prefix each / and \ character with a backslash. If we were searching backward, we could instead call escape(@u, ’?\’).

1
=> escape(@u, getcmdtype().'\')

13: Search

page 263: - Searching:

1
2
3
4
5
6
7
8
Command | Effect
--------+----------------------------------------------------
`n`     | Jump to next match, preserving direction and offset
`N`     | Jump to previous match, preserving direction and offset
`/<CR>` | Jump forward to next match of same pattern
`?<CR>` | Jump backward to previous match of same pattern
`gn`    | Enable character-wise Visual mode and select next search match
`gN`    | Enable character-wise Visual mode and select previous search match

page 268: - after pressing n a couple of times, we realize that we’d prefer to place the cursor at the end of the match. That’s no problem: we could simply run //e<CR> . When we leave the search field blank like this, Vim reuses the pattern from the previous search. So this repeats the last search but with an offset.

page 273: - q/ brings up command mode with search history

1
2
3
4
5
6
7
8
9
xnoremap * :<C-u>call <SID>VSetSearch(' /')<CR>/<C-R>=@/<CR><CR>
xnoremap # :<C-u>call <SID>VSetSearch(' ?')<CR>?<C-R>=@/<CR><CR>

function! s:VSetSearch(cmdtype)
  let temp = @s
  norm! gv"sy
  let @/ = '\V' . substitute(escape(@s, a:cmdtype.' \'), '\ n ' , '\\n ' , 'g' )
  let @s = temp
endfunction

14: Substitution

page 281: - The n flag suppresses the usual substitute behavior, causing the command to report the number of occurrences that would be affected if we ran the substitute command. Tip 86, gives an example of usage.

page 282: - The & flag simply tells Vim to reuse the same flags from the previous substitute command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Symbol           | Represents
-----------------+-------------------------
`\r`             | Insert a carriage return
`\t`             | Insert a tab character
`\\`             | Insert a single backslash
`\1`             | Insert the first submatch
`\2`             | Insert the second submatch (and so on, up to \9)
`\0`             | Insert the entire matched pattern
`&`              | Insert the entire matched pattern
`~`              | Use {string} from the previous invocation of :substitute
`\={Vim script}` | Evaluate {Vim script} expression; use result as replacement {string}

page 284: - If we prefix a % at the start of the substitute command, it will be executed on every line of the file: :%s/going/rolling/g

page 285: - In fact, [when :s///c] Vim helpfully reminds us of our options with the prompt “y/n/a/q/l/^E/^Y.” This table shows what each answer means:

1
2
3
4
5
6
7
8
9
Trigger | Effect
--------+----------------------
`y`     | Substitute this match
`n`     | Skip this match
`q`     | Quit substituting
`l`     | last”—Substitute this match, then quit
`a`     | all”—Substitute this and any remaining matches
`<C-e>` | Scroll the screen up
`<C-y>` | Scroll the screen down

page 287: - Leaving the search field of the substitute command blank instructs Vim to reuse the most recent search pattern.

page 288: - Pressing <C-r>/ at the command line pastes the contents of the last search register in place.

page 290: - Look at this command:

1
=> :%s/Pragmatic Vim/Practical Vim/g
1
2
3
=> :let @/='Pragmatic Vim'
=> :let @a='Practical Vim'
=> :%s//\=@a/g

page 292: - :%s//~/&

page 294: - We can always specify a new range and replay the substitution using the :&& command. It doesn’t matter what range was used the last time. :&& by itself acts on the current line, :’<,’>&& acts on the visual selection, and :%&& acts on the entire file. As we saw already, the g& command is a handy shortcut for :%&&.

page 295: - The & command is a synonym for :s, which repeats the last substitution. Unfortunately, if any flags were used, the & command disregards them, meaning that the outcome could be quite different from the previous substitution. Making & trigger the :&& command is more useful. It preserves flags and therefore produces more consistent results. These mappings fix the & command in Normal mode and create a Visual mode equivalent:

1
2
nnoremap & :&&<CR>
xnoremap & :&&<CR>

page 296:

1
2
3
last name,first name,email
neil,drew,drew@vimcasts.org
doe,john,john@example.com
1
2
=> `/\v^([^,]*),([^,]*),([^,]*)$`
=> `:%s//\3,\2,\1`
1
2
3
email,first name,last name
drew@vimcasts.org,drew,neil
john@example.com,john,doe

page 298: - Arithmetic on searches:

page 299:

1
2
3
4
5
=> :let swapper={"dog":"man","man":"dog"}
=> :echo swapper["dog"]
<= man
=> :echo swapper["man"]
<= dog
1
2
=> /\v(<man>|<dog>)
=> :%s//\={"dog":"man","man":"dog"}[submatch(1)]/g

15: Global Commands

page 306: - The :global command takes the following form (see :g):

page 308: - Just like with the :substitute command, we can leave the search field of the :global command blank, and Vim will reuse the last search pattern

page 309: - Suppose that we wanted to collect all of the TODO items in one place. We could view them all at a glance by running this command: :g/TODO

page 310: - let’s yank each line containing the word “TODO” into a register. Then we can paste the contents of that register into another file and keep them around for later. We’ll use the a register. First we’ll need to clear it by running qaq. Let’s break that down: qa tells Vim to start recording a macro into the a register, and then q stops the recording. We didn’t type anything while the macro was recording, so the register ends up empty. We can check that by running the following:

1
2
3
:reg a
--- Registers ---
 "a
1
2
3
4
:g/TODO/yank A
:reg a
"a // TODO: Cache this regexp for certain depths.
// TODO: No matching end code found - warn!

page 312: - ':sort'

page 313: - :g/{/ .+1,/}/-1 sort - The { pattern matches the first line of each {} block. For every line that matches, the :sort command is executed on a [range] that terminates at the end of the {} block. The end result is that all CSS properties are alphabetized within each block of rules.

16: Index and Navigate Source Code with ctags

page 317: - // ctags

17:Compile Code and Navigate Errors with the Quickfix List

page 337: - ‘:compiler’ and ‘:make’ Are Not Just for Compiled Languages

18: Search Project-Wide with grep, vimgrep, and Others

page 345: - //vimgrep

page 346: - This is the format of the :vimgrep command (:vimgrep): :vim[grep][!] /{pattern}/[g][j] {file}

1
2
=> :args *.txt
=> :vim /going/g ##

19: Dial X for Autocompletion

20: Find and Fix Typos with Vim’s Spell Checker

21: Now What?

Appendix 1











[About] [Contact]