Tag: vim

  • Set vim as default cronjob editor

    Method 1

    vi /etc/bashrc
    

    Find

    export EDITOR="pico"
    export VISUAL="pico"
    

    Replace with

    export EDITOR="vi"
    export VISUAL="vi"
    

    Method 2

    edit .bash_profile

    vi /root/.bash_profile
    

    Add

    export VISUAL=vi
    

    Once added, .bash_profile will look like

    root@server54 [~]# cat .bash_profile
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export VISUAL=vi
    export PATH
    unset USERNAME
    uptime
    root@server54 [~]#
    

    Method 3

    If you need it done one time only, run following commands on command prompt.

    VISUAL=vi; export VISUAL
    
  • Install vim from source on CentOS

    To install vim from source on the CentOS server, run

    yum -y install  ncurses-devel
    cd /usr/local/src
    wget https://github.com/vim/vim/archive/master.zip	
    unzip master.zip
    cd vim-master/src/
    ./configure
    make
    make install

    This will install the latest version of Vim. You can start it with the command

    vim
    

    To get it work with “vi” command, i removed preinstalled vim editor with command

    rpm -e --nodeps  vim-minimal

    Now create a symlink with

    ln -s /usr/local/bin/vim /bin/vi
  • Show line number in vim

    To show line number in vim, use command

    :set number
    

    To hide numbers, type

    :set nonumber
    

    To always show line number, edit file

    vi ~/.vimrc
    

    Add

    set number
    
  • Comment multiple lines in vim

    To comment multiple lines in vim, first select the line in visual mode.

    To change mode to visual mode, type

    v
    

    Now use j/k keys to select lines of text you need to comment.

    To comment

    To comment, use :norm command.

    :norm i#
    

    This will insert # to start of every line.

    When you type :norm, in visual mode, you will see

    :'<,'>norm i#
    

    Uncomment

    To uncomment, select the lines in visual mode. Then run

    :norm x
    

    this will delete first chars from every selected lines.

    Method 2

    You can use CTRL + V, this will select first char only.

    To comment

    1) Get into VISUAL_BLOCK mode by pressing CTRL + V
    2) use j/k keys to select lines to comment.
    3) Press SHIFT + I, now type #
    4) Press ESC

    To uncomment

    1) Get into VISUAL_BLOCK mode by pressing CTRL + V
    2) use j/k keys to select lines to comment.
    3) Press x

  • Vim 8 goes Visual Mode on right click

    Vim 8 shipped with Fedora 25 and Debian 9 when you mouse right click or use middle button to insert text to vim editor, it just go VISUAL mode.

    This is because default changes in Vim 8.

    Solution 1

    The solution is to press SHIFT key before using mouse.

    SHIFT + right mouse button = you will get paste option for copied text.

    SHIFT + middle mouse button (press mouse wheel) = selected text will be pasted to vim.

    SHIFT + INSERT = paste.

    Solution 2

    use vim.tiny, this can be activated by running

    update-alternatives --config vi
    

    Example

    root@debug-instance:~# update-alternatives --config vi
    There are 3 choices for the alternative vi (providing /usr/bin/vi).
    
      Selection    Path                Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/nvim        30        auto mode
      1            /usr/bin/nvim        30        manual mode
      2            /usr/bin/vim.basic   30        manual mode
      3            /usr/bin/vim.tiny    15        manual mode
    
    Press  to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/vi (vi) in manual mode
    root@debug-instance:~# 
    

    Solution 3

    Run command

    :set mouse-=a
    

    to make this permanent, edit ~/.vimrc file, add

    set mouse-=a
    

    See vi