How to combine :b and :ls functionality in Vim? - vim

In Vim, is there a way to make :b list the buffer numbers next to open buffers, in a way similar to the :ls command but without having to retype :b afterwards?

This great mapping, popularized by a most valuable member of the community, does exactly what you want:
nnoremap gb :ls<CR>:b
A more generic approach…
Vim uses that non-interactive list to display the result of a number of other useful commands. I've written this simple function to insert the right "stub" each time I press <CR> after one of those commands:
function! CmdCR()
" grab the content of the command line
let cmdline = getcmdline()
if cmdline =~ '\C^ls'
" like :ls but prompts for a buffer command
return "\<CR>:b"
elseif cmdline =~ '/#$'
" like :g//# but prompts for a command
return "\<CR>:"
elseif cmdline =~ '\v\C^(dli|il)'
" like :dlist or :ilist but prompts for a count for :djump or :ijump
return "\<CR>:" . cmdline[0] . "jump " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
elseif cmdline =~ '\v\C^(cli|lli)'
" like :clist or :llist but prompts for an error/location number
return "\<CR>:silent " . repeat(cmdline[0], 2) . "\<Space>"
elseif cmdline =~ '\C^old'
" like :oldfiles but prompts for an old file to edit
return "\<CR>:edit #<"
else
" default to a regular <CR>
return "\<CR>"
endif
endfunction
cnoremap <expr> <CR> CmdCR()

Related

Run J script as if it were line-by-line entered into the interpreter

Is there a way to run the script as if it were typed into the interpeter? The benefits are that I don't need echos everywhere, the work done is saved as a file, and I can use vim to do the editing. Example:
example.ijs
x =. 1
x + 3
terminal
x =. 1
x + 3
4
If not, I'll write a vimscript that can do the above then share them here. The advantage of a vimscript solution is that I could have commands to run the entire file, the current line, the current selection, everything up to and including the current line, or whatever else is useful.
Related but not a duplicate: How to call J (ijconsole) with a script automatically
It sounds like you are asking for this to be solved for the jconsole interface. I don't have answer for that, but would point out that that functionality is available for both the JHS and jQt interfaces. If you don't mind switching to a different interface then that would be a quick and easy solution.
The easiest way to interactively run a script is to use labs command:
load'labs/lab'
lab'myscript.ijt'
1 of 2 in myscript.ijt
x =. 1
NB. press Ctrl+'.' to advance.
x + 3
4
NB. Run the whole script.
lab 1 _
x =. 1
x + 3
4
More info about labs here
If you want to non-interactively run a script as if typed in the console, you can just feed the script to j (in linux):
j < myscript.ijs
4
Alternatively, to
see the lines on the screen just as though they had been typed from the keyboard
from a script, you can use 0!:1:
0!:1 < 'myscript.ijs'
x =. 1
x + 3
4
Use loadd rather than load to run the script and display the lines and results.
loadd 'example.ijs'
x =. 1
x + 3
4
This is the vimscript solution I mentioned. It's as far as I can tell language agnostic as well.
" Global variable dictates whether new terminals are opened
" horizontally ('h') or vertically ('v').
let g:terminalsplit = 'h'
" Add execution strings for each language you use.
augroup terminalcommands
autocmd!
autocmd Filetype j let g:cmdstr = 'jconsole.cmd'
autocmd Filetype python let g:cmdstr = 'python'
augroup END
" Close all terminals
nnoremap <silent> <leader>p :call CloseTerminal()<cr>
" Run file
nnoremap <leader>h :call Run(g:cmdstr, 'script')<cr>
" Run as if file were entered line-by-line into the interpreter
" Mappings for: Line, selection, file up to line, entire file
nnoremap <leader>j yy:call Run(g:cmdstr, 'interpreter')<cr>
vnoremap <leader>j ygv<esc>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>k Vggy<c-O>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>l mzggVGy'z:call Run(g:cmdstr, 'interpreter')<cr>
function! Run(cmdstr, mode)
let filepath = expand('%:p') " Copy filepath before switch to terminal
call CloseTerminal()
call OpenTerminal()
echo g:clear . " & " . a:cmdstr
call feedkeys(g:clear . " & " . a:cmdstr) " Begin run command
call RunCode(filepath, a:mode)
call feedkeys("\<c-w>p") " Switch back to file window
endfunction
function! CloseTerminal()
if has('nvim')
let terminals = split(execute('filter/term:/ls'), '\n')
else
let terminals = split(execute('filter/!/ls'), '\n')
endif
for i in range(len(terminals))
silent! exe "bd! " . split(terminals[i], ' ')[0]
endfor
endfunction
function! OpenTerminal()
if g:terminalsplit == 'h'
terminal
elseif g:terminalsplit == 'v'
vertical terminal
else
echo 'g:terminalsplit=' . &g:terminalsplit . '. Must be "h" or "v".'
endif
endfunction
function! RunCode(filepath, mode)
if a:mode == 'script'
call feedkeys(" " . a:filepath . "\<cr>")
elseif a:mode == 'interpreter'
call feedkeys("\<cr>")
call feedkeys("\<c-w>\"\"")
else
echo 'a:mode=' . a:mode . '. Must be "script" or "interpreter".'
endif
endfunction
" Use to clear the terminal window before running the script
if has('unix')
let g:clear = 'clear'
else
let g:clear = 'cls'
endif

