Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vim tips and recommendations


May 24, 2021 Vim


Table of contents


Tips and suggestions

Displays the line number

Use :set number display the line number. The absolute line number is displayed by default, :set relativenumber

Jump :_行号_ by _行号_gg : _gg or line number. Jumps are recorded in a jump list, for more :h jump-motions

Spell check

Vim has spell checking, which is turned on with the following command:

set spell

Vim only has an English dictionary installed by default. O ther dictionaries can be found by searching for vim-spell in the Official Software Warehouse. Check the available language packs:

# pacman -Ss vim-spell

Additional dictionaries can be obtained from vim's FTP archive. Put the downloaded dictionary file ~/.vim/spell/ open :setlocal spell spelllang=_en_us_ _en_us_ with the name of the dictionary you want).

Behavior Shortcuts
The next spelling mistake ]s
The last spelling mistake [ s
Spelling correction suggestions z=
Add the word to the user's correct dictionary zg
Add the word to the internal correct dictionary zG
Add words to the user error dictionary zw
Add the word to the internal correct dictionary zW
Re-check the spelling :spellr

Tips:

  • If you need spelling checking for two languages ~/.vimrc English and German), add set spelllang=_en,de_ in .vimrc or /etc/vimrc and restart Vim.

  • With the FileType plug-in and self-built rules for file type detection, you can turn on spell checking for any file type. For example, to turn on .txt a file with the extension .txt, create a file /usr/share/vim/vimfiles/ftdetect/plaintext.vim add autocmd BufRead,BufNewFile *.txt setfiletype plaintext and then add autocmd FileType plaintext setlocal spell spelllang=en_us /etc/vimrc en_us and restart vim. ~/.vimrc

  • If you want to use spell checking only for LaTeX (or TeX) documents, just restart Vim by adding autocmd FileType **tex** setlocal spell spelllang=_en_us_ /etc/vimrc _en_us_. ~/.vimrc For non-English languages, replace the en_us en_us above as the appropriate language code.

Record the cursor position

Vim can record the cursor position the last time a file was opened and move the cursor to that location the next time the same file is opened. To turn this on, add the following to your profile , ~/.vimrc

augroup resCur
  autocmd!
  autocmd BufReadPost * call setpos(".", getpos("'\""))
augroup END

See also: the relevant content on the Vim Wiki.

Replace vi with vim

Create an alias, as follows:

alias vi=vim

Or, if you sudo vi and get vim install vi-vim-symlink AUR, vi and replace it with a symbolic link vim

DOS/Windows carriage return issue

When you open a text file created under MS-DOS or Windows, you often end each line with a ^M This is because Linux uses Unix-style line breaks to represent the end of a line with a line break (LF), but in Windows, MS-DOS, one carriage return (CR) after another line break (LF), so the carriage return character is displayed ^M

You can use the following command to remove carriage returns from the file:

:%s/^M//g

Note that ^ represented by the . The way to enter the ^M is Ctrl+v,Ctrl+m

Another workaround is to install dos2unix and then perform dos2unix <文件名> .

The space at the bottom of the gVim window

If Window Manager is set to ignore window-sized rendering windows, gVim fills the blank space with a GTK theme background color, which can look ugly.

The solution is to resize the space that gVim reserves at the bottom of the window. Add the following code ~/.vimrc

set guiheadroom=0

Note: If you set it to 0, you won't see the horizontal scroll bar at the bottom.