Vim remaps only work if inside of a function - vim

I'm attempting to enable navigation based on displayed lines in VIM with set wrap linebreak enabled.
I have this function, which will toggle line wrapping with <Leader-W>
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
This works perfectly. But I want to have wrap and linebreak enabled all the time and have this navigation mappings work. So I removed the function and left this.
set wrap linebreak nolist
set virtualedit=
set 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>
Normal mode works fine, but insert mode does not. Any reason why this isn't working outside of the function?

There are two possibilities:
A plugin may override the insert mode mappings; check with :verbose imap <Up>.
Your mappings will only work in the first buffer; for global configuration, drop the <buffer> from the :map commands (like you switched :setlocal to :set).

Related

Update ideavim: remap <ctrl+j> for <b> and <ctrl+l> for <w> not working

New update of plugin ideavim make my remap not working, how can I resolve it ?
He now make some tab indentation, I don't realy understand what is going on.
I just want to move on previous word typing <C-h> & <C-l> to move on next one.
Here is my .ideavimrc file:
" -------------------------------------------------------------------------------
" Navigation
" -------------------------------------------------------------------------------
" Go to next/previous word
nnoremap <C-h> <b>
nnoremap <C-l> <w>
vnoremap <C-h> <b>
vnoremap <C-l> <w>
nnoremap <C-S-h> <B>
nnoremap <C-S-l> <W>
vnoremap <C-S-h> <B>
vnoremap <C-S-l> <W>
" Insert Mode
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
cnoremap <C-h> <Left>
cnoremap <C-j> <Down>
cnoremap <C-k> <Up>
cnoremap <C-l> <Right>
Someone find the solution here:
Github forum

Making commands work only for chosen filetype(s)

I want to map F2 to compiling tex files, and F3 for viewing tex files.
This is what I put in my .vimrc:
if &filetype == "tex"
nnoremap <F2> :Latexmk<cr>
inoremap <F2> <Esc>:Latexmk<cr>a
nnoremap <F3> :LatexView<cr>
inoremap <F3> <Esc>:LatexView<cr>a
endif
These Latex* commands are from LaTeX-Box plugin. I can execute them manually without any problems. Typing :echo &filetype in any *.tex file returns tex.
You have two methods…
Create a self-clearing group in your vimrc and add as many autocommands as needed:
augroup Tex
autocmd!
autocmd FileType tex nnoremap <buffer> <F2> :Latexmk<cr>
autocmd FileType tex nnoremap <buffer> <F3> :LatexView<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
autocmd FileType tex inoremap <buffer> <F3> <Esc>:LatexView<cr>a
augroup END
Use a ftplugin:
Put the following in after/ftplugin/tex.vim:
nnoremap <buffer> <F2> :Latexmk<cr>
nnoremap <buffer> <F3> :LatexView<cr>
inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
inoremap <buffer> <F3> <Esc>:LatexView<cr>a
The second method is recommended because it is a lot cleaner and less expensive than the first.
I replaced the code above with this:
autocmd FileType tex nnoremap <buffer> <F2> :Latexmk<cr>
autocmd FileType tex nnoremap <buffer> <F3> :LatexView<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
autocmd FileType tex inoremap <buffer> <F3> <Esc>:LatexView<cr>a

Vim: mapping to switch window and fill screen

I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:
nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l
These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:
nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|
But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.
I have also tried using :wincmd:
nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>
But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.
Any ideas?
Try using <Bar> instead of |. ie:
nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:
nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|
On the other hand | can be useful:
nnoremap xxx :if 1 == 2 | echom "hello" | endif

Navigate between soft lines in Vim

I have the following one-line text input that's broken into several soft line wraps.
When I press j, I'd go straight to the next hard line, line 2. How do I navigate among soft line wraps?
Use gj to go down and gk to go up by visual lines instead of hard lines.
put that to your .vimrc:
map <silent> <Up> gk
imap <silent> <Up> <C-o>gk
map <silent> <Down> gj
imap <silent> <Down> <C-o>gj
map <silent> <home> g<home>
imap <silent> <home> <C-o>g<home>
map <silent> <End> g<End>
imap <silent> <End> <C-o>g<End>

vim: Having trouble rebinding Escape to jj

All my other keybindings are working correctly, but I can't bind jj to escape for some reason. Mashing jk or kj doesn't work either. Here is my entire .vimrc:
"Maps for jj to act as Esc
inoremap jk <Esc>
inoremap kj <Esc>
"inoremap jj <Esc>
ino jj <Esc>
cno jj <C-c>
set number
set nocompatible
set paste
"fix cygwin backspace problem
set backspace=indent,eol,start
fixdel
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
"use spaces instead of \t
"set expandtab
set nowrap
syntax on
highlight ExtraWhitespace ctermbg=darkgreen guibg=lightgreen
autocmd Syntax * syn match ExtraWhitespace /\s\+$\| \+\ze\t/
"remove trailing whitespace
"http://vim.wikia.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace
"autocmd BufWritePre * :%s/\s\+$//e
autocmd BufWritePre *.c :%s/\s\+$//e
autocmd BufWritePre *.cpp :%s/\s\+$//e
autocmd BufWritePre *.c++ :%s/\s\+$//e
autocmd BufWritePre *.h :%s/\s\+$//e
autocmd BufWritePre *.java :%s/\s\+$//e
autocmd BufWritePre *.php :%s/\s\+$//e
autocmd BufWritePre *.pl :%s/\s\+$//e
autocmd BufWritePre *.py :%s/\s\+$//e
"autocmd FileType c,cpp,c++,java,php,pl,py autocmd BufWritePre <buffer> :call setline(1,map(getline(1,"$"),'substitute(v:val,
"search options
set incsearch
set ignorecase
set showmatch
nmap <space> zz
nmap n nzz
nmap N Nzz
"set arrow keys to move between buffer / tabs
inoremap <Up> :bprev<CR>
inoremap <Down> :bnext<CR>
inoremap <Left> :tabprev<CR>
inoremap <Right> :tabnext<CR>
noremap <Up> :bprev<CR>
noremap <Down> :bnext<CR>
noremap <Left> :tabprev<CR>
noremap <Right> :tabnext<CR>
set vb t_vb=
set guioptions-=T
"set foldmethod=indent
set showtabline=2
"au BufWinLeave * mkview
"au BufWinEnter * silent loadview
For the record, timeoutlen is set to the default of 1000 ms.
Just found the answer researching an unrelated .vimrc question. Using set paste disables insert abbreviations, even if it never actually every worked for me in .vimrc.
You want inoremap jj <Esc>, which you seem to have commented. Uncomment it and comment/delete the other mappings for jj. Does that work?

Resources