vim: execute keystroke in function without triggering function

I am trying to write a vim function that conditionally changes the behavior of the enter key. I want the enter key to sometimes indent, other times behave "normally". By normally I mean that if a series of cases doesn't apply, act like the function/mapping doesn't exist. The trouble I'm running into is that I'm using <CR> as my trigger to invoke the function, and thus I'm not sure how to just say "oh, none of these cases apply, execute a <CR> as if this mapping was never defined."
As an example, consider this in my .vimrc, which indents the line if it starts with an a, otherwise triggers a carriage return. (My vimscript is very novice, so this function might not be correct, but I think the idea remains...)
function! SpecialEnter()
let line=getline(".")
if line =~ '\va*'
" at least one space then a valid indentation
normal! >>
else
" just do the regular thing
" echo "in else"
call feedkeys("\<CR>")
endif
endfunction
inoremap <CR> <Esc>:call SpecialEnter()<CR>
This is somewhat simplified from what I'm actually trying to do, but the concept is the same. I'm looking for a way to say "none of my if statements applied, act like this mapping doesn't exist". Is there a way to do this?
You need to give your mapping the <expr> flag. With this, the right hand side of your mapping is evaluated as an expression.
Here is an example taken from my config where I return different prompts for different commands:
cnoremap <expr> <CR> CCR()
" make list-like commands more intuitive
function! CCR()
let cmdline = getcmdline()
command! -bar Z silent set more|delcommand Z
if cmdline =~ '\v\C^(ls|files|buffers)'
" like :ls but prompts for a buffer command
return "\<CR>:b"
elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$'
" like :g//# but prompts for a command
return "\<CR>:"
elseif cmdline =~ '\v\C^(dli|il)'
" like :dlist or :ilist but prompts for a count for :djump or :ijump
return "\<CR>:" . cmdline[0] . "j " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
elseif cmdline =~ '\v\C^(cli|lli)'
" like :clist or :llist but prompts for an error/location number
return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
elseif cmdline =~ '\C^old'
" like :oldfiles but prompts for an old file to edit
set nomore
return "\<CR>:Z|e #<"
elseif cmdline =~ '\C^changes'
" like :changes but prompts for a change to jump to
set nomore
return "\<CR>:Z|norm! g;\<S-Left>"
elseif cmdline =~ '\C^ju'
" like :jumps but prompts for a position to jump to
set nomore
return "\<CR>:Z|norm! \<C-o>\<S-Left>"
elseif cmdline =~ '\C^marks'
" like :marks but prompts for a mark to jump to
return "\<CR>:norm! `"
elseif cmdline =~ '\C^undol'
" like :undolist but prompts for a change to undo
return "\<CR>:u "
else
return "\<CR>"
endif
endfunction

Vim. set command line from a function

