Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My .vimrc configuration for working in Python with vim
" plugins
let need_to_install_plugins = 0
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
"autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
let need_to_install_plugins = 1
endif
call plug#begin()
Plug 'tpope/vim-sensible'
Plug 'itchyny/lightline.vim'
Plug 'joshdick/onedark.vim'
Plug 'ap/vim-buftabline'
Plug 'airblade/vim-gitgutter'
Plug 'vim-scripts/The-NERD-tree'
Plug 'jistr/vim-nerdtree-tabs'
Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'scrooloose/syntastic'
Plug 'majutsushi/tagbar'
Plug 'vim-scripts/indentpython.vim'
Plug 'lepture/vim-jinja'
Plug 'pangloss/vim-javascript'
call plug#end()
filetype plugin indent on
syntax on
if need_to_install_plugins == 1
echo "Installing plugins..."
silent! PlugInstall
echo "Done!"
q
endif
" always show the status bar
set laststatus=2
" enable 256 colors
set t_Co=256
set t_ut=
" turn on line numbering
set number
" sane text files
set fileformat=unix
set encoding=utf-8
set fileencoding=utf-8
" sane editing
set tabstop=4
set shiftwidth=4
set softtabstop=4
set colorcolumn=80
set expandtab
set viminfo='25,\"50,n~/.viminfo
" word movement
imap <S-Left> <Esc>bi
nmap <S-Left> b
imap <S-Right> <Esc><Right>wi
nmap <S-Right> w
" indent/unindent with tab/shift-tab
nmap <Tab> >>
imap <S-Tab> <Esc><<i
nmap <S-tab> <<
" mouse
set mouse=a
let g:is_mouse_enabled = 1
noremap <silent> <Leader>m :call ToggleMouse()<CR>
function ToggleMouse()
if g:is_mouse_enabled == 1
echo "Mouse OFF"
set mouse=
let g:is_mouse_enabled = 0
else
echo "Mouse ON"
set mouse=a
let g:is_mouse_enabled = 1
endif
endfunction
" color scheme
syntax on
colorscheme onedark
filetype on
filetype plugin indent on
" lightline
set noshowmode
let g:lightline = { 'colorscheme': 'onedark' }
" code folding
set foldmethod=indent
set foldlevel=99
" wrap toggle
setlocal nowrap
noremap <silent> <Leader>w :call ToggleWrap()<CR>
function ToggleWrap()
if &wrap
echo "Wrap OFF"
setlocal nowrap
set virtualedit=all
silent! nunmap <buffer> <Up>
silent! nunmap <buffer> <Down>
silent! nunmap <buffer> <Home>
silent! nunmap <buffer> <End>
silent! iunmap <buffer> <Up>
silent! iunmap <buffer> <Down>
silent! iunmap <buffer> <Home>
silent! iunmap <buffer> <End>
else
echo "Wrap ON"
setlocal wrap linebreak nolist
set virtualedit=
setlocal display+=lastline
noremap <buffer> <silent> <Up> gk
noremap <buffer> <silent> <Down> gj
noremap <buffer> <silent> <Home> g<Home>
noremap <buffer> <silent> <End> g<End>
inoremap <buffer> <silent> <Up> <C-o>gk
inoremap <buffer> <silent> <Down> <C-o>gj
inoremap <buffer> <silent> <Home> <C-o>g<Home>
inoremap <buffer> <silent> <End> <C-o>g<End>
endif
endfunction
" move through split windows
nmap <leader><Up> :wincmd k<CR>
nmap <leader><Down> :wincmd j<CR>
nmap <leader><Left> :wincmd h<CR>
nmap <leader><Right> :wincmd l<CR>
" move through buffers
nmap <leader>[ :bp!<CR>
nmap <leader>] :bn!<CR>
nmap <leader>x :bd<CR>
" restore place in file from previous session
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" file browser
let NERDTreeIgnore = ['\.pyc$', '__pycache__']
let NERDTreeMinimalUI = 1
let g:nerdtree_open = 0
map <leader>n :call NERDTreeToggle()<CR>
function NERDTreeToggle()
NERDTreeTabsToggle
if g:nerdtree_open == 1
let g:nerdtree_open = 0
else
let g:nerdtree_open = 1
wincmd p
endif
endfunction
" syntastic
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 0
let g:syntastic_check_on_wq = 0
map <leader>s :SyntasticCheck<CR>
map <leader>d :SyntasticReset<CR>
map <leader>e :lnext<CR>
map <leader>r :lprev<CR>
" tag list
map <leader>t :TagbarToggle<CR>
" copy, cut and paste
vmap <C-c> "+y
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <ESC>"+pa
" disable autoindent when pasting text
" source: https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode
let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
@rayuduak

