I tried:
$ vi +'help|only|set nu|execute "normal! 48G<cr>zt"'
But the zt part doesn't apply. (48 is the Getting Started line)
Could someone please explain why the zt part doesn't work and why this:
$ vi +'help|only|set nu|execute "normal! /^Getting<cr>zt"'
doesn't even apply the search for /^Getting.
You don't need a <cr> after a G command.
vi +'help|only|set nu|execute "normal! 48Gzt"'
works fine for me.
Additionally, you need to specify the Enter character instead of <cr> (which you can write by typing Ctrl-V and then the Enter key) if you want to use search.
The command shows up like this :
vi +'help|only|set nu|execute "normal! /^Getting^Mzt"'
But the ^M is actually the Ctrl-v-Enter binding
Related
I want to map <M-o> to quickly look up buflist from command-mode.
Here's an example normal-mode mapping:
nnoremap <M-o> :ls<CR>
Here's an attempt to do the same for command mode using execute():
cnoremap <M-o> <C-r>=execute(':ls')<CR>
This however simply appends whitespace-separated :ls results to the command line, with no side effect of the :ls table actually showing.
Is there a way to execute a command without leaving command-mode?
Update:
Closest I've come to cracking this using Vimscript is the following:
function! RunCmdFromCmdline(cmd)
let l:cmd_line = getcmdline()
echo execute(a:cmd)
return l:cmd_line
endfunction
cnoremap <M-o> <C-\>eRunCmdFromCmdline(':ls')<CR>
The echo makes it so the results of a:cmd do pop on the screen, however in invisible font. I tried playing with :redraw <bar> in different combinations with no success.
Exiting from the command mode from within a <C-r>= or <C-\>e prompt does not seem possible.
You can save the contents of the current command line in a register, then run ls and paste the contents of the register:
cnoremap <esc>o <home>let #x="<end>"<cr>:ls<cr>:<c-r>x
So if command line looks like this:
:echo 'some'
type <M-o> to get:
:ls
1 %a + "filename" line 1
:echo 'some'
This works. It uses global variables, but arguably this is an acceptable use for them, since there's only a single command-line you can be in at a time.
function! SaveCmdline()
let g:save_cmdline = getcmdline()
let g:save_cmdpos = getcmdpos()
return ''
endfunction
function! RestoreCmdline()
call setcmdpos(g:save_cmdpos)
return g:save_cmdline
endfunction
cnoremap <M-o> <C-\>eSaveCmdline()<CR>ls<CR>:<C-\>eRestoreCmdline()<CR>
This works with whatever quotes or unfinished syntax you have in your command-line, restores the full command-line and the cursor to the position it was before running the command, doesn't mess with any registers (only touches the two global variables) and doesn't mess with command-line history either (see q: command for history.)
It does have one tiny bug, in that cnoremap actually also works when you're typing a search expression (after / or ?) among a few other sub-modes (see getcmdtype() for a list of those.) If you use the mapping there, it will search for "ls" and then restore your search term as an Ex command, oops...
You can probably use getcmdtype() to detect that and possibly only perform the actions while you're typing an Ex command and not in the other modes.
UPDATE: This fixes the last part, only executing the command while in the Ex command-line, not in search or other context. It also makes the interjected command parameterizable.
function! CmdlineExecute(cmd)
if getcmdtype() != ':'
return ''
endif
return "\<C-\>eSaveCmdline()\r".a:cmd.
\ "\r:\<C-\>eRestoreCmdline()\r"
endfunction
cnoremap <expr> <M-o> CmdlineExecute('ls')
(Note this still needs the definition of the two functions above.)
I'm using GVim 8.1 on Windows 10 with no external plugins.
I have the following set up in my .gvimrc file:
let g:build_file_abs_path = fnamemodify(findfile("windows-build.bat", ";."), ":p:h")
" This build script is a basic wrapper for 'clang.exe file.c -o file.exe' style invocation
let &makeprg=g:build_file_abs_path . "\\windows-build.bat"
nnoremap <silent> <C-B> :cd <C-R>=g:build_file_abs_path<CR> <bar> make! <bar> copen <bar> redraw <bar> cd -<CR>
Now, this automatically opens a quickfix window with the correct compiler output. However, when I press ENTER over the error, the cursor jumps to the buffer for the affected file, yet it is completely blank with a single line. Furthermore, this occurs as I use :cn and :cp commands inside the quickfix window. e.g:
Images showing these two states:
before
after
Please note that:
:verbose nmap <CR> returns no mappings, so there is not conflict there.
I would appreciate it if someone could provide some insight as to how to avoid the buffer becoming empty and actually jump to the error in the appropriate file. Many thanks.
Thanks to Christian Brabandt's comment, I was able to solve the issue. I was misunderstanding the distinction between the working directories of vim and the build script. I made the following changes:
let &makeprg="cd " . g:build_file_abs_path . " && windows-build.bat"
nnoremap <silent> <C-B> :make! <bar> copen <bar> redraw <CR>
I want to highlight trailing space at the end of a line in vim. The following command works:
:match Error /\v\s+\n/
But I'm having trouble mapping this to a key:
nnoremap <leader>w :execute "normal! :match Error " . '/\v\s+\n/' . "\<cr>"
Vim responds with:
E114: Missing quote: "\
E15: Invalid expression: "normal! :match Error " . '/\v\s+\n/' . "\
Update:
Having just seen this: http://vim.wikia.com/wiki/Highlight_unwanted_spaces I've got the mapping working, by just writing:
nnoremap <leader>w :match Error /\v\s+\n/<cr>
Can anyone explaing what the problem is with the original execute normal! ... construct which prevents it from working?
your mapping
I think it should be
nnoremap <leader>w :execute "normal! :match Error " . '/\v\s+\n/' . "\<lt>cr>"<cr>
:help <lt> I am not super aware of exactly why this is needed (once upon a time, my executes, too, were not working, as I had \<esc> and \<cr> in my execute "normal! .."s. Just know that in place or <cr>, you need to use \<lt>cr> for <cr> in this construct.
You also need the <cr> at the end to actually run the :execute ".."<cr> command.
easier solution
Also, I think it's easier to just put
set listchars=trail:·
set list
in your vimrc, and then you will be able to see trailing white space.
Personally, I have set listchars=trail:·,tab:»\ ,extends:» in my vimrc.
and an easy regex search (/\s\+$) reveals the white space, which you can easily delete with d$ ("delete 'till end"). Then once you do that, you can press n. to "go to next" then "repeat" the d$.
cool tip
Once you highlight your search pattern, you can even dgn to "select next occurrence and delete it" in one go -- then you can just repeatedly press . until nothing happens ... :help gn. Also, mess around with cgn; it's pretty neat stuff.
Cheers.
I am trying to add this to my vimrc but I am getting problems one it inserts {{{ at the beginning of my vimrc whenever I open it and also it apparently has 'c' in the regiser as the last pressed key so it deletes the first 2 lines when I press j
And when I run the command it complains that
"A{{{\ is missing a quotation mark and is not a command.
This gave me tip. map execute command vim
You don't need use :execute "normal! ..." if you are using nnoremap. This will work.
nnoremap <C-[> A{{{<Esc><CR>
I am trying to execute a normal mode command Ctrl-n or j from Ex mode. Normally, one would do this using the :normal command. For example, the following command moves the cursor one line down.
:normal j
So does the following command. Note: ^N is typed by pressing Ctrl-v Ctrl-n.
:normal ^N
But the following command does not work. This command seems to have no effect on the buffer.
:normal <C-n>
However, when I create a new Ex command for Ctrl-n using the following command, it works!
:command Down <C-n>
Even this works, although normal is redundant here.
:command Down normal <C-n>
Now, I can use the Ex command :Down to move the cursor one line down.
My question is why does the <C-n> syntax not work with the :normal command but works with the :command command?
use :exec and escape the <c-x>:
for example:
:exec "normal \<c-n>"
in fact the instruction you can find in :h :normal help doc:
to use |:execute|, which uses an
expression as argument. This allows the use of
printable characters to represent special characters.
Example: >
:exe "normal \<c-w>\<c-w>"
Your question is probably academic (or you are trying to solve another problem) but, for what it's worth, you can already do :+ and :join.