vim removes indentation from the current line - vim

Every time I enter the Insert mode in vim using A and press Enter to go to the next line, vim automatically removes all indentation from the current line. For instance, ideally, it should be like this -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
If I press A on line 15 and then <CR>, this is what my vim looks like -
14 echo $sample;
15 $ai = system("python ai.py " + $sample, $retVal);
16
This is my .vimrc -
"General
set nu " Set line numbers
set hlsearch " Set search highlight
set shiftwidth=2
set tabstop=2
"Pathogen
execute pathogen#infect()
call pathogen#helptags()
syntax on
filetype plugin indent on
"ConqueTerm
command Cterm split | ConqueTerm bash
nnoremap <silent> sh :Cterm<CR>
"NerdTree -
"Fs open Nerdtree on the same tab. Nfs opens NerdTree in new tab.
command Fs NERDTree
command Nfs tabedit | NERDTree
nnoremap <silent> fs :Fs<CR>
"TagBar
nnoremap <silent> tt :TagbarToggle<CR>
"Omni Completion
filetype plugin on
filetype indent on
inoremap <C-Space> <C-X><C-o>
"Editor
command Ide NERDTree | TagbarToggle
"Move between split windows
nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>
"Drag words like in Windows
nnoremap <C-Left> b
vnoremap <C-S-Left> b
nnoremap <C-S-Left> gh<C-O>b
inoremap <C-S-Left> <C-\><C-O>gh<C-O>b
nnoremap <C-Right> w
vnoremap <C-S-Right> w
nnoremap <C-S-Right> gh<C-O>w
inoremap <C-S-Right> <C-\><C-O>gh<C-O>wA
" Start Syntastic in passive mode
let g:syntastic_mode_map = { 'mode': 'passive' }
nnoremap <silent> ch :SyntasticCheck<CR>
Any idea what's going on?

You probably want to set autoindent in your vimrc file:
:set ai
You can also verify if autoindent was previously set (for example through the filetype plugins) by checking its value with
:set ai?

Related

Copy from Vim to VSCode causing extra tab

Having copied normaly indented text from Vim into the system clipboard using "+y, I paste it VSCode. Each line that is tab-indented will be indented two-tab width as the picture shows.
Code in Vim:
Code in VSCode copied from Vim:
shiftwidth is set to 4 in my .vimrc and tab size is set to 4 in VSCode.
Below is my full .vimrc:
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-surround'
Plug 'tpope/vim-repeat'
Plug 'jiangmiao/auto-pairs'
" Theme
Plug 'phanviet/vim-monokai-pro'
Plug 'sainnhe/sonokai'
Plug 'crusoexia/vim-monokai'
Plug 'chriskempson/base16-vim'
" Syntax highlighting
Plug 'sheerun/vim-polyglot'
" Master Vim
Plug 'takac/vim-hardtime'
" Autocomplete
Plug 'ycm-core/YouCompleteMe'
" Full path fuzzy file, buffer, mru, tag, ... finder
Plug 'ctrlpvim/ctrlp.vim'
" Move line
Plug 'matze/vim-move'
" Window
Plug 'tpope/vim-vinegar'
" Status bar
Plug 'vim-airline/vim-airline'
" Git tool
Plug 'tpope/vim-fugitive'
" Comment stuff
Plug 'tpope/vim-commentary'
" Syntax checking
Plug 'vim-syntastic/syntastic'
call plug#end()
" Basics
set nocompatible
filetype plugin on
syntax on
set encoding=utf-8
set fileencoding=utf-8
set shiftwidth=4
" Enable autocompletion
set wildmode=longest,list,full
" Comment stuff
autocmd FileType apache setlocal commentstring=#\ %s
autocmd FileType c,cpp,java,lisp setlocal commentstring=//\ %s
" Relative number
set number relativenumber
set nu rnu
" Disable automatic commenting on newline
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
" Splits open at the bottom and the right
set splitbelow splitright
" Shortcutting split navigation
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
" Treat numbers as decimals
set nrformats-=octal
" Theme
set t_Co=256
set background=dark
let g:sonokai_style = 'shusia'
let g:sonokai_enable_italic = 1
let g:sonokai_disable_italic_comment = 1
colorscheme monokai_pro
let g:airline_theme = 'sonokai'
" NERDTree
nmap <F6> :NERDTreeToggle<CR>
" Arrow keys are dead, stupid
noremap <Up> :echo "Arrow keys are dead, stupid!"<CR>
noremap <Down> :echo "Arrow keys are dead, stupid!"<CR>
noremap <Left> :echo "Arrow keys are dead, stupid!"<CR>
noremap <Right> :echo "Arrow keys are dead, stupid!"<CR>
inoremap <Up> <esc>:echo "Arrow keys are dead, stupid!"<CR>
inoremap <Up> <nop>
inoremap <Down> <esc>:echo "Arrow keys are dead, stupid!"<CR>
inoremap <Left> <esc>:echo "Arrow keys are dead, stupid!"<CR>
inoremap <Right> <esc>:echo "Arrow keys are dead, stupid!"<CR>
nnoremap ∆ :m .+1<CR>==
nnoremap ˚ :m .-2<CR>==
inoremap ∆ <Esc>:m .+1<CR>==gi
inoremap ˚ <Esc>:m .-2<CR>==gi
vnoremap ∆ :m '>+1<CR>gv=gv
vnoremap ˚ :m '<-2<CR>gv=gv
" Master Vim
let g:hardtime_default_on = 0
" ctrlp
let g:ctrlp_map = '<c-p>'
My computer's system version is macOS Catalina 10.15.7.
there are 3 settings actually you should use for vim if you want all files with tabs with width 4 spaces
:set shiftwidth=4
:set tabstop=4
:retab

