Difference between revisions of "Vi editor"

From Teknologisk videncenter
Jump to: navigation, search
m (FAQ)
m (Enhanced Python programmin ~/.vimrc)
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
== Search and replace ==
 +
Replace all '''oldtext''' to '''newtext''' starting at line '''1''' to last line ('''$'''). '''s''' means search and '''g''' globally - not just once.
 +
<source lang=cli>
 +
:1,$s/oldtext/newtext/g
 +
</source>
 
= FAQ =
 
= FAQ =
 +
== Syntax highlighting ==
 +
To use syntax highlighting temporarily use the command '':syntax on''. To switch it off again use '':syntax off''.
 +
 +
To use syntax highlighting permanently use
 +
<source lang="cli">
 +
[root@mars ~]# <input>echo "syntax on" >> $HOME/.vimrc</input>
 +
</source>
 +
 +
== Line numbers in vi ==
 +
To use line numbers temporarily use the command '':set number''. To switch it off again use '':set nonumber''.
  
== How can I see which mode I'm in ==
+
To use line number permanently use
 +
<source lang="cli">
 +
[root@mars ~]# <input>echo "set number" >> $HOME/.vimrc</input>
 +
</source>
 +
== Which editor mode are I'm in ==
 
Use ''showmode'' option. Shown below
 
Use ''showmode'' option. Shown below
 
<source lang="cli">
 
<source lang="cli">
Line 9: Line 28:
 
#In insert mode it will write '''-- INSERT --'''
 
#In insert mode it will write '''-- INSERT --'''
 
#In replace mode it will write '''-- REPLACE --'''
 
#In replace mode it will write '''-- REPLACE --'''
#In line command mode it will write ''':'''
+
#In line command mode it will write a colon (''':''') in the lower left cornor of the screen.
 
#In command mode - empty or everything else
 
#In command mode - empty or everything else
  
 +
==Show special characters==
 +
<source lang="cli">
 +
:set list
 +
</source>
 
== VIM and FreeBSD malfunctioning arrow keys in insert mode ==
 
== VIM and FreeBSD malfunctioning arrow keys in insert mode ==
 
Set ''vim'' to work in ''nocompatible'' mode. Not as old ''vi''.
 
Set ''vim'' to work in ''nocompatible'' mode. Not as old ''vi''.
Line 17: Line 40:
 
[root@mars ~]# <input>echo "set nocompatible" >> $HOME/.vimrc</input>
 
[root@mars ~]# <input>echo "set nocompatible" >> $HOME/.vimrc</input>
 
</source>
 
