Vim Tips Wiki
(Undo revision 25112 by 24.152.167.247 (talk) thanks but Category:Map is for "how to map" tips (not the many tips that use a map))
(7 intermediate revisions by 4 users not shown)
Line 10: Line 10:
 
|rating=
 
|rating=
 
|category1=Searching
 
|category1=Searching
|category2=
+
|category2=Usage
 
}}
 
}}
 
This tip presents suggestions for useful mappings. To qualify, each mapping should be ''short'' and should be useful for a ''common'' task. Other mappings belong in another tip.
 
This tip presents suggestions for useful mappings. To qualify, each mapping should be ''short'' and should be useful for a ''common'' task. Other mappings belong in another tip.
   
 
This is a collection of unrelated mappings. You probably can't use all of them "as-is" at the same time. Assign your own left-hand-side as you see fit.
 
This is a collection of unrelated mappings. You probably can't use all of them "as-is" at the same time. Assign your own left-hand-side as you see fit.
  +
  +
==Visual mode blockwise indent==
  +
This keeps the current visual block selection active after changing indent with '<' or '>'. Usually the visual block selection is lost after you shift it, which is incredibly annoying.
  +
<pre>
  +
vmap > >gv
  +
vmap < <gv
  +
</pre>
  +
  +
==Record into register 'q', playback with 'Q'==
  +
Set this map:
  +
<pre>
  +
nnoremap Q @q
  +
</pre>
  +
For quick recordings just type '''qq''' to start recording, then '''q''' to stop. You don't have to worry about the name this way (you just named the recording 'q'). Now, to play back the recording you just type '''Q'''. This will redefine the standard meaning of 'Q', but all that does is enter "Ex" mode which I can live without.
   
 
==Copy==
 
==Copy==
To copy text to the end-of-line, you can press <tt>y$</tt> or you can use the following and press <tt>Y</tt> instead. This mapping sets up <tt>Y</tt> to be consistent with the <tt>C</tt> and <tt>D</tt> operators, which act from the cursor to the end of the line. The default behavior of <tt>Y</tt> is to yank the whole line.
+
To copy text to the end-of-line, you can press <code>y$</code> or you can use the following and press <code>Y</code> instead. This mapping sets up <code>Y</code> to be consistent with the <code>C</code> and <code>D</code> operators, which act from the cursor to the end of the line. The default behavior of <code>Y</code> is to yank the whole line.
 
<pre>
 
<pre>
 
nnoremap Y y$
 
nnoremap Y y$
Line 27: Line 41:
 
</pre>
 
</pre>
   
Use a count to specify the line number (default is line 1). For example, <tt>12_</tt> would copy line 12 to before the current line.
+
Use a count to specify the line number (default is line 1). For example, <code>12_</code> would copy line 12 to before the current line.
   
 
==Delete==
 
==Delete==
In normal mode, you can press <tt>D</tt> to delete all characters from the cursor to end-of-line. The following mapping allows Ctrl-Delete to do the same, in insert mode: {{help|i_CTRL-\_CTRL-O}}
+
In normal mode, you can press <code>D</code> to delete all characters from the cursor to end-of-line. The following mapping allows Ctrl-Delete to do the same, in insert mode: {{help|i_CTRL-\_CTRL-O}}
 
<pre>
 
<pre>
:inoremap <C-Del> <C-\><C-O>D
+
inoremap <C-Del> <C-\><C-O>D
 
</pre>
 
</pre>
   
 
==Search==
 
==Search==
It's easier to press Space than <tt>/</tt> for searching:
+
It's easier to press Space than <code>/</code> for searching:
 
<pre>
 
<pre>
 
nmap <Space> /
 
nmap <Space> /
Line 48: Line 62:
 
</pre>
 
</pre>
 
----
 
----
Search for word under cursor with [[The super star|* or #]] in new window:
+
Search for word under cursor with [[Searching|* or #]] in new window:
 
<pre>
 
<pre>
 
nmap <C-W>* <C-W>s*
 
nmap <C-W>* <C-W>s*
Line 67: Line 81:
 
nnoremap j gj
 
nnoremap j gj
 
nnoremap k gk
 
nnoremap k gk
  +
</pre>
  +
  +
Another option is to leave 'j' and 'j' at their default and instead map the cursor keys for use "precision" scrolling by visual lines. Plus this works in insert and visual select modes.
  +
<pre>
  +
imap <up> <C-O>gk
  +
imap <down> <C-O>gj
  +
nmap <up> gk
  +
nmap <down> gj
  +
vmap <up> gk
  +
vmap <down> gj
 
</pre>
 
</pre>
   

Revision as of 23:44, 19 March 2013

Tip 979 Printable Monobook Previous Next

created 2005 · complexity basic · version 7.0


This tip presents suggestions for useful mappings. To qualify, each mapping should be short and should be useful for a common task. Other mappings belong in another tip.

This is a collection of unrelated mappings. You probably can't use all of them "as-is" at the same time. Assign your own left-hand-side as you see fit.

Visual mode blockwise indent

This keeps the current visual block selection active after changing indent with '<' or '>'. Usually the visual block selection is lost after you shift it, which is incredibly annoying.

vmap > >gv
vmap < <gv

Record into register 'q', playback with 'Q'

Set this map:

nnoremap Q @q

For quick recordings just type qq to start recording, then q to stop. You don't have to worry about the name this way (you just named the recording 'q'). Now, to play back the recording you just type Q. This will redefine the standard meaning of 'Q', but all that does is enter "Ex" mode which I can live without.

Copy

To copy text to the end-of-line, you can press y$ or you can use the following and press Y instead. This mapping sets up Y to be consistent with the C and D operators, which act from the cursor to the end of the line. The default behavior of Y is to yank the whole line.

nnoremap Y y$

To copy an entire line, and paste it before the current line:

nnoremap _ ggY``P

Use a count to specify the line number (default is line 1). For example, 12_ would copy line 12 to before the current line.

Delete

In normal mode, you can press D to delete all characters from the cursor to end-of-line. The following mapping allows Ctrl-Delete to do the same, in insert mode: :help i_CTRL-\_CTRL-O

inoremap <C-Del> <C-\><C-O>D

Search

It's easier to press Space than / for searching:

nmap <Space> /
nmap <C-Space> ?

Highlight text on the screen matching that under the cursor: Press Ctrl-k to start; each subsequent Ctrl-l matches one more character. Uses marks "x" and "y", and register "z".

map <C-k> mx
map <C-l> lmy"zy`x/<C-r>z<CR>`y

Search for word under cursor with * or # in new window:

nmap <C-W>* <C-W>s*
nmap <C-W># <C-W>s#

Quickfix

" map <F3> and <S-F3> to jump between locations in a quickfix list, or
" differences if in window in diff mode
nnoremap <expr> <silent> <F3>   (&diff ? "]c" : ":cnext\<CR>")
nnoremap <expr> <silent> <S-F3> (&diff ? "[c" : ":cprev\<CR>")

Movement

" remap j and k to scroll by visual lines
nnoremap j gj
nnoremap k gk

Another option is to leave 'j' and 'j' at their default and instead map the cursor keys for use "precision" scrolling by visual lines. Plus this works in insert and visual select modes.

imap <up> <C-O>gk
imap <down> <C-O>gj
nmap <up> gk
nmap <down> gj
vmap <up> gk
vmap <down> gj

Miscellaneous

You can a more easily accessed key to enter command-line mode to speed your editing, for example:

nnoremap <Space> :

For those with non-QWERTY keyboards, for example a Brazillian ABNT2 keyboard, you can easily avoid built-in Vim commands in your mapping:

nnoremap ç :

Comments