Vim status line won't immediately appear unless i press a key?

I just moved to Vim about 3 weeks ago. I know this is minor problem but i find it somehow annoy me. Whenever i open vim, statusline (or statusbar?) won't immediately appear (+ cursor always positioned at the last line of the screen) unless i press a key (any key). This is my vimrc for reference:
set nocompatible
" Pathogen
filetype off
runtime bundle/vim-pathogen/autoload/pathogen.vim
execute pathogen#infect()
filetype plugin indent on
" Color Schemes
set t_Co=256
syntax on
set background=dark
colorscheme ir_black
" Misc
set history=200
set backspace=indent,eol,start
set mouse=n
set lazyredraw
runtime macros/matchit.vim
" Escape Timeout
set notimeout
set ttimeout
set ttimeoutlen=10
" Backup & Undo
set noswapfile
set backup
set undofile
set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
set undodir=~/.vim/undo//
" Scroll
set scrolloff=3
" Status Bar
set showmode
set showcmd
set ruler
set laststatus=2
set statusline=
" Tabs & Spaces
set shiftwidth=4
set tabstop=4
set softtabstop=4
set expandtab
set shiftround
set smarttab
" Text
set encoding=utf-8
set nowrap
set autoindent
set textwidth=80
set formatoptions=qrn1j
" Splitting
set splitbelow
set splitright
" Buffer
set hidden
" Wildmenu
set wildmenu
set wildmode=list:longest,full
set wildignore+=**/vendor/**
set wildignore+=**/node_modules/**
" Auto-completion
set complete=.,i,w,b,u,t
set omnifunc=
set completeopt=longest,menuone,preview
" Searching + Regex
set ignorecase
set smartcase
set hlsearch
set incsearch
set gdefault
highlight Search cterm=underline
nnoremap / /\v
vnoremap / /\v
nnoremap <space> :noh<cr>
" Mapping
let mapleader=","
noremap \ ,
inoremap jk <esc>
noremap L g_
noremap H ^
" Window Resize
nnoremap <silent> <up> <c-w>+
nnoremap <silent> <down> <c-w>-
nnoremap <silent> <left> <c-w><
nnoremap <silent> <right> <c-w>>
" Disable Arrow Keys
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
" Move by Display Line
noremap j gj
noremap k gk
" Toggles
nnoremap <leader>wl :set wrap!<cr>
nnoremap <leader>n :setlocal number!<cr>
nnoremap <leader>l :set list!<cr>
" Wipe Whitespace
nnoremap <leader>ww mz:%s/\s\+$//<cr>:let #/=''<cr>`z
" Easy Access to Active File Directory
cnoremap <expr> %% getcmdtype() == ':' ? expand('%:h').'/' : '%%'
" Toggles
nnoremap <silent> <leader>n :set number!<cr>
nnoremap <silent> <leader>wl :set wrap!<cr>
" Buffer Navigation
nnoremap <silent> [b :bprevious<cr>
nnoremap <silent> ]b :bnext<cr>
nnoremap <silent> [B :bfirst<cr>
nnoremap <silent> ]B :blat<cr>
" Split Navigation
nnoremap <c-h> <c-w><c-h>
nnoremap <c-j> <c-w><c-j>
nnoremap <c-k> <c-w><c-k>
nnoremap <c-l> <c-w><c-l>
" Netrw
let g:netrw_banner=0
" Grep Ag
if executable("ag")
set grepprg=ag\ --nogroup\ --nocolor\ --ignore-case\ --column
set grepformat=%f:%l:%c:%m,%f:%l:%m
endif
is there something wrong with it?
edit: this is screenshot of what happen with my vim.
before i press a key
Removing lazyredraw from my vimrc fixed this for me as well.
This was with Vim 7.4 on Ubuntu 16.04.

vim Tagbar plugin

I do use vim with Tagbars and The-NERD-tree plugin. I also like to have autochdir enabled. But sometimes, when I switch between files with NERD-tree plugin - Tagbar does not refresh its content and I stick with tags from previous file. If I do "TagbarToggle" again - second Tagbar window appears with current file tags. If I disable autochdir - it seems to work ok. How can I fix this problem? Current .vimrc is attached. Thanks.
syntax on " syntax hl by default
set background=dark " dark background
set shortmess+=I " no startup uganda msg
set showcmd " Show (partial) command in status line.
set showmatch " Show matching brackets.
set incsearch " Incremental search
set autowrite " Automatically save before commands like :next and :make
set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes)
set number " line numbers
set guioptions-=T " no toolbar
set vb t_vb= " no beeps
set ruler " status bar and ruller for each windows
set showmatch " show matching brace
set autoindent " the current line's indent level to set the indent level of new lines)
set smartindent " attempt to intelligently guess the indent level
set smartcase " Do smart case matching
set ignorecase " Do case insensitive matching
set virtualedit=all " This setting allows the cursor to freely roam anywhere it likes in command mode.
"set autochdir " Automaticly changes dir to active
"set autowriteall " Autosave files
"-color scheme-------------------------------------------
"colorscheme ir_black
"-tabs configs-------------------------------------------
set expandtab
set tabstop=4
set showtabline=4
set shiftwidth=4
"-ctags configs------------------------------------------
"search for tag files until root
set tags=tags;/
"-autosave configs---------------------------------------
set autowrite
"-search configs-----------------------------------------
set hlsearch
" seek for selected test
nnoremap * *N
"turn off higliting
nnoremap 8 :nohlsearch<CR>
"nnoremap <S-F8> :nohlsearch<CR>
" search in selected text
vnoremap * y :execute ":let #/=#\""<CR> :execute "set hlsearch"<CR>
"-spellcheck configs-------------------------------------
set spelllang=en_us
nn <F7> :setlocal spell!<CR>
let g:spellfile_URL = 'http://ftp.vim.org/vim/runtime/spell'
"setlocal spell spelllang=en_us
"nn <F7> :setlocal spell! spelllang=en_us<CR>
"imap <F7> <C-o>:setlocal spell! spelllang=en_us<CR>
"it should get the dictionary files
"-move between windows-----------------------------------
nmap <silent> <C-k> :wincmd k<CR>
nmap <silent> <C-j> :wincmd j<CR>
nmap <silent> <C-h> :wincmd h<CR>
nmap <silent> <C-l> :wincmd l<CR>
"move between windows
nnoremap <Tab> <C-W>w
"move between windows backwards
nnoremap <S-Tab> <C-W>W
"-encoding-----------------------------------------------
"set encoding=utf-8
"-show invisible chars-----------------------------------
set listchars=tab:>-,eol:$
nnoremap <F8> :set list!<CR>
":highlight NonText ctermfg=red
"set listchars=tab:▸\ ,eol:¬
"set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
"-Tag bar config-----------------------------------------
nnoremap <F4> :TagbarToggle<CR>
"-buffers------------------------------------------------
"nnoremap <F7> :TMiniBufExplorer<CR>
nnoremap <F5> :ls<CR>
nnoremap <C-n> :bnext<CR>
nnoremap <C-b> :bprev<CR>
nnoremap <C-c> :enew<CR>
nnoremap <C-x> :bp<bar>sp<bar>bn<bar>bd<CR>
"-grep---------------------------------------------------
nnoremap <silent> <F9> :Grep<CR>
"-nerd tree----------------------------------------------
nnoremap <F3> :NERDTreeToggle<CR>
"-clipboard----------------------------------------------
set clipboard=unnamedplus
"-easymotion---------------------------------------------
map <F6> <Leader><Leader>W
"-cscope---------------------------------------------
"-VIM plugins--------------------------------------------
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
filetype plugin indent on
" L9 - vim programming library
Bundle 'L9'
Bundle 'The-NERD-tree'
Bundle 'EasyMotion'
Bundle 'grep.vim'
Bundle 'Tagbar'
Bundle 'chazy/cscope_maps'
"Bundle 'buftabs'
"Bundle 'autoload_cscope.vim'
"Bundle 'chazy/cscope_maps'
"Bundle 'FuzzyFinder'
"Bundle 'tpope/vim-fugitive'
Try updating to Vim 7.4. I had the same problem, as described on this tagbar issue.

Vim mapping persists even after removal from .vimrc

This is a really strange issue and it's been driving me insane. At one point, in my .vimrc file, I had a line that looked like the following:
nnoremap <tab> gg=G''
which I used to re-indent my whole file. Time goes on and I decide that I want to use tab like %, to move between open/close tags/parens/etc. So I remove the line above, and remap the command to control-i (like Eclipse) and replace it with
nnoremap <tab> %
vnoremap <tab> %
I save the file, source it, close vim, restart my computer, do whatever, but regardless of what I do, pressing tab still indents the entire file. When I check the mapping with
:verbose map <tab>
The output I get is:
v <Tab> * %
Last set from ~/.vimrc
n <Tab> * gg=G''
Last set from ~/.vimrc
Similarly, if I try
:verbose nnoremap <tab>
I get:
n <Tab> * gg=G''
Last set from ~/.vimrc
I'm really not sure what's going on here, that mapping most certainly does not exist any more. Here is my .vimrc:
set nocompatible " be iMproved
filetype off " required!
let mapleader = ","
nnoremap <leader><space> :noh<cr>
nnoremap ; :
nnoremap <leader>v <C-w>v<C-w>l
nnoremap <leader>h <C-w>s<C-w>j
nnoremap <leader>f :CtrlP<CR>
nnoremap <tab> %
vnoremap <tab> %
inoremap jk <ESC>l
nnoremap <C-i> gg=G''
nnoremap <up> <nop>
nnoremap <down> <nop>
nnoremap <left> <nop>
nnoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
syntax enable
set t_Co=16
set background=dark
set tabstop=2
set shiftwidth=2
set softtabstop=2
set smarttab
set expandtab
set number
set ignorecase
set smartcase
set gdefault
set incsearch
set showmatch
set hlsearch
set nobackup
set noswapfile
set smartindent
set hidden
set wildmenu
set nonumber
set nowrap
set relativenumber
set timeoutlen=100
set backspace=indent,eol,start
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
" required!
Bundle 'gmarik/vundle'
" My Bundles here:
"
" original repos on github
Bundle 'tpope/vim-rails.git'
Bundle 'tpope/vim-endwise.git'
Bundle 'tpope/vim-surround.git'
Bundle 'scrooloose/nerdcommenter.git'
Bundle 'scrooloose/syntastic.git'
Bundle 'jiangmiao/auto-pairs.git'
Bundle 'kien/ctrlp.vim'
Bundle 'altercation/vim-colors-solarized.git'
colorscheme solarized
" vim-scripts repos
Bundle 'bufexplorer.zip'
Bundle 'HTML-AutoCloseTag'
Bundle 'matchit.zip'
Bundle 'ruby-matchit'
Bundle 'Rename2'
filetype plugin indent on " required!
filetype indent on
"
" Brief help
" :BundleList - list configured bundles
" :BundleInstall(!) - install(update) bundles
" :BundleSearch(!) foo - search(or refresh cache first) for foo
" :BundleClean(!) - confirm(or auto-approve) removal of unused bundles
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Bundle command are not allowed..
<c-i> and <tab> share the same key codes so they can not be distinguished from each other.
Use a different mapping than <c-i>. I suggest you use leader, e.g. nnoremap <leader>i gg=G''
For more help see:
:h keycodes
:h mapleader

Which custom key bindings do you use in Vim? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which custom key bindings do you use to increase productivity in Vim?
My two favorites are:
inoremap jj <Esc>
" Clear screen clears search highlighting.
nnoremap <C-L> :nohl<CR><C-L>
Window Management
" Window splitting
nmap <silent> <leader>sh :leftabove vnew<cr>
nmap <silent> <leader>sl :rightbelow vnew<cr>
nmap <silent> <leader>sk :leftabove new<cr>
nmap <silent> <leader>sj :rightbelow new<cr>
nmap <silent> <leader>swh :topleft vnew<cr>
nmap <silent> <leader>swl :botright vnew<cr>
nmap <silent> <leader>swk :topleft new<cr>
nmap <silent> <leader>swj :botright new<cr>
" Scroll the window next to the current one
" (especially useful for two-window splits)
nmap <silent> <leader>j <c-w>w<c-d><c-w>W
nmap <silent> <leader>k <c-w>w<c-u><c-w>W
Text Editing
" Toggle search highlighting
nmap <silent> <leader>/ :set hlsearch!<cr>
" Toggle paste mode
" (prefer this over 'pastetoggle' to echo the current state)
nmap <leader>p :setlocal paste! paste?<cr>
" Select the last edited (or pasted) text
nmap gv `[v`]
" Keep lines that do (or do not) contain the last search term
nmap <leader>v/ :v/<c-r>//d<cr>gg
nmap <leader>g/ :g/<c-r>//d<cr>gg
" Email (de-)quotation
nmap <leader>q vip:s/^/> /<cr>
vmap <leader>q :s/^/> /<cr>
nmap <leader>Q vip:s/^> //<cr>
vmap <leader>Q :s/^> //<cr>
File Opening and Saving
" Save and restore session
nmap <leader>ss :wa<cr>:mksession! $HOME/.vim/sessions/
nmap <leader>rs :wa<cr>:source $HOME/.vim/sessions/
" Write buffer through sudo
cnoreabbrev w!! w !sudo tee % >/dev/null
" Change the current directory to the directory of the file in buffer
nmap <silent> <leader>cd :cd %:p:h<cr>:pwd<cr>
" Open file located in the same directory as the current one
nmap <leader>e :e <c-r>=expand('%:p:h').'/'<cr>
Grep the word under cursor:
Using the following grepprg options it will search recursively into the current directory excluding and including some specific files.
" Quick Grep
noremap <Leader>g :grep<space><C-r><C-w><CR>:copen<CR><CR><C-W>b
set grepprg=grep\ -nH
\\--include='*.c'
\\--include='*.cpp'
\\--include='*.h'
\\--exclude-dir='.svn'
\\--exclude='*.svn-base'
\\--exclude-dir='OBJ'
\\--exclude='symTbl.c'
\\ $*
\\ -R\ .
It greps the word under cursor, then open the Quickfix Window and move the cursor to the bottow window (which should be the list of grep results)
This is probably one of the shortcut I use the most, and it saves lots of typing !
Moving around quickly between windows
noremap <C-j> <C-W>j
noremap <C-k> <C-W>k
noremap <C-h> <C-W>h
noremap <C-l> <C-W>l
It is quite intuitive and handy to move around when your screen is split horizontally and vertically.
The following command remaps ; to : in command mode, saving you from wasting precious milliseconds holding and releasing the Shift key when typing commands like :wq:
" Remap ";" to ":"
map ; :
noremap ;; ;
If you need to type an actual ;, just press it twice.
Insert mode
" <esc> in normal mode clears highlight
nnoremap <silent> <esc> :noh<cr><esc>
Command Line Editing
" copy an entire word from the line above instead of just one
inoremap <expr> <c-y> matchstr(getline(line('.')-1), '\%' .
\ virtcol('.') . 'v\%(\k\+\\|.\)')
" Insert Directory of current buffer and open completion
cnoremap <expr> <c-k> getcmdline()[getcmdpos()-2] == " " ?
\ expand("%:h") . "/\<c-d>" : "\<c-d>"
let mapleader=","
" omnicompletion : words
inoremap <leader>, <C-x><C-o>
" omnicompletion : filepaths
inoremap <leader>: <C-x><C-f>
" omnicompletion : lines
inoremap <leader>= <C-x><C-l>
" toggle TagList
nnoremap <leader>l :TlistToggle<CR>
" toggle NERDTree
nnoremap <leader>n :NERDTreeToggle<CR>
" I like vertically aligned assignation operators
nnoremap <leader>a :Tabularize<Space>
" with | marking the cursor
" it turns this
" function foo(){|}
" into this
" function foo(){
" |
" }
inoremap <C-Return> <CR><CR><C-o>k<Tab>
" push the current ligne up and down
nnoremap <M-D-Up> ddKp
nnoremap <M-D-Down> ddP
" swap word under the cursor with previous word on the left
" from the Vim wiki
nnoremap <M-D-Left> "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>
" swap word under the cursor with next word on the right
" from the Vim wiki
nnoremap <M-D-Right> "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>/\w\+\_W\+<CR><C-l>
" and I have lusty-explorers "modes" mapped to:
" "files" <leader>f
" "buffers" <leader>b
" "grep" <leader>g
Useful for navigating up/down long lines that wrap to multiple lines on your display: Alt + ↑ or ↓ arrow keys move through screen lines rather than file lines.
map <A-Down> gj
map <A-Up> gk
imap <A-Up> <ESC>gki
imap <A-Down> <ESC>gji

Resources