Ubuntu 20.04 Vim Configuration Guide: Personalize Your Text Editor

text:

Vim is a powerful and popular text editor widely used on Linux systems. On Ubuntu 20.04, you can personalize your Vim to meet your editing needs through simple configuration. This article will guide you through some common Vim configurations to make your editing experience more efficient and enjoyable.

Here are some important information about Vim editor in Ubuntu:

  1. Install Vim: On Ubuntu, you can use a package manager such as apt to install Vim. Open a terminal and enter the following command:

    sudo apt update

    sudo apt install vim

  2. Start Vim: To start the Vim editor, just enter the "vim" command in the terminal followed by the name of the file you want to edit. For example:

    vim filename.txt

    If the file does not exist, Vim will create a new one.

  3. Vim editor modes: Vim has different modes, and keyboard input behaves differently in each mode. Here are a few common patterns:

    • Normal mode: the default mode, in which you can move the cursor, copy, paste, delete text and other operations.
    • Insert mode: In this mode, you can enter text. Press the "i" or "a" key to enter insert mode, and press the "Esc" key to return to normal mode.
    • Command-line mode: In this mode, various commands can be executed, such as saving files, exiting the editor, etc. Press a colon (:) to enter command line mode.
  4. Basic operation:

    • Move the cursor: In normal mode, you can use the arrow keys or the H, J, K, L keys (corresponding to left, down, up, and right respectively) to move the cursor.
    • Insert text: In normal mode, press the "i" key to enter insert mode, and start typing text.
    • Save and exit: In the command line mode, enter ":w" to save the file, and ":q" to exit the editor. If you want to save and exit, you can type ":wq".
    • Delete text: In normal mode, press the "x" key to delete the character at the current cursor position.
  5. Advanced Features:

    • Undo and Redo: In normal mode, press the "u" key to undo the last operation. Press "Ctrl + r" to redo undone operations.
    • Find and replace: In normal mode, press the "/" key to start searching. After entering the text you want to find, press the "Enter" key to start the search. Press the "n" key to continue looking for the next match. In command line mode, you can use the ":s/old/new/g" command to replace text.
    • Split window: In normal mode, press ":split" to split the window horizontally. Press ":vsplit" to split the window vertically.

Step 1: Open the Vim configuration file

On Ubuntu 20.04, you can edit Vim's main configuration file through the terminal. Open a terminal and enter the following command:

vim ~/.vimrc

This will open an empty file where you can add your configuration options.

Step 2: Basic Configuration Options

Here are some common basic configuration options that you can set to your liking:

  1. Set the line number:

    set number

    This will display line numbers in front of each line for easy reference and orientation.

  2. Enable syntax highlighting:

    syntax on

    By enabling syntax highlighting, Vim will color different elements according to the syntax of the code to improve the readability of the code.

  3. Enable auto-indentation:

    set autoindent

    When you start a new line, Vim will automatically indent to the same position as the previous line, which is convenient for you to write and type code.

  4. Set tabs to 4 spaces:

    set tabstop=4 set shiftwidth=4 set expandtab

    This will expand tabs to 4 spaces and set the indentation level to 4 spaces. This helps keep your code consistent and readable.

Step 3: Advanced Configuration Options

In addition to basic configuration options, Vim also provides many advanced configuration options to meet more complex needs. Here are some examples:

  1. Enable mouse support:

    set mouse=a

    This will enable mouse support, allowing you to use the mouse for cursor positioning, text selection, and more.

  2. Enable file type detection:

    filetype on

    Vim can automatically detect the file type according to the file extension, and load the corresponding syntax highlighting and configuration.

  3. To set a theme (color scheme):

    colorscheme <theme-name>

    By setting themes, you can change Vim's appearance and color scheme. <theme-name>Replace with your favorite theme name, such as "desert", "morning"

Here are some more Vim configuration options for you to further personalize your editor:

  1. Set search highlighting:

    set hlsearch

    This will highlight matching results when searching, making it easier for you to locate the searched content.

  2. Enable automatic bracket matching:

    set showmatch

    When you enter a parenthesis, Vim will automatically highlight the matching parentheses, which is convenient for you to deal with the nesting of parentheses in your code.

  3. Enable autocompletion:

    set omnifunc=syntaxcomplete#Complete

    This will enable Vim's autocompletion feature, which can provide completion suggestions based on the current syntactical context.

  4. Settings are automatically saved:

    set autowrite

    This will make Vim automatically save the file when it loses focus, avoiding losing changes if you accidentally close the editor.

  5. Enable undo branch logging:

    set undofile

    This will make Vim record a branch of the undo history, allowing you to revert to a previous state after an undo operation.

  6. Set the wrapping method:

    set wrap

    This will enable text wrapping, causing long lines to wrap automatically when displayed.

  7. Configure code folding:

    set foldmethod=indent set foldlevel=2

    This will do code folding based on indentation level, making it easy to browse and edit large code files.

The above are just some examples of Vim configuration options, you can further personalize the configuration according to your needs. Remember to save .vimrcthe file and restart Vim for the configuration to take effect.

Guess you like

Origin blog.csdn.net/weixin_53000184/article/details/130798735