</source>
{{#css:
+
 
   
+
== Inserting control characters in documents ==
    pre {   font-weight: bold; font-size: 150%; color: #00FF00; background: black; margin: 10px 75px;}
+
Using the <CTRL>-v and A inserts SOH (001) See [[ASC-II]]. <CTRL>-v and <ESC> inserts <ESC>. Remember capital letters.
}}
+
 
[[Category:Operating Systems]][[Category:UNIX]][[Category:Linux]]
+
To see non-visible characters use '''set list''' turn of again with '''set nolist'''
 +
 
 +
==Forcing UTF8 in vim==
 +
Add the following lines to your $HOME/.vimrc
 +
;Note:Remember to set your terminal to UTF-8
 +
<source lang=text>
 +
set encoding=utf-8
 +
set fileencoding=utf-8
 +
set fileencodings=utf-8
 +
</source>
 +
or
 +
<source lang=text>
 +
set nocompatible
 +
syntax off
 +
set showmode
 +
set tabstop=4
 +
set encoding=utf-8
 +
set fileencoding=utf-8
 +
set fileencodings=utf-8
 +
</source>
 +
==Sane C programming .vimrc==
 +
<source lang=text>
 +
syntax on
 +
set showmode
 +
set tabstop=4
 +
set number
 +
set autoindent
 +
set smartindent
 +
if has("autocmd")
 +
   au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
 +
endif
 +
</source>
 +
==Enhanced C programming ~/.vimrc==
 +
<source lang=text>
 +
syntax on
 +
set showmode
 +
" set noet ci pi sts=0 sw=4 ts=4 - same as the next 6 lines
 +
set noexpandtab
 +
set copyindent
 +
set preserveindent
 +
set softtabstop=0
 +
set shiftwidth=4
 +
set tabstop=4
 +
set number
 +
set autoindent
 +
set smartindent
 +
set mouse=v
 +
" Yank and Paste between terminal windows
 +
set clipboard=unnamed
 +
if has("autocmd")
 +
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
 +
endif
 +
</source>
 +
==Enhanced Python programmin ~/.vimrc==
 +
{{:Python#Vim_setting_for_python}}
 +
{{Source cli}}
 +
[[Category:Operating Systems]][[Category:UNIX]][[Category:Linux]][[Category:Linux Command]]

Latest revision as of 10:41, 3 December 2023

Search and replace

Replace all oldtext to newtext starting at line 1 to last line ($). s means search and g globally - not just once.

:1,$s/oldtext/newtext/g

FAQ

Syntax highlighting

To use syntax highlighting temporarily use the command :syntax on. To switch it off again use :syntax off.

To use syntax highlighting permanently use

[root@mars ~]# <input>echo "syntax on" >> $HOME/.vimrc</input>

Line numbers in vi

To use line numbers temporarily use the command :set number. To switch it off again use :set nonumber.

To use line number permanently use

[root@mars ~]# <input>echo "set number" >> $HOME/.vimrc</input>

Which editor mode are I'm in

Use showmode option. Shown below

[root@mars ~]# <input>echo "set showmode" >> $HOME/.vimrc</input>

Now you can see which mode you are in by looking at the bottom line

  1. In insert mode it will write -- INSERT --
  2. In replace mode it will write -- REPLACE --
  3. In line command mode it will write a colon (:) in the lower left cornor of the screen.
  4. In command mode - empty or everything else

Show special characters

:set list

VIM and FreeBSD malfunctioning arrow keys in insert mode

Set vim to work in nocompatible mode. Not as old vi.

[root@mars ~]# <input>echo "set nocompatible" >> $HOME/.vimrc</input>

Inserting control characters in documents

Using the <CTRL>-v and A inserts SOH (001) See ASC-II. <CTRL>-v and <ESC> inserts <ESC>. Remember capital letters.

To see non-visible characters use set list turn of again with set nolist

Forcing UTF8 in vim

Add the following lines to your $HOME/.vimrc

Note
Remember to set your terminal to UTF-8
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8

or

set nocompatible
syntax off
set showmode
set tabstop=4
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8

Sane C programming .vimrc

syntax on
set showmode
set tabstop=4
set number
set autoindent
set smartindent
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
endif

Enhanced C programming ~/.vimrc

syntax on
set showmode
" set noet ci pi sts=0 sw=4 ts=4 - same as the next 6 lines
set noexpandtab
set copyindent
set preserveindent
set softtabstop=0
set shiftwidth=4
set tabstop=4
set number
set autoindent
set smartindent
set mouse=v
" Yank and Paste between terminal windows
set clipboard=unnamed
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
endif

Enhanced Python programmin ~/.vimrc

Programming style

  • Recomended indent - 4 spaces
pip install reindent  # Pyhon reindent script
reindent my_script.py

Vim setting for python

add following lines in ~/.vimrc

"Python Settings
autocmd FileType python set softtabstop=4
autocmd FileType python set tabstop=4
autocmd FileType python set autoindent
autocmd FileType python set expandtab
autocmd FileType python set textwidth=80
autocmd FileType python set smartindent
autocmd FileType python set shiftwidth=4
" Map <F2> to write file and execute script in both command- and insert mode
autocmd FileType python map <buffer> <F2> :w<CR>:exec '! python' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F2> <esc>:w<CR>:exec '! python' shellescape(@%, 1)<CR>

Links