06 August 2023
My .vimrc
"
" set linewidth
" set textwidth=78
set encoding=utf-8
set fileencoding=utf-8
" enable syntax highlighting
syntax enable
" show line numbers
set number
" set tabs to have 4 spaces
set ts=4
" indent when moving to the next line while writing code
set autoindent
" expand tabs into spaces
set expandtab
" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4
" show a visual line under the cursor's current line
" set cursorline
" show the matching part of the pair for [] {} and ()
set showmatch
set guifont=Consolas:h11
" avoid word breaking at end of each screen line
set linebreak
" enable all Python syntax highlighting features
let python_highlight_all = 1
set splitbelow
set splitright
nmap <C-h> <C-w>h
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-l> <C-w>l
" vim-pathogen
execute pathogen#infect()
syntax on
filetype plugin indent on
" Ctrl-P
set runtimepath^=~/.vim/bundle/ctrlp.vim
" *'g:ctrlp_map'*
" Use this option to change the mapping to invoke CtrlP in |Normal| mode:
let g:ctrlp_map = '<c-p>'
" *'g:ctrlp_cmd'*
" Set the default opening command to use when pressing the above mapping:
let g:ctrlp_cmd = 'CtrlP'
" *'g:ctrlp_working_path_mode'*
" When starting up, CtrlP sets its local working directory according to this
" variable: >
let g:ctrlp_working_path_mode = 'ra'
set colorcolumn=5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65
" NERDTree settings
" Start NERDTree. If a file is specified, move the cursor to its window.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif
" Open the existing NERDTree on each new tab.
autocmd BufWinEnter * if getcmdwintype() == '' | silent NERDTreeMirror | endif
let NERDTreeWinSize = 20
" toggle between line wrapping and unwrapping
map <C-a> :set wrap!<CR>
" set colorcolumn color
:hi ColorColumn ctermbg=lightgrey guibg=lightgrey
" set foreground and background colours for vim window
" :highlight Normal ctermfg=black ctermbg=lightyellow guifg=black guibg=lightyellow
02 April 2023
How to resolve merge conflicts in Git
https://stackoverflow.com/questions/161813/how-do-i-resolve-merge-conflicts-in-a-git-repository
Try:
git mergetool
It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.
As per Josh Glover's comment:
[This command] doesn't necessarily open a GUI unless you install one. Running
git mergetoolfor me resulted invimdiffbeing used. You can install one of the following tools to use it instead:meld,opendiff,kdiff3,tkdiff,xxdiff,tortoisemerge,gvimdiff,diffuse,ecmerge,p4merge,araxis,vimdiff,emerge.
Below is a sample procedure using vimdiff to resolve merge conflicts, based on this link.
Run the following commands in your terminal
git config merge.tool vimdiff git config merge.conflictstyle diff3 git config mergetool.prompt falseThis will set
vimdiffas the default merge tool.Run the following command in your terminal
git mergetoolYou will see a
vimdiffdisplay in the following format:╔═══════╦══════╦════════╗ ║ ║ ║ ║ ║ LOCAL ║ BASE ║ REMOTE ║ ║ ║ ║ ║ ╠═══════╩══════╩════════╣ ║ ║ ║ MERGED ║ ║ ║ ╚═══════════════════════╝These 4 views are
- LOCAL: this is the file from the current branch
- BASE: the common ancestor, how this file looked before both changes
- REMOTE: the file you are merging into your branch
- MERGED: the merge result; this is what gets saved in the merge commit and used in the future
You can navigate among these views using ctrl+w. You can directly reach the MERGED view using ctrl+w followed by j.
You can edit the MERGED view like this:
If you want to get changes from REMOTE
:diffg REIf you want to get changes from BASE
:diffg BAIf you want to get changes from LOCAL
:diffg LO
Save, Exit, Commit, and Clean up
:wqasave and exit from vigit commit -m "message"git cleanRemove extra files (e.g.*.orig). Warning: It will remove all untracked files, if you won't pass any arguments.
05 April 2022
Is there an easier way to reselect the previous selection?
In normal mode, you can use gv to re-select the previous visual selection.
If you just want to re-issue an additional ex command over the same range, you can use * on the command line, as long as "*" isn't in your 'cpoptions' setting.
:'<,'>s/foo/bar/g
:*s/baz/bat/g
:help cpo-star
:help :*
:help v_:31 January 2022
How to use marks and jumps in vim?
How to use marks and jumps in vim?
https://www.linux.com/news/vim-tips-moving-around-using-marks-and-jumps/
How to split windows in vim, or say how to use viewport in vim?
How to split windows in vim, or say how to use viewport in vim?
https://www.linux.com/training-tutorials/vim-tips-using-viewports/
26 January 2022
How to break all lines into 78 character long all at once in vim?
(Author: Sherman Chen)
Method 1
========
:set textwidth=78
gggqG
"gg" takes you to the first line, "gq" is the format operator and "G" the motion that jumps to the last line.
Method 2
========
:set textwidth=78
:1,$g/.*/normal jgwap
Method 3
========
1. :set linebreak
to recover those broken words at the end of each line into whole words.
2. :set textwidth=78
3. Find a line which is longer than 78 characters, say line #3. Move the cursor to that line minus one, i.e. 3 - 1 = 2.
4. qaj$aa Backspace Escape q
5. See how many lines the file contains totally, say, 100.
6. gg
7. 100@a Enter
All the lines will become 78 character long now.
Method 4 (which has improved Method 3)
========
1. :set linebreak
to recover those broken words at the end of each line into whole words.
2. :set textwidth=78
3. Move the cursor to any line which is longer than 78 characters.
4. qa$aa Backspace Escape q
5. :%normal @a
All the lines will become 78 character long now.
(END)
23 January 2022
29 December 2021
21 December 2021
How to change multiple rows into one row in vim?
Check how many rows are there totally (e.g. 50)
50J
How to change a one-row text into multiple rows in vim?
:%s/; /\r/g
The above command replaces '; ' with \r (carriage return)
How to append some text to each row in vim?
:%s/$/,/g
How to remove duplicate rows in vim?
:sort u
05 September 2021
How to Delete Lines in Vim / Vi
How to Delete Lines in Vim / Vi
https://linuxize.com/post/vim-delete-line/
Deleting a Line
The command to delete a line in Vim is dd .
Below are step-by-step instructions to delete a line:
Press the Esc key to go to normal mode.
Place the cursor on the line you want to delete.
Type dd and hit Enter to remove the line.
Pressing dd multiple times will delete multiple lines.
Deleting Multiple Lines
To delete multiple lines at once, prepend the dd command with the number of lines to be deleted. For example, to delete five lines you would do the following:
Press the Esc key to go to normal mode.
Place the cursor on the first line you want to delete.
Type 5dd and hit Enter to delete the next five lines.
Delete a range of lines
The syntax for deleting a range of lines is as follows:
:[start],[end]d
For example, to delete lines starting from 3 to 5 you would do the following:
Press the Esc key to go to normal mode.
Type :3,5d and hit Enter to delete the lines.
You can also use the following characters to specify the range:
. (dot) - The current line.
$ - The last line.
% - All lines.
Here are a few examples:
:.,$d - From the current line to the end of the file.
:.,1d - From the current line to the beginning of the file.
:10,$d - From the 10th line to the end of the file.
Delete All Lines
To delete all line you can use either the % symbol that represents all lines or the 1,$ range:
Press the Esc key to go to normal mode.
Type :%d and hit Enter to delete all the lines.
Deleting Lines Containing a Pattern
The syntax for deleting multiple lines based on a specific pattern is as follows:
:g/<pattern>/d
The global command (g) tells the delete command (d) to delete all lines containing the <pattern>.
To match the lines not matching the pattern, add an exclamation mark (!) before the pattern:
:g!/<pattern>/d
The pattern can be a literal match or regular expression . Below are some examples:
:g/foo/d - Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
:g!/foo/d - Delete all lines not containing the string “foo”.
:g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
:g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines.
:g/^\s*$/d - Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters (\s*).
01 June 2021
My '_vimrc' File
source $VIMRUNTIME/vimrc_example.vim
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg1 = substitute(arg1, '!', '\!', 'g')
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg2 = substitute(arg2, '!', '\!', 'g')
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let arg3 = substitute(arg3, '!', '\!', 'g')
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
if empty(&shellxquote)
let l:shxq_sav = ''
set shellxquote&
endif
let cmd = '"' . $VIMRUNTIME . '\diff"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
let cmd = substitute(cmd, '!', '\!', 'g')
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
if exists('l:shxq_sav')
let &shellxquote=l:shxq_sav
endif
endfunction
" 字符编码{{{
" Vim显示的编码(设置这个不会改变文件的编码){
if has('win32') || has('win64')
set encoding=utf-8
set termencoding=chinese
endif
" }
" 编辑已存在的文件时的参考文件编码.需要注意顺序,前面的字符集应该比后面的字符集大{
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
" }
" }}}
"
"
filetype plugin indent on
autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
set number
"
"
" The following commands do not work
" autocmd FileType html
" set omnifunc=htmlcomplete#CompleteTags
" The following commands do not work
" set omnifunc=csscomplete#CompleteCSS
" autocmd FileType css set omnifunc=csscomplete#CompleteCSS
" html css class id auto complete
" https://github.com/Shougo/neocomplete.vim
let g:neocomplete#enable_at_startup = 1
27 April 2021
Good Text Editors
Good Text Editors
1. Sublime Text 3
2. Vim
09 November 2020
Vi - How to Automatic Indent?
Vi - How to Automatic Indent?
Windows
In Vim folder, create a text file named '_vimrc' with the following contents -
filetype plugin indent on
autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
30 June 2015
Chinese are not displayed in vi / vim on Windows OS
Solution 1:
(1) Change your system locale into 'Chinese (simplified, PRC)'. Restart computer.
(2) Edit the file - C:\Program Files (x86)\Vim\_vimrc, and append the following code -
" 字符编码{{{
" Vim显示的编码(设置这个不会改变文件的编码){
if has('win32') || has('win64')
set encoding=utf-8
set termencoding=chinese
endif
" }
" 编辑已存在的文件时的参考文件编码.需要注意顺序,前面的字符集应该比后面的字符集大{
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
" }
" }}}
Open your text file with gvim. You should be able to read Chinese now. Congratulations!
Solution 2: download WinVi from http://www.winvi.de/en/email.html . Chinese can be read in WinVi.
Solution 3: install Cygwin. Choose vim when installing Cygwin. Run Cygwin. Open your file with vim.