Ok so this question is vaguely related to one of my previous questions:
I want Vim to be able to save and close similarly to Photoshop in regards to buffers?
Basically the solution I found (or gets really close to what I want), is a plugin called BufOnly which basically closes all buffers that have not been modified. So when I have a lot of buffers open and I want to close, I just run this, and then just care of everything that I haven't already. It works well.
But I'm greedy. I want this to execute automatically when I need it to. Basically I would like it so that if I run qa, if qa runs into -->
E73: No write since list change (add ! to override)
Then I want to run
:BufOnly<CR>:bd <cr>
Is there a way to do that?
you can write a function with vim's try-catch mechanism. example:
function! Funk()
try
execute "qa"
let yes = 1
catch /^Vim\%((\a\+)\)\=:E37/
execute "BufOnly"
execute "bd"
endtry
endfunction
this will catch the error :E37 and do the command you want. I don't have that plugin installed, I therefore didn't test with BufOnly. I tested with "h gg", it shows the help page of gg
to call the command, type :call Funk(), of course you could create mapping for that function call.
Related
So here is my mapping:
nnoremap <cr> :nnoremap <lt>cr> :w!<lt>cr>:!tmux send-keys -t :1.1 "py.test --cov=." C-m <lt>cr><lt>cr><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left><left>
It's pretty awesome. What it does is when I first start my vim session (or after reloading a vimrc), and hit enter, I can immediately type the name of the tmux session that I'm in and hit enter again. Assuming that I'm editing my files in the zeroth pane of window 1 and I have a tmux split (horizontal, in my typical case). Subsequent times hitting enter in normal mode will save my active file and launch my py.test tests in another window. That means that I can technically continue coding before my tests have finished passing. I forget who I first got the idea from, but h/t that guy.
Anyhow, you've no doubt noticed that there are a lot of <left>'s in that mapping, because I'd like to start the command out being able to set the session name. But there also might be cases where I'm editing my code in a different window or something, or I need to change my pytest command or something, so I still want the ability to make those modifications.
Is there a way that I can improve this mapping? Maybe by approaching it in a different way altogether?
Your initial mapping builds another mapping command, and uses <Left> to insert the cursor on the right position to complete and then execute it.
An alternative to that would be defining a custom command (e.g. :TestInSession) that takes the variable parts as argument(s). Then, your initial mapping could just build the other mapping, leaving the cursor at the end of :TestInSession, and you would have less clutter in the command-line, and easier editing at the end.
If you need the ability to re-configure your other mapping, you could define an alternative initial mapping that isn't overwritten, e.g.:
:nnoremap <Leader><cr> :nnoremap <lt>cr> ...
Problem:
If the file "tmp.rb" has been modified, the command "e tmp.rb" will fail.
I'm seeking for a command to succeed in both following conditions:
If "tmp.rb" is not in the buffer list, the file will be loaded;
If "tmp.rb" is already in the buffer list and been modified, the
buffer will be loaded(in current window).
Clarification:
Sorry for all the misleadings. Indeed a single command e tmp.rb will achive both as long as the modified "tmp.rb" is not loaded into the current window, in which situation I prefer e tmp.rb does not cause an error and has the same effect as b tmp.rb. Maybe this little concern will make scripting around buffers, windows etc. a little more easily.
The :edit command fails when the buffer is already loaded and modified to prevent you from accidentally overwriting the changes. If you do want to override, use :edit! instead. In many Vim commands, an appended bang (!) forces the command. Alternatively, if you want Vim to ask you for confirmation, use :confirm edit.
I couldn't reproduce your problem.
What I do to try reproduce your problem is
Open tmp.rb with vim, write some thing and save.
In other shell,
$ echo > tmp.rb
Enter :e or e: tmp.rb in vim and it reopen the file without any problem.
If other program is accessing the tmp.rb at the same time this other program maybe are locking the file and because of it you are having the trouble you describe.
The title of your question says "OR" but its body says "AND".
Vim's commands are usually very precise, it's rare to see an ambiguous command that does multiple mutually exclusive things. The number of keys you can use to enter insert mode (iaIAsScCoO) is a perfect example of this.
You should use the appropriate command for each situation:
:e tmp.rb
:b tmp.rb
If you don't mind installing plugins, CtrlP's :CtrlPMixed may help you with its nice abstraction.
I'm using Janus (isn't pivotal to understand the question though). Basically what they do, is rewrite :e to be :Edit to work with NERDTree, but NERDTree doesn't support force opening a file (e.g. e!). So I had written something that rewrites :Edit! to :e!, but I like the benefits of using just :Edit!, so I'm trying to spoof if as if it was already built in. This is what I came up with:
ca Edit! e! <bar> Edit
The only problem, is that you can't do that for files that haven't been saved at least once. But I know doing
ca Edit! bd! <bar> Edit
will work: it will close the unsaved buffer and keep moving like nothing happened. All I need to do is write logic that will use the correct command in the right scenario all the time. Preferably the logic doesn't run on every page: only when I actually run the command should it attempt to figure it out. Any ideas?
Edit: I tried asking the #vim channel (gave me tons of help)... they said using expand("%") and checking to see if it exists might help my case? Don't know, just providing as much detail as I can.
Try this. Define a function like this:
function! CommandAbbrForEdit()
if &modified
return 'bd! | Edit'
endif
return 'e! | Edit'
endfunction
Then define your abbreviation like this:
cabbrev <expr> Edit! CommandAbbrForEdit()
At the end of my .vimrc I want to conditionally launch a command opening my TODO list.
The problem is that I only want that behaviour when I am using vim or gvim.
And currently, I also load that buffer if I launch vim foo.bar, which is pretty inconvenient because I have to switch back to foo.bar
So basically I want to be able to write something like :
if (some condition telling me that I am on the defaut buffer)
silent LaunchTaskList
endif
Well, you always have a buffer when you're running Vim. It sounds like the check you really want to do is based on whether file arguments were given to Vim, and thus the args list is empty. For that, you can use the argc function.
if argc() == 0
...
endif
I am using mvim with plugin JavaScriptLint. It works great.
However sometimes I work on jQuery or other JavaScript libraries and every single time I save the file, I get tons of warning and cursor moves to the very first warning.
What I would like to have is some way to turn this feature On or off. Something like
:set enableJavaScriptLint
:set disableJavaSCriptLint
My vimrc file is here. And the plugin is here . Notce at line number 14 the plugin autodetects if jsl command is present then it gets enabled. I guess I need to write some function to enable or disable that call.
Any help is appreciated.
Not exactly what you have in mind, but
you can remove the autocommand and call the function manually or replace it with a shortcutkey maybe like this:
map <F11> :call JavascriptLint()
you then have to remember to first save the file, and only call it on *.js files though...
Or, you could fix all warnings...it'll stop complaining too ;-)