I'm trying to write a function that replaces text in all buffers. So I call Ack to search all the matches and next step I want to set into Quickfix command line this code
:QuickFixDoAll %s/foo/boo/gc
Seems like I can only call 'exec' function which runs this command immediately and there is no ablility to edit it or cancel at all
I also tried "input" function to read user input but got this error at runtime
not an editor command
Any ideas?
.vimrc:
function! ReplaceInFiles(o, n)
exec "Ack '" . a:o . "'"
exec "QuickFixDoAll %s/" . a:o . "/" . a:n . "/gc"
endfunction
" QuickFixDoAll
function! QuickFixDoAll(command)
if empty(getqflist())
return
endif
let s:prev_val = ""
for d in getqflist()
let s:curr_val = bufname(d.bufnr)
if (s:curr_val != s:prev_val)
exec "edit " . s:curr_val
exec a:command
endif
let s:prev_val = s:curr_val
endfor
exec "quit"
endfunction
command! -nargs=+ QuickFixDoAll call QuickFixDoAll(<f-args>)
Using input()
This queries both values interactively:
function! ReplaceInFiles()
let l:o = input('search? ')
let l:n = input('replace? ')
exec "Ack '" . l:o . "'"
exec "QuickFixDoAll %s/" . l:o . "/" . l:n . "/gc"
endfunction
nnoremap <Leader>r :call ReplaceInFiles()<CR>
Incomplete mapping
nnoremap <Leader>r :let o = ''<Bar>exec "Ack '" . o . "'"<Bar>exec "QuickFixDoAll %s/" . o . "//gc"<Home><Right><Right><Right><Right><Right><Right><Right><Right><Right>
This one puts the cursor on the right spot for the search. As this value is used twice (Ack and QuickFixDoAll), it is assigned to a variable. After that, move to the end of the command and fill in the replacement in between the //gc.
Custom parsing
The most comfortable option would be a custom command :AckAndSubstAll/search/replacement/. For that, you'd need to parse the two parts in the custom command (like :s does). You could do that with matchstr(), or use ingo#cmdargs#substitute#Parse() from my ingo-library plugin.
First use vim-qargs to copy all files from the quickfix window into Vim's arglist by calling :Qargs.
Then run your replace on all arguments in the arglist by doing :argdo %s/search/replace/gc

How to use vim variables in an external filter command in visual mode?

I'm trying to make a code pretty printer filter (e.g. perltidy) accept arbitrary options depending on vim variables. My goal is to pass project specific options to an external command used as a filter (:!) in visual mode.
The following expresses my intention (the last line is problematic):
" set b:perltidy_options based on dirname of the currently edited file
function! SetProjectVars()
if match(expand("%:p:h"), "/project-foo/") >= 0
let b:perltidy_options = "--profile=$HOME/.perltidyrc-foo --quiet"
elseif match(expand("%:p:h"), "/project-bar/") >= 0
let b:perltidy_options = "--profile=$HOME/.perltidyrc-bar --quiet"
else
let b:perltidy_options = "--quiet"
endif
endfunction
" first set the project specific stuff
autocmd BufRead,BufNewFile * call SetProjectVars()
" then use it
vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter>
However, the last line (vnoremap) is an error in vim, because it expands to:
:'<,'>execute "!perltidy " . b:perltidy_options
and the execute command cannot accept a range.
But I'd like to have this:
:execute "'<,'>!perltidy " . b:perltidy_options
How can I do this?
p.s. My perltidy is configured to act like a unix filter and I use vim 7.3.
You can use <C-\>e and getcmdline() to preserve command-line contents:
vnoremap ,t :<C-\>e'execute '.string(getcmdline()).'."!perltidy " . b:perltidy_options'<CR><CR>
, but in this case I would suggest simpler <C-r>= which purges out the need for :execute:
vnoremap ,t :!perltidy <C-r>=b:perltidy_options<CR><CR>
If you ever want to get rid of a range in command (ex) mode,
CRL-u will do just that.
vnoremap ,t :execute "!perltidy " . b:perltidy_options<Enter>
becomes
vnoremap ,t :<C-u>execute "!perltidy " . b:perltidy_options<CR>
:h c_CTRL-u
Happy vimming,
-Luke

How to delete multiple buffers in Vim?

