Vim: Difference between revisions
mNo edit summary |
|||
| Line 1: | Line 1: | ||
== Quick Overview == | ==Quick Overview== | ||
=== Movement === | ===Movement=== | ||
Some movement keys | Some movement keys | ||
{| class="wikitable" | {| class="wikitable" | ||
! Key | !Key | ||
! Description | !Description | ||
|- | |- | ||
| j | |j | ||
| down | |down | ||
|- | |- | ||
| k | |k | ||
| up | |up | ||
|- | |- | ||
| l | |l | ||
| right | |right | ||
|- | |- | ||
|- | |- | ||
| h | |h | ||
| left | |left | ||
|- | |- | ||
| w | |w | ||
| go forward 1 word | |go forward 1 word | ||
|- | |- | ||
| W | |W | ||
| go forward 1 word, ignoring symbols and punctuations | |go forward 1 word, ignoring symbols and punctuations | ||
|- | |- | ||
| b | |b | ||
| go backward 1 word | |go backward 1 word | ||
|- | |- | ||
| B | |B | ||
| go backward 1 word, ignoring symbols and punctuations | |go backward 1 word, ignoring symbols and punctuations | ||
|- | |- | ||
| f $char | |f $char | ||
| Go to next instance of '$char' | |Go to next instance of '$char' | ||
|- | |- | ||
| ; | |; | ||
| Repeat the previous f command | |Repeat the previous f command | ||
|- | |- | ||
| 0 | |0 | ||
| Move to beginning of the line | |Move to beginning of the line | ||
|- | |- | ||
| ^ | |^ | ||
| Move to first non-blank character of the line | |Move to first non-blank character of the line | ||
|- | |- | ||
| $ | |$ | ||
| Move to end of the line | |Move to end of the line | ||
|- | |- | ||
| gg | |gg | ||
| Move to top of file | |Move to top of file | ||
|- | |- | ||
| G | |G | ||
| Move to end of the file | |Move to end of the file | ||
|- | |- | ||
| - | | - | ||
| Go to start of previous line | |Go to start of previous line | ||
|- | |- | ||
| + | | + | ||
| Go to start of next line | |Go to start of next line | ||
|} | |} | ||
=== Actions === | ===Actions=== | ||
{| class="wikitable" | {| class="wikitable" | ||
! Key | !Key | ||
! Description | !Description | ||
|- | |- | ||
| u | |u | ||
| Undo | |Undo | ||
|- | |- | ||
| Ctrl+r | |Ctrl+r | ||
| Redo | |Redo | ||
|- | |- | ||
| / | |/ | ||
| find | |find | ||
|- | |- | ||
| >> | |>> | ||
| Indent by shiftwidth amount | |Indent by shiftwidth amount | ||
|- | |- | ||
| << | |<< | ||
| Un-indent by shiftwidth amount | |Un-indent by shiftwidth amount | ||
|- | |- | ||
| ~ | |~ | ||
| Toggle selected character case | |Toggle selected character case | ||
|- | |- | ||
| . | |. | ||
| Repeat last command | |Repeat last command | ||
|- | |- | ||
| x | |x | ||
| Delete character | |Delete character | ||
|- | |- | ||
| d<movement> | |d<movement> | ||
| Delete in the given movement. Eg. {{code|dw}} deletes a word. | |Delete in the given movement. Eg. {{code|dw}} deletes a word. | ||
|- | |- | ||
| dd | |dd | ||
| Delete current line | |Delete current line | ||
|- | |- | ||
| D | |D | ||
| Delete till the end of the line | |Delete till the end of the line | ||
|- | |- | ||
| p | |p | ||
| Pastes what was last deleted | |Pastes what was last deleted | ||
|} | |} | ||
| Line 109: | Line 109: | ||
Enter visual block mode with {{code|ctrl+v}} starting at the current cursor location. This mode is useful if you wish to change multiple lines at a particular column. | Enter visual block mode with {{code|ctrl+v}} starting at the current cursor location. This mode is useful if you wish to change multiple lines at a particular column. | ||
=== Registers === | ===Registers=== | ||
Registers hold some value and are named {{code|a}} through {{code|z}}. You may use registers as a type of clipboard or temporary scratch space. Type {{code|:register}} to list all registers with values. | Registers hold some value and are named {{code|a}} through {{code|z}}. You may use registers as a type of clipboard or temporary scratch space. Type {{code|:register}} to list all registers with values. | ||
{| class="wikitable" | {| class="wikitable" | ||
! Key | !Key | ||
! Description | !Description | ||
|- | |- | ||
| "<register>p | |"<register>p | ||
| Paste whatever is in <register> | |Paste whatever is in <register> | ||
|- | |- | ||
| "xdd | |"xdd | ||
| Put deleted line into register x | |Put deleted line into register x | ||
|} | |} | ||
Macros repeat a set of commands stored within a register. | Macros repeat a set of commands stored within a register. | ||
{| class="wikitable" | {| class="wikitable" | ||
! Key | !Key | ||
! Description | !Description | ||
|- | |- | ||
| q<register> | |q<register> | ||
| Begin recording macro into register. Eg. {{code|qq}} records into register q. | |Begin recording macro into register. Eg. {{code|qq}} records into register q. | ||
|- | |- | ||
| q | |q | ||
| End the recording by pressing q again | |End the recording by pressing q again | ||
|- | |- | ||
| @<register> | |@<register> | ||
| Repeat macro stored in register <register>. Eg. {{code|@q}} replays macro at q. | |Repeat macro stored in register <register>. Eg. {{code|@q}} replays macro at q. | ||
|} | |} | ||
| Line 141: | Line 141: | ||
== Tips == | ==Tips== | ||
=== Execute Current File using Preset Key === | ===Execute Current File using Preset Key=== | ||
To make vi execute a script that is being edited using a function key (in this case, F5), add the following to your {{code|.vimrc}} | To make vi execute a script that is being edited using a function key (in this case, F5), add the following to your {{code|.vimrc}} | ||
| Line 162: | Line 162: | ||
}} | }} | ||
=== Toggle Line Numbers === | ===Toggle Line Numbers=== | ||
To make vi toggle line numbers using a key (in this case, F4), add the following to your {{code|.vimrc}} | To make vi toggle line numbers using a key (in this case, F4), add the following to your {{code|.vimrc}} | ||
{{highlight|lang=vim|code= | {{highlight|lang=vim|code= | ||
| Line 169: | Line 169: | ||
}} | }} | ||
=== Multi-line Edit === | ===Multi-line Edit=== | ||
Indent lines 1-33 with 2 spaces. | Indent lines 1-33 with 2 spaces. | ||
{{highlight|lang=text|code= | {{highlight|lang=text|code= | ||
| Line 175: | Line 175: | ||
}} | }} | ||
=== Vi Visual Bell Flash === | ===Vi Visual Bell Flash=== | ||
Out of the box {{code|vim}} flashes to the point where I feel like I'll get a seizure. To turn it off: | Out of the box {{code|vim}} flashes to the point where I feel like I'll get a seizure. To turn it off: | ||
{{highlight|lang=text|code= | {{highlight|lang=text|code= | ||
| Line 181: | Line 181: | ||
}} | }} | ||
=== Force Tabs === | ===Force Tabs=== | ||
Force tabs instead of soft tabs with tab stops and width set to 4. | Force tabs instead of soft tabs with tab stops and width set to 4. | ||
{{highlight|lang=vim|code= | {{highlight|lang=vim|code= | ||
| Line 187: | Line 187: | ||
}} | }} | ||
=== Force Soft Tabs === | ===Force Soft Tabs=== | ||
The reverse of the above: | The reverse of the above: | ||
{{highlight|lang=vim|code= | {{highlight|lang=vim|code= | ||
| Line 193: | Line 193: | ||
}} | }} | ||
=== Mouse Scroll Only === | |||
To enable mouse wheel scrolling but not mouse interactions: | |||
{{Highlight | |||
| code = " Enable mouse support | |||
:set mouse=a | |||
" Disable left mouse | |||
:nmap <LeftMouse> <nop> | |||
:imap <LeftMouse> <nop> | |||
:vmap <LeftMouse> <nop> | |||
" Disable double click | |||
:nmap <2-LeftMouse> <nop> | |||
| lang = vim | |||
}} | |||
== See also == | |||
* https://superuser.com/questions/495652/vim-enable-mouse-scroll-only-not-cursor-positioning | |||
[[Category:TipsnTrics]] | [[Category:TipsnTrics]] | ||
{{Navbox Linux}}[[Category:Linux]] | {{Navbox Linux}} | ||
[[Category:Linux]] | |||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Revision as of 20:58, 15 July 2020
Quick Overview
Movement
Some movement keys
| Key | Description |
|---|---|
| j | down |
| k | up |
| l | right |
| h | left |
| w | go forward 1 word |
| W | go forward 1 word, ignoring symbols and punctuations |
| b | go backward 1 word |
| B | go backward 1 word, ignoring symbols and punctuations |
| f $char | Go to next instance of '$char' |
| ; | Repeat the previous f command |
| 0 | Move to beginning of the line |
| ^ | Move to first non-blank character of the line |
| $ | Move to end of the line |
| gg | Move to top of file |
| G | Move to end of the file |
| - | Go to start of previous line |
| + | Go to start of next line |
Actions
| Key | Description |
|---|---|
| u | Undo |
| Ctrl+r | Redo |
| / | find |
| >> | Indent by shiftwidth amount |
| << | Un-indent by shiftwidth amount |
| ~ | Toggle selected character case |
| . | Repeat last command |
| x | Delete character |
| d<movement> | Delete in the given movement. Eg. dw deletes a word.
|
| dd | Delete current line |
| D | Delete till the end of the line |
| p | Pastes what was last deleted |
You can repeat actions by specifying the number prior to the command.
Eg. To delete 10 characters: 10x
Eg. To delete 10 lines: 10dd
Enter visual line mode with shift+v starting at the current location. Use movement keys to change what is selected. Actions can then be done to the selected portion of the text.
Enter visual block mode with ctrl+v starting at the current cursor location. This mode is useful if you wish to change multiple lines at a particular column.
Registers
Registers hold some value and are named a through z. You may use registers as a type of clipboard or temporary scratch space. Type :register to list all registers with values.
| Key | Description |
|---|---|
| "<register>p | Paste whatever is in <register> |
| "xdd | Put deleted line into register x |
Macros repeat a set of commands stored within a register.
| Key | Description |
|---|---|
| q<register> | Begin recording macro into register. Eg. qq records into register q.
|
| q | End the recording by pressing q again |
| @<register> | Repeat macro stored in register <register>. Eg. @q replays macro at q.
|
Tips
Execute Current File using Preset Key
To make vi execute a script that is being edited using a function key (in this case, F5), add the following to your .vimrc
" Mapping F5 to run hashbang scripts
" Will save, then run the interpreter.
au BufEnter * if match( getline(1) , '^\#!') == 0
Toggle Line Numbers
To make vi toggle line numbers using a key (in this case, F4), add the following to your .vimrc
" Map F4 to toggle line numbers
map <F4> :set nonumber!<CR>
Multi-line Edit
Indent lines 1-33 with 2 spaces.
1,33s/^/ /g
Vi Visual Bell Flash
Out of the box vim flashes to the point where I feel like I'll get a seizure. To turn it off:
:set noflash
Force Tabs
Force tabs instead of soft tabs with tab stops and width set to 4.
:set autoindent noexpandtab tabstop=4 shiftwidth=4
Force Soft Tabs
The reverse of the above:
:set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab
Mouse Scroll Only
To enable mouse wheel scrolling but not mouse interactions:
" Enable mouse support
:set mouse=a
" Disable left mouse
:nmap <LeftMouse> <nop>
:imap <LeftMouse> <nop>
:vmap <LeftMouse> <nop>
" Disable double click
:nmap <2-LeftMouse> <nop>