Vim: Difference between revisions

From Leo's Notes
This page was last edited on 5 September 2019, at 16:54.
m Text replacement - "Category:Linux{{Navbox Linux}}" to "{{Navbox Linux}}Category:Linux"
mNo edit summary
Line 6: Line 6:
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}}


" Mapping F5 to run hashbang scripts
{{highlight|lang=vim|code=
" Will save, then run the interpreter.
" Mapping F5 to run hashbang scripts
au BufEnter * if match( getline(1) , '^\#!') == 0 |
" Will save, then run the interpreter.
\ execute("let b:interpreter = getline(1)[2:]") |
au BufEnter * if match( getline(1) , '^\#!') == 0 |
\endif
\ execute("let b:interpreter = getline(1)[2:]") |
\endif
fun! CallInterpreter()
 
    if exists("b:interpreter")
fun! CallInterpreter()
          exec ("w")
    if exists("b:interpreter")
          exec ("!".b:interpreter." %")
        exec ("w")
    endif
        exec ("!".b:interpreter." %")
endfun
    endif
endfun
map <F5> :call CallInterpreter()<CR>
 
map <F5> :call CallInterpreter()<CR>
}}


=== 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=
" Map F4 to toggle line numbers
" Map F4 to toggle line numbers
map <F4> :set nonumber!<CR>
map <F4> :set nonumber!<CR>
 
}}


=== Multi-line Edit ===
=== Multi-line Edit ===
1,33s/^/  /g
Indent lines 1-33 with 2 spaces.
{{highlight|lang=text|code=
1,33s/^/  /g
}}


=== Vi Visual Bell Flash ===
=== Vi Visual Bell Flash ===
Out of the box {{code|vi}} 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=
:set noflash
:set noflash
Line 40: Line 44:
=== 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=text|code=
{{highlight|lang=vim|code=
:set autoindent noexpandtab tabstop=4 shiftwidth=4
:set autoindent noexpandtab tabstop=4 shiftwidth=4
}}
}}
Line 46: Line 50:
=== Force Soft Tabs ===
=== Force Soft Tabs ===
The reverse of the above:
The reverse of the above:
{{highlight|lang=text|code=
{{highlight|lang=vim|code=
: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab
:set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab
}}
}}



Revision as of 16:54, 5 September 2019

Quick Overview

TODO: Write guide from onenote.

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