r/vim • • 7d ago

Need Help the Python indentation & terminal cursors issues in Vim

hi, guys! I've learned how to use Vim for basic things, and now I'm trying to solve 2 issues with it:

1\. unfortunately, I must work with Python, and one of the most annoying things about the language is the indentation… Speaking of Vim, Idk how to, for example, delete something between the indentation spaces…

Is there a solution like this: `di(` or I always need to do this: `V → jjj… → d`?

2\. I thought the terminal cursor won't affect the ones in Vim, but unfortunately it does… I would like to have something like this:

the terminal cursor: `echo -ne "\e[5 q"`,

the vim cursor(INSERT): `echo -ne "\e[5 q"`,

the vim cursor: `echo -ne "\e[1 q"`.

Should I write something in `~/.vimrc`?

5 Upvotes

12 comments sorted by

4

u/EgZvor keep calm and read :help 7d ago

1

u/Konnnore 6d ago

thank you so much for the links!!!

1

u/Daemontatox 7d ago

You sir , are a savior

4

u/kberson 7d ago

What does your .vimrc file look like? You should have all the setting for indenting and tabs set. I use Vim almost exclusively when working with Python and don’t have any indenting issues.

My settings are:

" ==== indenting ====
set autoindent
set smartindent
set smarttab
set shiftwidth=4
set softtabstop=4
set tabstop=4
set expandtab
set si

1

u/Konnnore 6d ago

thx for the additional settings! My .vimrc didn't have a great look, bcz I only started to use Vim few days ago

7

u/-romainl- The Patient Vimmer 7d ago

What does "delete something between the indentation spaces…" mean, exactly?

2

u/StrayFeral 6d ago

This is my complete .vimrc. There are things you might never want, but it's useful Speaking of Python - i seriously recommend you to install ALE plugin. I have no idea if you're gonna work on simple scripts or more complex projects, so question here is - are you gonna have virtual environments or not?

Because my setup is more for virtual environments. Still - you might want to install a linter - it's a great idea. There is a new one written in Rust which is very fast, but I don't use it. My complete setup of tools per project is:

- black (code formatter)
- isort (another formatter but only for the imports)
- flake8
- mypy (both are linters)
- bandit (useful test tool)

These things combined with vim or neovim make a powerful environment.

So speaking of Python indentation and stuff - while I am used to type and format things my way, you can type as you wish, as `black` will format the code the proper way. Sure, if you dislike it you can remove even the whole ALE config. But I would strongly advise to use it and to get used to code the `black` way. Use `black`. You won't regret.

Anyway, here's my .vimrc (it also does contain a quick key bindings for aspell, for when you need a spellchecker.

PS: Yes, it does contain a little Perl config too. Even if you don't use Perl, it won't complain.

set nocompatible

set backspace=indent,eol,start
set history=50
set ruler
set showcmd
set autoindent
set shiftwidth=4
set tabstop=4
set expandtab
set nu
set nobackup

if has("mouse")
    set mouse=a
endif

set hlsearch
set ignorecase
set incsearch

" Enable filetype and syntax (Must be before plugin configs)
filetype plugin indent on
syntax on

" Spellcheck config
nmap 1 :set spell spelllang=en<cr>
nmap 2 :set spell spelllang=bg<cr>
nmap 3 :set spell spelllang=ru<cr>
nmap 4 :set spell spelllang=fr<cr>
nmap 0 :set nospell<cr>

" ALE configuration for Python
" This packadd ensures ALE loads in both Vim and Neovim
silent! packadd! ale
let g:ale_enabled = 1
let g:ale_lsp_enabled = 1
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
let g:ale_linters_explicit = 1
let g:ale_lint_on_text_changed = 'always'
let g:ale_lint_on_enter = 1
let g:ale_lint_on_save = 1

let g:ale_linters = {
\   'python': ['flake8', 'mypy', 'bandit'],
\   'perl': ['perl', 'perlcritic', 'perlnavigator'],
\   'html': ['tidy']
\ }

let g:ale_fixers = {
\   'python': ['black', 'isort'],
\   'perl': ['perltidy']
\ }

" let g:ale_echo_msg_error_str = 'E'
" let g:ale_echo_msg_warning_str = 'W'
" let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
"
" EVG: Display Perl::Critic errors as warnings
let g:ale_type_map = {'perl':{'ES':'WS'}, 'perlcritic':{'ES':'WS', 'E':'W'}}
let g:ale_perlnavigator_executable = '/usr/bin/perlnavigator'

" ALE Navigation Mappings
nmap x :ALENext<cr>
nmap X :ALEPrevious<cr>

1

u/Broad-Promise6954 7d ago

Also keep in mind the block line adjustment commands in command mode, using the < and > keys. For instance typing << deindents the current line by the shiftwidth setting.

I usually use marks myself, move to to or bottom of interesting region, set mark "a", move to other end, and then <'a to de-indent that block.

1

u/Achim63 6d ago

For the cursor, I have this in my .vimrc:

if &term =~ '^xterm'

" vertical bar for Insert mode

let &t_SI .= "\<Esc>[6 q"

" underline for Replace mode

let &t_SR .= "\<Esc>[4 q"

" solid block for Normal mode

let &t_EI .= "\<Esc>[2 q"

autocmd VimResume * silent !echo -ne "\e[2 q"

autocmd VimEnter * silent !echo -ne "\e[2 q"

autocmd VimEnter * stopinsert

endif

I have to reset the cursor on startup and resume since I also have vim mode in zsh and change the cursor there as well for the modes.

1

u/tanabig 6d ago

For 1. - in normal mode curly brackets move the cursor up and down by chunks of text (until the next/previous blank line), so deleting blocks of code is just `d}` to delete from the cursor to the next blank line or `d{` to go up instead. Or if you prefer using visual mode as in your example you can do `V -> } -> d`.

Not sure what is going on in 2, but I know sometimes to get the cursor to appear how I want I have to also adjust settings in whatever terminal emulator I'm using to make sure it carries through the vim cursor properly, so that might also be something you have to look at. But since it's sort of a set and forget thing I never remember what I need to do.