I am looking to modify Ctrl-G <C-g> in the following way:
Current Behavior:
<C-g>
"test.txt" 89 lines --1%--
What I want is to modify so it's output looks like 1 Ctrl-G 1<C-g>
"~/Documents/test.txt" 89 lines --1%--
if I make the following mapping:
map <C-g> echo expand('%:p')
I get:
/home/me/Documents/test.txt
I would like to push <C-g> and get:
"~/Documents/test.txt" 89 lines --1%--
Can you help me fix my mapping to get desired output?
I am piggy-backing heavily off this question: Display name of the current file in vim? by muthuh
Try
:nnoremap <c-g> 1<c-g>
What about
:noremap <C-g> 1<C-g>
which avoids a recursion?
Related
Im trying to remap :CtrlP to :ff but I cant seem to get it to work.
Here is what I have tried:
nnoremap ff :CtrlP<cr>
nnoremap <ff> :CtrlP<cr>
Would appreciate the help!
To give more context, im trying to use the method :CtrlP when I open my project by doing: vi .. I am using Nerdtree as well.
It's not possible to remap :CtrlP to :ff, because user-defined ex commands must start with a capital letter. You could map it to :Ff by doing the following:
command! Ff CtrlP
If you want to map it as a normal mode command instead of an ex command, then the first choice you've mentioned should do it.
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>
I would like to have the following behavior for a key in vim (for example the F8 key):
Find the most recent ex command starting with :! and repeat it.
For example, if my command history is
(...)
:!python abc.py
/hi
:%s/hi/ho/g
:w
then by pressing F8 I would like vim to run :!python abc.py
:!! repeats the last :!{cmd}. You can map it to F8 like so:
:nnoremap <F8> :!!<CR>
You could try the following mapping:
nnoremap <F8> q:?^!<CR><CR>
Explanation:
q: - open the command-line window
?^! - search for a line starting with !
<CR><CR> - hit enter twice to execute that command
nnoremap <F8> :!<UP><ENTER>
Is there a way to search the list of recently used file in Vim? The list can be displayed using
browse old
but / does not work. I am aware of some plugins (e.g. MRU) but would prefer to not use a plugin.
Here's a short scriptlet that opens the file list in a scratch buffer. As a bonus, it defines a local <Enter> mapping to :edit the current file. With this, you can search with all built-in commands like /:
:new +setl\ buftype=nofile | 0put =v:oldfiles | nnoremap <buffer> <CR> :e <C-r>=getline('.')<CR><CR>
If you really want to avoid a plugin:
:new The old files will be printed into this buffer
:redir #X where X is a temporary register`
:silent echo(v:oldfiles) 'Silent' is there to not actually print onto your screen
:redir END
"Xp paste the temporary register
(optional) Do some regex-fu to put each file on its own line.
Put the above into a function and voila. Also :help redir
It's actually not very hard to write a simple (simplistic?) MRU command with completion that works like :edit or :split:
" this is our 'main' function: it couldn't be simpler
function! MRU(arg)
execute 'edit ' . a:arg
endfunction
" the completion function, again it's very simple
function! MRUComplete(ArgLead, CmdLine, CursorPos)
return filter(copy(v:oldfiles), 'v:val =~ a:ArgLead')
endfunction
" the actual command
" it accepts only one argument
" it's set to use the function above for completion
command! -nargs=1 -complete=customlist,MRUComplete MRU call MRU(<f-args>)
Here is a .vimrc version of code above. Just add following lines to .vimrc and map to desired keys (in my case it is 'o). In addition define patterns to remove "junk" files. Also cursor is placed at the top for convenience.
Most hard thing is to map an Enter inside nested nmap. ^V is the result of doubled Ctrl-V. ^R is the result of Ctrl-V+Ctrl-R. ^M is the result of Ctrl-V+Enter. You need manually repeat those symbols - not just Copy/Paste. Spent hours to understand this magic - so I'm glad to share. This technology lets you add own macroses in .vimrc.
" Browse Old Files
nnoremap <silent> 'o :enew<CR>:set buftype=nofile<CR>:set nobuflisted<CR>:exe "0put =v:oldfiles"<CR>:nmap <buffer> ^V^V^M :e ^V^V^R=getline('.')^V^V^M^V^V^M<CR>:g/\v(stdout\|nerd\|fugitive)/d<CR>:0<CR>
This is my take on Ingo's answer above for my .vimrc:
Opens the old files in either a vertical split or tab, then maps enter to open file under cursor! magic!
" open old files list and map enter to open line
" vertical split
noremap <leader>vv :vnew +setl\ buftype=nofile <bar> 0put =v:oldfiles <bar> nnoremap <lt>buffer> <lt>CR> :e <lt>C-r>=getline('.')<lt>CR><lt>CR><CR><CR>
" in new tab
noremap <leader>vt :tabnew +setl\ buftype=nofile <bar> 0put =v:oldfiles <bar> nnoremap <lt>buffer> <lt>CR> :e <lt>C-r>=getline('.')<lt>CR><lt>CR <CR><CR>
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>