This comment has been minimized.

Copy link

rayuduak commented May 8, 2020

Hi
I get this issue on Linux
I added .vimrc in my home
I launched vim or vi , i get this below

Error detected while processing /u/users/ray1234/.vimrc:
line 2:
E15: Invalid expression: 0^M
line 3:
E15: Invalid expression: empty(glob('~/.vim/autoload/plug.vim'))^M
line 184:
E171: Missing :endif
Press ENTER or type command to continue

@miguelgrinberg

This comment has been minimized.

Copy link
Owner Author

miguelgrinberg commented May 8, 2020

What version of vim do you have?

@rayuduak

This comment has been minimized.

Copy link

rayuduak commented May 8, 2020

What version of vim do you have?

VIM - Vi IMproved 7.4
I got it working. Problem was copy paste from windows to linux,
I did a curl and got it working.
The FileManager on left and center workspace works with \n & \t.
but the methods/functions on right does not work , some ctags issue i guess.
But its great for my python work in linux.
I am vim fan.

@pinsard

This comment has been minimized.

Copy link

pinsard commented May 9, 2020

Do yourself a favor and upgrade to 8.1 - nothing to loose there while you'll have many benefits. Check it out here

REgarding your problem, it might be related to end of line characters.

@giuliohome

This comment has been minimized.

Copy link

giuliohome commented May 9, 2020

It seems that my vim can't copy to the system clipboard (I'm in Linux Fedora 31). I have to disable the mouse (and the lateral sections) to use the terminal ctrl+shift+c (quite ugly in comparison to, say, the powerful notepad++ on windows or even the basic write editor - gedit - in my Linux) or do you know a better trick? Indeed, for this reason, I'm using gedit and not vim on Linux when I want to copy some code from a python file to the browser (in a gist like this, for example). Thank you anyway for this awesome configuration!

@miguelgrinberg

This comment has been minimized.

Copy link
Owner Author

miguelgrinberg commented May 10, 2020

If my Ctrl-C/Ctrl-X/Ctrl-V key definitions do not work it is probably because your vim wasn't built with system clipboard support. See https://vim.fandom.com/wiki/Accessing_the_system_clipboard for info on how to check if your vim was built with clipboard support or not.

Now separate from that, if you like your terminal window's mouse based text selection and clipboard usage, you can typically get those to work even while mouse is enabled in vim. On my Mac, for example, I can bypass vim's mouse support and access the iTerm2 text selection by pressing the Option key when selecting text with the mouse. Try selecting text while pressing Alt, Shift, etc. to see if you find a key that works on your Linux machine.

@giuliohome

This comment has been minimized.

Copy link

giuliohome commented May 10, 2020

Oh yes, I could press Shift while selecting with the mouse, you're right (indeed I've also found the same tip here), but in that case I'd better close the left navigator and it's still ugly to see the line numbers.
Well, I've followed this so installed vim-X11 plus started vimx instead of vim and now the + buffer is copying to the clipboard (not the * buffer). So problem solved, somehow.
But I still can't see your Ctrl-C work, even if I've checked that, now, if I select with the mouse and I type "+y it works (so it's strange that your config shortcut is lost). Notice that if, in normal mode, I open the command : and I enter vmap <C-c> "+y, it gets enabled, so I guess it is overwritten at startup, don't know why (o which plugin).
In conclusion, thank you for your competent reply, for this wonderful config and for your valuable and instructive videos, kind regards.

