Vim config file on Linux desktop

Customizing your own vim configuration file will bring you future convenience and fluency. Writing into ~/.vimrc, it will take effect next time you enter Vim editor. If you want your config file to apply to all users, edit /etc/vim/vimrc with sudo privilige. For some specific purposes like ACM contest, you can also create a config file. When using it, source this command.

If you want to specify different settings for different file types, for example, C++ files, you should first add filetype plugin indent on to the .vimrc file. and then create .vim/after/ftplugin/cpp.vim, and write

setlocal expandtab
setlocal shiftwidth=2
setlocal softtabstop=2

backup file, swap file and undo file

‘.filename~’ is a backup file, it stores the file before you start to edit it.
‘.filename.swp’ is a swap file, containing the unsaved changes.
‘.filename.un~’ is an undo file that stores the undo trees of the file edited.

Key mappings

Default

<C-^> change active window panel

Self define

Finally I will give the sample code that I set for my personal computer. Before that, let’s clarify a braces auto-completing algorithm. When the left brace is typed, only when there is no right char near it can the right brace be completed. When the right brace is typed, only when the right char near it is the same right char can the cursor be jumped.

Response for typing ‘(‘

1
2
3
4
5
6
get the line and column positions of current cursor as lnum, cnum

if no next character exists:
print '()'
else:
print '('

Reponse for typing ‘)’

1
2
3
4
if the right next char is ')':
jump one char
else:
print ')'

To get the current line text, use getline('.')

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"~/.vimrc
"internationalisation
set encoding=utf-8

"Indentations without hard tabs
set expandtab "pressing <TAB> will always insert 'softtabstop' amount of spaces
set softtabstop=4 "default value equals 'tabstop', but when using indentation without hard tabs, set it to the same as 'shiftwidth'
set shiftwidth=4 "affects when you press '>>', '<<', or '=='. It also affects how automatic indentation works.

"Automatic indentation
set autoindent "when starting a new line, indent as the previous line

"Syntax highlighting
syntax on

"Mouse setting
"don't use the mouse to prevent the appearence of line numbers on copy. For example, use `:1,3y+` instead for copying to the '+clipboard' from line-1 to line-3
set mouse=a

"Appearence
set cursorline "highlight the line where the curent cursor stays
set nu "set line number
set ruler
colorscheme desert "available color themes are in '/usr/share/vim/vim80/colors', also you can add your own in '~/.vim/colors'
let g:molokai_original = 1
set cmdheight=2

"Key mappings
set showmatch
set matchtime=3
inoremap ( ()<ESC>i
inoremap ) <ESC>:call Right_Brace(')')<CR>a

function Right_Brace(schar)
let [bufnum, lnum, cnum, of] = getpos('.')
"echo lnum cnum
let [lnum2, cnum2]=searchpos(a:schar, 'n')
"echo lnum2 cnum2
if lnum==lnum2 && cnum+1==cnum2
execute "normal! l"
else
execute "normal! a)"
endif
endfunction

"Enable plugins
filetype plugin indent on
1
2
3
4
5
6
7
8
9
10
11
"~/.vim/after/ftplugin/cpp.vim
"When it comes to C and C++, file type based indentations automatically sets 'cindent', and for that reason, there is no need to set 'cindent' manually for such files.

"indentation
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal tabstop=2

"matching brackets
inoremap { {}<ESC>i
inoremap {<CR> {<CR>}<ESC>O

(copy from vim ggVG then "+y)

paste to remote vim

  1. ssh -Y user@server or permanently added in .ssh/config

  2. Ctrl+Insert for copy, Shift+Insert for paste

Tweak vim as C/C++ IDE

Plugin manager

vim-plug: https://github.com/junegunn/vim-plug

Debugging

vimspector: https://github.com/puremourning/vimspector#quick-start
After putting Plug 'puremourning/vimspector' into .vimrc. Go to gadget home to execute

1
./install_gadget.py --all

Close a tab

:tabclose

References

How do I set up different tab settings for different languages in Vim: https://stackoverflow.com/a/1743255
Vim: Remove swap, backup and undo Files from Working Directory: https://medium.com/@Aenon/vim-swap-backup-undo-git-2bf353caa02f
How to close a tab in vim? : https://stackoverflow.com/questions/32714834/how-to-close-a-tab-in-vim

Differences between Python 2.7 and Python 3 如何做一个绅士

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×