Assuming I have multiple files opened as buffers in Vim. The files have *.cpp, *.h and some are *.xml. I want to close all the XML files with :bd *.xml. However, Vim does not allow this (E93: More than one match...).
Is there any way to do this?
P.S. I know that :bd file1 file2 file3 works. So can I somehow evaluate *.xml to file1.xml file2.xml file3.xml?
You can use <C-a> to complete all matches. So if you type :bd *.xml and then hit <C-a>, vim will complete the command to :bd file1.xml file2.xml file3.xml.
:3,5bd[elete]
Will delete buffer range from 3 to 5 .
You also can use alternatively use:
:.,$-bd[elete] " to delete buffers from the current one to last but one
:%bd[elete] " to delete all buffers
You can use this.
:exe 'bd '. join(filter(map(copy(range(1, bufnr('$'))), 'bufname(v:val)'), 'v:val =~ "\.xml$"'), ' ')
It should be quite easy to add it to a command.
function! s:BDExt(ext)
let buffers = filter(range(1, bufnr('$')), 'buflisted(v:val) && bufname(v:val) =~ "\.'.a:ext.'$"')
if empty(buffers) |throw "no *.".a:ext." buffer" | endif
exe 'bd '.join(buffers, ' ')
endfunction
command! -nargs=1 BDExt :call s:BDExt(<f-args>)
Try the script below. The example is for "txt", change it as needed, e.g. to "xml".
Modified buffers are not deleted. Press \bd to delete the buffers.
map <Leader>bd :bufdo call <SID>DeleteBufferByExtension("txt")
function! <SID>DeleteBufferByExtension(strExt)
if (matchstr(bufname("%"), ".".a:strExt."$") == ".".a:strExt )
if (! &modified)
bd
endif
endif
endfunction
[Edit]
Same without :bufdo (as requested by Luc Hermitte, see comment below)
map <Leader>bd :call <SID>DeleteBufferByExtension("txt")
function! <SID>DeleteBufferByExtension(strExt)
let s:bufNr = bufnr("$")
while s:bufNr > 0
if buflisted(s:bufNr)
if (matchstr(bufname(s:bufNr), ".".a:strExt."$") == ".".a:strExt )
if getbufvar(s:bufNr, '&modified') == 0
execute "bd ".s:bufNr
endif
endif
endif
let s:bufNr = s:bufNr-1
endwhile
endfunction
I too had a need for this functionality all the time. This is the solution I have in my vimrc.
function! GetBufferList()
return filter(range(1,bufnr('$')), 'buflisted(v:val)')
endfunction
function! GetMatchingBuffers(pattern)
return filter(GetBufferList(), 'bufname(v:val) =~ a:pattern')
endfunction
function! WipeMatchingBuffers(pattern)
let l:matchList = GetMatchingBuffers(a:pattern)
let l:count = len(l:matchList)
if l:count < 1
echo 'No buffers found matching pattern ' . a:pattern
return
endif
if l:count == 1
let l:suffix = ''
else
let l:suffix = 's'
endif
exec 'bw ' . join(l:matchList, ' ')
echo 'Wiped ' . l:count . ' buffer' . l:suffix . '.'
endfunction
command! -nargs=1 BW call WipeMatchingBuffers('<args>')
Now, I can just do :BW regex (e.g. :BW \.cpp$ and wipe all matching buffers that have match that pattern in their pathname.
If you want to delete rather than wipe, you can of course replace exec 'bw ' . join(l:matchList, ' ') with exec 'bd ' . join(l:matchList, ' ')
TAB will only autocomplete one file for you as of Vim 7.4.282
use <c-a> to autocomplete all files.
You can just use:
bd filetype
then just use <c-a> to facilitate the completion of all open files of specified filetype.
for example, you have 1.xml, 2.xml, 3.xml, and 4.xml,
you can do:
bd xml
then press <c-a>
vim will autocomplete for you as follow:
bd 1.xml 2.xml 3.xml 4.xml
you can just press enter to complete the command.
if you have made changes in one of the files mentioned above, do remember to do:
bd! xml
Very simply: use the :bd[elete] command. For example, :bd[elete] buf#1 buf#5 buf#3 will delete the buffers 1, 3, and 5.

Resources