@bmillemathias-1a

This comment has been minimized.

Copy link

bmillemathias-1a commented May 10, 2020

Hello @miguelgrinberg,

Coming from your youtube video, I'm using vim plug as well, and saw you're using tmux, what kind of customisations did you to have both of them working together (the mouse wheel, or the color support for instance) <

Best.

@miguelgrinberg

This comment has been minimized.

Copy link
Owner Author

miguelgrinberg commented May 10, 2020

@bmillemathias-1a I didn't really go too crazy on the tmux + vim config. On the tmux side I have mouse enabled, which makes scrolling with the wheel work nicely. 256 color support is also enabled with xterm-256color. A video on tmux is coming soon, by the way.

@dkadiyala3

This comment has been minimized.

Copy link

dkadiyala3 commented May 12, 2020

Hi,
I am trying to source this .vimrc in my WSL ubuntu and I am getting the following errors. Any suggestions on how to resolve these errors ?

-bash: source: filename argument required
source: usage: source filename [arguments]
-bash: plugins
let need_to_install_plugins = 0
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd: No such file or directory
-bash: let: =: syntax error: operand expected (error token is "=")
endif: command not found
-bash: /home/dkadiyala3/.vimrc: line 13: syntax error near unexpected token (' -bash: /home/dkadiyala3/.vimrc: line 13: call plug#begin()'

@miguelgrinberg

This comment has been minimized.

Copy link
Owner Author

miguelgrinberg commented May 12, 2020

This file is not for sourcing, you just have to put it in your home directory.

@dkadiyala3

This comment has been minimized.

Copy link

dkadiyala3 commented May 12, 2020

@miguelgrinberg Thank you very much!!

@mchlsm

This comment has been minimized.

Copy link

mchlsm commented May 13, 2020

Thank you Miguel for the video and sharing your config file.

I have a question regarding commenting multiple lines of code in vim. I normally just do ctrl + / in VS Code but in vim, some of the solutions I've found take too many keystrokes. There's also several plugins but now I'm just offered too many choices.

What is your process for quickly commenting out multiple lines of code?

@miguelgrinberg

This comment has been minimized.

Copy link
Owner Author

miguelgrinberg commented May 13, 2020

@mchlsm I don't have a shortcut built for this. You have to put the cursor on the place where you want the # to be in the first line. Select Ctrl-V for visual block mode, then down arrow until the bottom line. Then Shift-I to insert before cursor, then type the # and maybe a space after it, and finally ESC.

To uncomment use Ctrl-V down arrow to the bottom line, right arrow if you added a space after the #. Then x to remove the comments.

@mchlsm

This comment has been minimized.

Copy link

mchlsm commented May 14, 2020

@mchlsm I don't have a shortcut built for this. You have to put the cursor on the place where you want the # to be in the first line. Select Ctrl-V for visual block mode, then down arrow until the bottom line. Then Shift-I to insert before cursor, then type the # and maybe a space after it, and finally ESC.

To uncomment use Ctrl-V down arrow to the bottom line, right arrow if you added a space after the #. Then x to remove the comments.

It's a little strange that you don't see the comment marker appear on every line immediately after you do Shift-I and start typing (you only see it on the main line you initially selected). The rest of the comment marks only appear after you press ESC. For me, I had to press ESC twice. I suppose I will just have to do this more to make it a muscle memory.

Uncommenting worked great.

Thank you!

@giuliohome

This comment has been minimized.

Copy link

giuliohome commented May 24, 2020

I was getting an annoying error for gtk c files with gtk.h include, and found this suggestion useful:

let g:syntastic_c_config_file = '.config_c'
let g:syntastic_c_no_include_search = 1

Now it works as expected (notice that also a config_c is needed however)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.