Taking Notes from Learning Vim

Taking Notes from Learning Vim

Categories of Vim (vi Improved) Features:

Syntax extensions, Programmer assistance, Graphical user interface (GUI) features, Scripting and plug-ins, Initialization,

Session context, Postprocessing, Keyword completion.

Transparent editing

Vim detects and automatically unbundles archived or compressed files. You can directly edit a zipped file such as myfile.tar.gz. You can even editdirectories. Vim lets you navigate a directory and select files to edit using familiar Vim navigation commands.


Vim offers four handy read-only registers from which the user may extract meta-information for “puts”: the current filename (%), the alternate filename (#), the last command-line command executed (:), and the last inserted text (., a period). 

Vim also lets you drop back to a vi-compatible mode with its compatible option (:set compatible).

Major Vim Improvements over vi

Built-in Help :help :help split

Tab completion in Vim’s command line 

Startup and Initialization Options :help startup

Command-Line Options

-b  Edit in binary mode.

-c[cmd] command  command will be executed as an ex command.

-C  Run Vim in compatible (vi) mode.

-d[or vimdiff]  Start in diff mode

-E  Start in improved ex mode.

-g  Start gvim (GUI)

-m  Turn off the write option. Buffers will not be modifiable.

-o  Open all files in a separate window.

-O  Like -o, but opens vertically split windows.

-y  Run Vim in easy mode.

Behaviors Associated to Command Name

vim, gvim[vim -g], vimdiff, gvimdiff

view, gview  Start Vim or gvim in read-only mode. Same as vim -R.

rvim         Start Vim in restrictive mode.

evim, eview  Use “easy” mode for editing or read-only viewing.

ex, gex      Use the old line-editing ex mode. Useful in scripts. Same as vim -e.

System and User Configuration Files

1. VIMINIT. This is an environment variable. If it is nonempty, Vim executes its content

as an ex command.

2. User $HOME/.vimrc files.

3. exrc option. If the Vim exrc option is set, Vim looks for the three additional config

files: [._]vimrc; [._]vimrc; and [._]exrc.

New Motion Commands

count%      Go to the line count percent into the file, putting the cursor on the first nonblank line

:go n[n go] Go to the nth byte in the buffer. All characters, including end-of-line characters, are counted.

Visual Mode Motion

you can type v  in normal mode to start visual mode. Once you are  in visual mode, any motion commands move the cursor and highlight text as the cursor

moves to a new position. So, the “next word” command (w) in visual mode moves the cursor to the next word and highlights the selected text.

Visual mode motion commands in Vim

as,is  Add sentence, or inner sentence.

ap,ip  Add paragraph, or inner paragraph.

Extended Regular Expressions

\| Indicates alternation, house\|home.   

\+ Matches one or more of the preceding regular expression.

\= Matches zero or one of the preceding regular expression.

\{n,m}, \{n}, \{n,}, \{,m}, \{},

Matches n to m of the preceding regular expression, as much as possible.

\{-n,m}Matches n to m of the preceding regular expression, as few as possible.


\{-n,} Matches at least n of the preceding regular expression, as few as possible.

\{-,m} Matches 0 to m of the preceding regular expression, as few as possible.

\i Matches any identifier character, as defined by the isident option.

\s,\S,\b,\e,\r,\t,\n,~,\(…\), \1

Multiple Windows in Vim

Initiating Multiwindow Editing

Multiwindow Initiation from the Command Line (Shell)

Vim -o file1 file2

Another form of the command line preallocates the windows by appending a number n to -o:

Vim -o5 file1 file2

Multiwindow Editing Inside Vim :split[vsplit] [otherfile]

Opening Windows

The full :split command to open a new horizontal window is:

:[n]split [++opt] [+cmd] [file]

n       Tells Vim how many lines to display in the new window, which goes at the top.

:15split ++fileformat=unix otherfile

To simply split the screen, showing the same file in both windows and using all the current defaults, you can use the key commands ^Ws, ^WS, or ^W^S. ==>^ means ctrl.

Window Command Summary

ex command                        vi command              Description

:[n]split [++opt] [+cmd] [file] ^Ws,^WS,^W^S

:[n]new [++opt] [+cmd]          ^Wn,^W^N              Same as :split, but start the new window editing an empty file.

:[n]sview [++opt] [+cmd] [file]                          Read-only version of :split.

:[n]sfind [++opt] [+cmd] [file]                          Split window and open  file (if specified) in the new window. Look for file in the path.

:[n]vsplit [++opt] [+cmd] [file] ^Wv,^W^V             Vertical version of :split.

:[n]vnew [++opt] [+cmd]                                  Vertical version of :new.

Moving Around Windows

Vim uses the mnemonic prefix keystroke ^W consistently for window navigation.

CTRL-W      Move to next window above or to the left.

CTRL-W w    Move to the next window below or to the right.

CTRL-W  l   Move to the window to the left of the current window.

CTRL-W  r   Move to the window to the left of the current window.

CTRL-W  t   Move cursor to the top leftmost window.

CTRL-W  b   Move cursor to the bottom rightmost window.

CTRL-W  p   Move to the previous (last accessed) window.

Commands to rotate window positions

^Wr,^W^R       Rotate windows down or to the right.

^WR                  Rotate windows up or to the left.

^Wx,^W^X        Swap positions with the next window, or if issued with a count n, swap with nth next window.

Commands to change position and layout

^WK Move window to top of screen and use full width. The cursor stays with the moved window.

^WJ Move window to bottom of screen and use full width. The cursor stays with the moved window.

^WH Move window to left of screen and use full height. The cursor stays with the moved window.

^WL Move window to right of screen and use full height. The cursor stays with the moved window.

^WT Move window to new tab. The cursor stays with the moved window. If the current window is

the only window in the current tab, no action is taken.

Tabbed Editing

vim -p file1 file2 file3

:tabnew filename, :tabclose

:tabonly    Close all other tabs.

Basic Tab Navigation

:tabs         list all tabs

:tabm 0       move the current tab first

:tabm {i}     move the current tab to the i+1 position

:tabn         move to (view) the next tab

:tabp         move to (view) the previous tab

:tabfirst     move to the first tab

:tabf {file}  open a new tab with the filename given, searching the 'path' to find it

:tabc         close the current tab

:tabc {i}     close the i-th tab

:tabo         close other tabs

For basic tab navigation, it is probably more convenient to use the built-in normal-mode commands:

gt            move to (view) the next tab

gT            move to (view) the previous tab

{i}gt         move to (view) the tab in the i-th position

Closing and Quitting Windows

:quit[!],^Wq,^W^Q   Quit the current window.

:close[!],^Wc       Close the current window.

:only[!],^Wo,^W^O   Make the current window the only window.

What’s Your Favorite Color (Scheme)?    :colorscheme desert

Other Cool Stuff in Vim

Editing Binary Files    :set binary, vim -b

Editing Files in Other Places

vim scp://ehannah@mozart:122//home/ehannah/.vimrc

supported protocols: ftp,sftp,scp,http,dav,rcp

Navigating and Changing Directories

vim $Directory_Name

When the cursor is positioned over a directory name, move to that directory by pressing the ENTER key.

If the cursor is over a filename, pressing ENTER edits that file.

You can delete[D] and rename[R] files and directories.

One  really nice advantage of editing directories  is quick access  to  files  through Vim’s  search  function: /expreserve.c

HTML Your Text

gvim “Convert to HTML”

2html.vim script    :runtime!syntax/2html.vim

TOhtml command      :25,44TOhtml

What’s the Difference?

vimdiff old_file new_file, vim -d old_file new_file

Undoing Undos

Use the undolevels option to define the number of undoable changes you can make in an edit session. The default is 1,000.

If you want vi compatibility, set undolevels to zero: :set undolevels=0

:redo, or the CTRL-R to roll forward and “redo” changes.

Some abbreviations for common options:

ai autoindent

bg background

ff fileformat

ft filetype

ic ignorecase

li list

nu number

sc showcommand (not showcase)

sm showmatch

sw shiftwidth

wm wrapmargin

A Few Quickies (Not Necessarily Vim-Specific)

A quick swap

A common typing error is to enter two characters in the wrong order. Position the cursor  over  the  first  wayward  character  and  type  xp  (delete  character,  put character).

Another quick swap

Got  two  lines you’d  rather  swap? Position  the cursor on  the  top  line, and  type ddp (delete line, put line after current line).

Quick help  F1

Vim lets you access recently executed commands by using the arrow keys in the command line. Moving up and down with the arrow keys, Vim displays recent commands.

You can get even more sophisticated by invoking Vim’s built-in command history editing. Do this by entering CTRL-F on the command line. 




Post a Comment


Java (160) Lucene-Solr (112) Interview (64) All (58) J2SE (53) Algorithm (45) Soft Skills (39) Eclipse (33) Code Example (31) JavaScript (23) Linux (22) Spring (22) Windows (22) Tools (21) Web Development (20) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) Troubleshooting (14) J2EE (13) Network (13) PowerShell (11) Problem Solving (10) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) Tips (8) bat (8) blogger (8) Big Data (7) Database (7) Google (7) Guava (7) JSON (7) Shell (7) System Design (7) ANT (6) Coding Skills (6) Lesson Learned (6) Programmer Skills (6) Scala (6) css (6) Algorithm Series (5) Cache (5) Continuous Integration (5) IDE (5) adsense (5) xml (5) AIX (4) Become a Better You (4) Code Quality (4) Concurrency (4) Dynamic Languages (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Review (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Distributed (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Life (3) Python (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Fiddler (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Logging (2) Mac (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Firefox (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) Invest (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts