Go to a specific relative number line in Neovim - vim

I am trying to use relative line numbers with NeoVim to jump through my file so I am expecting to digit to jump 20 lines before:
:20k
and to jump 20 lines after:
:20j
I tried but nothing happens.
So I tried to add this configuration to my init.v file:
nnoremap <expr> j v:count ? 'j' : 'gj'
nnoremap <expr> k v:count ? 'k' : 'gk'
But when I insert something like: :20j it gives me this error:
E471: Argument required
How can I achieve my goal?

You can achieve your goal by learning properly how to use your tools. I don't know if Neovim still has it but Vim comes with $ vimtutor for the basics and you can still probably follow :help user-manual.
Case in point, it is 20j, not :20j.

Related

Getting 'E488: Trailing characters' With the following function

Does anyone know why I'm getting the following error? I'm mapping the following function
function! ToggleCompileErrors()
:if w:syntastic_is_open == 1
:call SyntasticReset()<CR>
let w:syntastic_is_open = 0
:else
:call SyntasticCheck()<CR>
let w:syntastic_is_open = 1
:endif
endfunction
With this command
command ToggleCompileErrors :call ToggleCompileErrors()
and it is getting called by the following keymapping
nnoremap <Leader>b :ToggleCompileErrors<CR>
And I don't know if it makes a difference but I am using neovim 0.2.2
Watch your modes. That <CR> :help key-notation is necessary for mappings, but not inside functions, which use Ex commands.
Likewise, you don't need to prefix commands inside a function with : (and this is rather odd here, especially because you're not even consistent about it). : is a normal mode command that enters command-line mode. As commands in a function already are Ex commands, the : is not needed.

How to map a key in a command line argument inside a key mapping?

The naive approach
nnoremap <leader>s :!echo "foo" <bar> vim -c="nnoremap x :q<cr>" -<cr>
displays errors. Is there a way to achieve this?
Figured it out. There are two problems with my original code. The first being an equals sign after -c. For the other, the solution is to use <lt>cr> instead of <cr>. So the entire command would look like this:
nnoremap <leader>s :!echo "foo" <bar> vim -c "nnoremap x :q<lt>cr>" -<cr>
:help <lt>

Passing line number from vim to external command

I am using VIM and I would like to pass the current line number to an external program.
something like this:
map <F3> :!mycmd <linenumber><CR>
I tried to substitute <linenumber> for line('.'), line("."), . and others, but nothing seems to work.
Thanks.
You must use :execute to use a variable or expression in your mapping:
nnoremap <F3> :execute ":!echo " . line('.')<CR>

Gvim, Usage of registers in command

I want to use registers value in command line and in function in GVIM.
More specificaly my goal is to ease the on-the-fly highlithing of my log file. I want to be able to hightlight by a simple command the selected string like the * touch do.
Currently I have these line in my vimrc
hi Hi1 guifg=#FFD000
hi Hi2 guifg=#FA0020
hi Hi3 guifg=#A5BB00
To highlith I write in the command line:
:syntax match Hi1 /MON_STRING/
and I obtain what I want. I would like to have a command like
noremap ,H1 :syntax math Hi1 "0
to do the same more easyly but I haven't find the right way to write "0
Any idea?
EDIT =>
Thanks to the answer I create the function that I was looking for:
let g:hicounter = 0
function TT(incr)
if a:incr == 1
let g:hicounter = g:hicounter + 1
let g:hicounter = g:hicounter % 18
endif
:exec 'syntax match Hi' . g:hicounter .' /'.#/.'/'
:echo "falue de incr a:incr " . a:incr . " :let g:hicounter ="
endfunction
noremap <F3> *#:call TT(1)<CR>
noremap <s-F3> *#:call TT(0)<CR>
use execute
:exec 'your command'.#x
x is the reg
. concatenating the cmd strings
Your approach uses :syntax match; that can interfere with the existing syntax (so sometimes, your highlighting won't match on existing syntax, or it prevents the existing syntax to properly highlight the file). For a more robust highlighting, :match / matchadd() is better (but more complex to manage).
If you're willing to install a plugin, my Mark plugin provides this functionality. (The plugin page has links to alternative plugins.)

Abbreviate Import and Drop command of Golang in Vim

I've used Vim to code my Go script for a month, and I'm pretty comfortable with :Import and :Drop vim command when it comes to importing and unimporting any packages.
However, I've more and more tired of typing such a long word which includes Capital letter so frequently, so I came to think that if I can use :i and :d other than :Import and :Drop, I'd be satisfied even more. However, I'm not sure whether I can define such a command in Vim, since it involves 1) turning into command-line mode, and 2) taking a variable which is determined dynamically in typing.
So for example, if I import encoding/csv package, all I want to type in is :i encoding/csv, etc... Can I define those commands?
I stumbled upon this question, but I cannot get what it is doing nor I'm not sure whether the answer applies to this question in the first place. Can I solve the issue by replacing all of w and W on the linked post to i or d?
Also, does it differ from the case that doesn't take a variable (say, Fmt command to go-format the script)?
Thanks.
EDIT 2: As pointed out by glts below, it's better to use cnoreabbrev or cnorea as pointed out in this answer.
Better working example (paste into ~/.vimrc):
cnorea <expr> i ((getcmdtype() is# ':' && getcmdline() is# 'i')?('Import'):('i'))
cnorea <expr> d ((getcmdtype() is# ':' && getcmdline() is# 'd')?('Drop'):('d'))
EDIT: Simple answer. Just using cabbrev or ca (command abbreviation) seems to work:
WORKING EXAMPLE (Paste this in ~/.vimrc):
ca i Import
ca d Drop
Working on vim 7.3, Ubuntu 64 bit.
ORIGINAL ANSWER (more complex):
According to http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev :
You can use :command to define your own commands, but user-defined commands must start with an uppercase letter to avoid confusion with built-in commands.
So, using :command, you can probably use :I and :D, but not :i and :d.
It goes on to say:
Suppose you have a user-defined :E command that you want to use to override the default :e command. You could do the following:
:cabbrev e <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'E' : 'e')<CR>
The (getcmdtype()==':' && getcmdpos()) makes sure the replacement happens only in the first column of the command line (i.e. not later in the line, where it is most likely NOT intended to be used as a command, and not on the search line, which is also affected by cabbrev).
If you do this a lot, it would be useful to define a function to do it for you. Use this to quickly and easily define lowercase abbreviations for whatever command you want:
function! CommandCabbr(abbreviation, expansion)
execute 'cabbr ' . a:abbreviation . ' <c-r>=getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"<CR>'
endfunction
command! -nargs=+ CommandCabbr call CommandCabbr(<f-args>)
" Use it on itself to define a simpler abbreviation for itself.
CommandCabbr ccab CommandCabbr
This not only creates the function, but also provides the (lowercase!) command :ccab to define such abbreviations "on the fly".
So using a function looks like one way to go if you want to use lowercase :i and :d.
The cmdalias.vim - Create aliases for Vim commands plugin allows you to set up lowercase aliases, e.g.:
:Alias i Import
A simple solution:
nnoremap <leader>i :Import<Space>
nnoremap <leader>d :Drop<Space>
In normal mode, <leader>i populates the command-line with
:Import | <--- cursor
ready for you to type in or <tab>-complete an argument.
Better:
nnoremap <leader>i :Import <C-d>
nnoremap <leader>d :Drop <C-d>
<leader>i populates the command-line with
:Import | <--- cursor
and shows a list of possible completions.
Even better:
set wildcharm=<C-z>
nnoremap <leader>i :Import <C-z>
nnoremap <leader>d :Drop <C-z>
Supposing you have wildmenu already setup, <leader>i populates the command-line with
:Import firstpackagenameinthelist
with the wildmenu open and ready for <tab>bing.

Resources