I have the following keymaps:
nnoremap <silent> <leader>vn :call Number()<CR>
function! Number()
set number relativenumber
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
augroup END
endfunction
nnoremap <silent> <leader>vN :call NoNumber()<CR>
function! NoNumber()
set number! norelativenumber
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave * set norelativenumber
autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
augroup END
endfunction
But I would like to use only one <leader>vn shortcut for this; i.e. to call NoNumber if Number has already been called. What is the correct way to do this?
It seems like I can use if ... else in Number function, but I'm not sure how to implement it correctly. Or is there some other way?
Just check if the option number is set:
if &number
set nonumber
else
set number
endif
See the docs on using &option.
You can combine if with the mapping:
:nnoremap <silent> <leader>vn :if &number | call NoNumber() | else | call Number() | endif<CR>
Related
So I use Gvim on Window, so i found an .vimrc file and copied on to my
here is the vimrc file
" 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
set rnu
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()
but anyways here are the problem whenever I enter a list or a string that is too long this message pop up.
line too long (83 > 79 characters) [E501]
so i tried to find a solution on the internet but non of it works, so if anyone knows the solution plz help thanks in advance.:)
You might be using a plug-in like Ale to check your files, if that is the case, this might be of help
I've set up some options in my .vimrc so that it toggles to relative line numbering when in visual modes and switches back to absolute when hitting escape. The problem is every time I switch modes, it brings the cursor back to the first column. I am trying to stop this.
My original code is as follows and is inspired by this post.
So my code is
autocmd InsertEnter * :set norelativenumber
autocmd InsertLeave * :set relativenumber
function! SetLineNumbersVisual()
set relativenumber
return
endfunction
function! ResetLineNumbering()
set norelativenumber
return
endfunction
map <Esc> :call ResetLineNumbering()<CR>
vnoremap <silent> <expr> <SID>SetLineNumbersVisual SetLineNumbersVisual()
nnoremap <silent> <script> v v<SID>SetLineNumbersVisual
nnoremap <silent> <script> V V<SID>SetLineNumbersVisual
nnoremap <silent> <script> <C-v> <C-v><SID>SetLineNumbersVisual
augroup LineNumberSwap
autocmd!
autocmd InsertEnter * call ResetLineNumbering()
autocmd InsertLeave * call ResetLineNumbering()
autocmd CursorHold * call ResetLineNumbering()
augroup END
So that code swaps to relative when in any visual mode and then upon exiting brings it back to absolute. What is also does it constantly move the cursor back to the first column.
To fix this I tried determining the cursor position before setting the mode and then forcing the cursor to move back, but does this doesn't seem to work.
function! SetLineNumbersVisual()
let l:cur_pos = getpos(".")
set relativenumber
call cursor(l:cur_pos[1],l:cur_pos[2])
return
endfunction
function! ResetLineNumbering()
let l:cur_pos = getpos(".")
set norelativenumber
call cursor(l:cur_pos[1],l:cur_pos[2])
return
endfunction
Isn't fixing the problem though.
If you don't specify a return expression, 0 is returned, which evaluates to "go to the first character" motion.
You want to return an empty string:
function! SetLineNumbersVisual()
set relativenumber
return ''
endfunction
When I issue a vim command that starts with :%!, such as :%!sort to sort all lines in the buffer, the cursor moves to the first line. How do I preserve the cursor position?
Ultimately, I want to use this command in an autocmd, such as:
augroup filetype_xxx
autocmd!
autocmd BufWrite *.xxx :%!sort
augroup END
Will the same method work in both places?
You can use a mark to remember the current line number (but note that the line contents could change):
augroup filetype_xxx
autocmd!
autocmd BufWrite *.xxx :kk
autocmd BufWrite *.xxx :%!sort
autocmd BufWrite *.xxx :'k
augroup END
I would rather use the Preserve function. Beyond solving your problem in this particular task you can use it for much more.
" preserve function
if !exists('*Preserve')
function! Preserve(command)
try
let l:win_view = winsaveview()
"silent! keepjumps keeppatterns execute a:command
silent! execute 'keeppatterns keepjumps ' . a:command
finally
call winrestview(l:win_view)
endtry
endfunction
endif
augroup filetype_xxx
autocmd!
autocmd BufWrite *.xxx :call Preserve("%!sort")
augroup END
You can also use the "Preserve Function" to perform other useful tasks like:
command! -nargs=0 Reindent :call Preserve('exec "normal! gg=G"')
DelBlankLines')
fun! DelBlankLines() range
if !&binary && &filetype != 'diff'
call Preserve(':%s/\s\+$//e')
call Preserve(':%s/^\n\{2,}/\r/ge')
endif
endfun
endif
command! -nargs=0 DelBlank :call DelBlankLines()
nnoremap <Leader>d :call DelBlankLines()<cr>
" remove trailing spaces
if !exists('*StripTrailingWhitespace')
function! StripTrailingWhitespace()
if !&binary && &filetype != 'diff'
call Preserve(":%s,\\s\\+$,,e")
endif
endfunction
endif
command! Cls call StripTrailingWhitespace()
cnoreabbrev cls Cls
cnoreabbrev StripTrailingSpace Cls
I have follwing in my .vimrc to hightlight all words that matches the one on current cursor
autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))
But sometimes it is a little annoying, so I'd like to map a key to turn on or off it, e.g. <F10>
How can I do this?
Clear the autocommand and remove highlight:
nmap <f8> :autocmd! CursorMoved<cr> :call clearmatches()<cr>
and to turn it back on using a different key:
nmap <f9> :autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))<cr>
Put the following in your .vimrc:
let g:toggleHighlight = 0
function! ToggleHighlight(...)
if a:0 == 1 "toggle behaviour
let g:toggleHighlight = 1 - g:toggleHighlight
endif
if g:toggleHighlight == 0 "normal action, do the hi
silent! exe printf('match Search /\<%s\>/', expand('<cword>'))
else
"do whatever you need to clear the matches
"or nothing at all, since you are not printing the matches
endif
endfunction
autocmd CursorMoved * call ToggleHighlight()
map <F8> :call ToggleHighlight(1)<CR>
The idea is, if you call the function with an argument it changes the behavior to print/no print.
The autocommand just uses the last setting because the function there is called without an argument.
I would like do disable following mapping when I open quickfix window.
map <F5> :ZoomWin<cr>
Did you mean quickfix? If so, there are three ways:
Use <expr> mappings:
nnoremap <expr> <F5> (&buftype is# "quickfix" ? "" : ":\<C-u>ZoomWin\n")
Use BufEnter event to set/restore mapping:
augroup F5Map
autocmd! BufEnter * :if &buftype is# 'quickfix' | nunmap <F5> | else | nnoremap <F5> :<C-u>ZoomWin<CR> | endif
augroup END
Create mapping locally only for buffers where it is needed:
augroup F5Map
autocmd! BufEnter * :if &buftype isnot# 'quickfix' && empty(maparg('<F5>')) | nnoremap <buffer> <F5> :<C-u>ZoomWin<CR> | endif
augroup END
Update: to disable mapping when any of the opened windows contains quickfix buffer use the following:
nnoremap <expr> <F5> (&buftype is# "quickfix" || empty(filter(tabpagebuflist(), 'getbufvar(v:val, "&buftype") is# "quickfix"')) ? ":\<C-u>ZoomWin\n" : "")