Skip to content

Instantly share code, notes, and snippets.

@RyanZurrin
Last active May 17, 2024 03:54
Show Gist options
  • Select an option

  • Save RyanZurrin/777b47381ec2da1ef59c0d2bcffda530 to your computer and use it in GitHub Desktop.

Select an option

Save RyanZurrin/777b47381ec2da1ef59c0d2bcffda530 to your computer and use it in GitHub Desktop.
my favorite .vimrc settings
set number
syntax on
filetype plugin on
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set wrap
set showcmd
set ruler
set cursorline
set mouse=a
set hlsearch
set ignorecase
set smartcase
set incsearch
set relativenumber
nnoremap <F4> :set number! relativenumber!<CR>
nnoremap <F5> :set mouse-=a<CR>
nnoremap <F6> :set mouse=a<CR>

Vim Configuration Guide

This guide explains the purpose of each setting in the provided Vim configuration. This configuration enhances the editing experience in Vim by setting up an environment that supports better navigation, visual aids, and usability features.

Configuration Options Explained

  • filetype plugin on: This command tells Vim to enable detection of the file type and to load the appropriate plugins and indentation rules for that file type. This enables custom behavior based on the type of file you are editing, which is very useful when working with different programming languages or file formats. For example, if you open a Python file, Vim can automatically load Python-specific indentation rules and plugins that improve coding in Python.
  • syntax on: Enables syntax highlighting in Vim. This feature automatically applies color schemes to text based on the syntax of the programming language or content type you are editing, improving readability and making it easier to identify errors, keywords, and other elements of the code.
  • set number: Enables line numbering on the left side of the window, which is useful for reference and navigation.
  • set relativenumber: Displays the line numbers relative to the line with the cursor. This is useful for quickly moving among nearby lines.
  • set autoindent: Automatically indents new lines to match the indentation level of the previous line.
  • set smartindent: Makes indentation smart, useful for coding with proper alignment according to syntax.
  • set tabstop=4: Sets the number of spaces that a tab character represents. Here it is set to 4 spaces.
  • set shiftwidth=4: Sets the number of spaces to use for each step of (auto)indent. Here it is set to 4 spaces.
  • set expandtab: Converts tabs into spaces. This ensures consistency in the file's formatting.
  • set wrap: Allows long lines to wrap to the next line.
  • set showcmd: Displays the command you are typing in the bottom right of the window.
  • set ruler: Shows the cursor position at all times in the status line.
  • set cursorline: Highlights the current line where the cursor is located.
  • set mouse=a: Enables mouse support in all modes (normal, visual, insert, and command-line). Useful for scrolling and resizing windows.
  • set hlsearch: Highlights all matches of search patterns.
  • set ignorecase: Makes searches case-insensitive, allowing for easier text searching.
  • set smartcase: Overrides ignorecase if the search pattern contains uppercase letters, making searches smarter.
  • set incsearch: Shows incremental search results as you type.

Key Bindings

  • <F4>: Toggles both absolute and relative line numbering on or off. This key binding allows you to quickly switch the visibility of line numbers, enabling a cleaner view when needed and providing detailed line references when working on editing or navigating through the document. This toggle is particularly useful for reducing visual clutter during comprehensive reading or editing sessions, while still offering the option to view line numbers for precise code referencing and navigation.
  • <F5>: Disables mouse support, restoring the right-click context menu for copy-pasting using the mouse.
  • <F6>: Re-enables mouse support, useful for navigating files and resizing panes.

Dependencies

  • These settings are generally supported by default in Vim. However, some features like smartindent may behave differently based on other Vim plugins or scripts that are installed.

Note on Mouse Support

Enabling mouse support with set mouse=a can interfere with the terminal's ability to use the normal right-click context menu. This is particularly relevant for copying and pasting text. Use the <F5> and <F6> key bindings to toggle mouse support off and on when you need to use the context menu for copy-pasting.

Conclusion

These settings aim to provide a powerful and efficient editing environment in Vim, especially for programming and text editing. Adjust these settings according to your workflow and environment requirements.

Cutting and pasting lines in Vim can be done using a few simple commands. Here's a step-by-step guide on how to perform these actions:

Cutting (Deleting) Lines

  1. Delete a Single Line:

    • Position the cursor on the line you want to delete.
    • Press dd to delete the line and save it in the register (clipboard).
  2. Delete Multiple Consecutive Lines:

    • Position the cursor on the first line you want to delete.
    • Press d followed by a motion. For example, d2j deletes the line under the cursor and the next two lines down.
    • Alternatively, you can specify the number of lines directly before dd, like 3dd to delete the current line and the next two lines.

Pasting Lines

After you have cut or copied lines into Vim's register, you can paste them:

  1. Paste Below the Cursor:

    • Press p to paste the line(s) from the register below the cursor's current line.
  2. Paste Above the Cursor:

    • Press P (uppercase P) to paste the line(s) from the register above the cursor's current line.

Visual Mode (Alternative Method)

  1. Select and Cut Lines:

    • Enter Visual Line mode by pressing V (uppercase V), which highlights the current line.
    • Use navigation keys (j or k) to select additional lines.
    • Press d to cut the selected lines.
  2. Paste Lines:

    • Move the cursor to where you want to paste the lines.
    • Press p to paste below the cursor or P to paste above the cursor.

Practical Example

Imagine you want to cut the next three lines including the current one and paste them five lines below:

  1. Go to the line where you want to start cutting.
  2. Type 3dd to cut the current line and the next two lines.
  3. Move the cursor five lines down.
  4. Press p to paste the lines below the cursor.

Tips

  • Using Registers: Vim uses registers to hold text; you can use them to cut or copy to specific registers. For example, "a3dd cuts three lines into register a. You can paste them with "ap.
  • Undo and Redo: If you make a mistake, press u to undo the last action or Ctrl-r to redo the last undone action.

Using these commands, you can efficiently manage lines of text in Vim, making it a powerful tool for text editing.

@RyanZurrin
Copy link
Author

This setup allows for user to use mouse as well as sets the F2 key to turn on / off the line numbers which makes it easier when you need to copy from vim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment