Opening files in tabs in Macvim - vim

I am trying to learn MacVim with the Janus build. I've done the Vim tutorial and now I want to dive in and create some simple websites. My first project is a site that will convert roman numerals to arabic. But I can't seem to do the simplest thing - create new files in seperate tabs in one window for html, css, and js - using MacVim. I can create blank files from the terminal and then open the finder and drag these files to an open MacVim window and achieve my goal but it seems like a very convoluted approach. What I want to do is launch MacVim and create my blank html file in the open window and then create a blank css file in an adjacent tab and then create a blank js file in a third adjacent tab and then get to work on them. But when I use the command line in MacVim to create a new file (:!mvim roman.html), I get a new window. So I end up with a series of windows instead of a series of tabs. I googled around and it seems like others have had this problem. Their solution is to modify .bashrc or .bash_profie with an alias (MacVim Open File In Existing Window), but when I tried this and attempted to open a file in MacVim, I got a file filled with garbage, not an empty file. I'm also trying to make sense of NERDTRee. Maybe there is a simple solution there but I am just starting to explore it. Any guidance would be appreciated. Thanks.

You didn't ask for that much but here we go…
Drop Janus as soon as you can. This piece of shit gives you a false sense of comfort while actively preventing you from learning how to use Vim and making it a lot harder than necessary to customize it to your liking.
Vim's tabs are not like tabs in other editors: they are not and can't be 1-to-1 proxies for files. In Vim, a file is loaded in a "buffer" and that buffer may or may not be displayed in zero or more "windows", in zero or more "tabs". This particularity will probably bite you one of these days so you'd better get used to deal with buffers.
In Vim, creating a new file works the same as in any editor: you edit a new empty file and, when you are done, you write it to disk.
:e[dit] file.html
(editing)
:w[rite]
Use :sp[lit] file.js to edit file.js in a new horizontally window.
Use :vs[plit] file.html to edit file.html in a new vertical window.
Use :tabe[dit] file.css to edit file.css in a new tab.
If you want to postpone the decision of the filename, :enew, :new, :vnew and :tabnew create empty buffers in place, in an horizontal window, in a vertical one and a tab.
You don't really need to create those files from outside of Vim.
From the Finder, the simplest way to edit a file in MacVim is to right-click on it and choose "Edit in MacVim". Same for three files.
There is a drop menu, somewhere in MacVim's Preferences window, that lets you define the default behavior when MacVim is launched by other programs. Try it.
Since you seem to have installed the mvim script, editing a file in the MacVim GUI is done with $ mvim filename and editing the same file directly in your shell is done with $ mvim -v filename.
You can also open multiples files (even if they don't exist) in their own tabs from your shell:
$ mvim -p file1 file2 file3
Your command, :!mvim filename, does exactly what it's supposed to do: it launches a new MacVim instance.
Vim already comes with a file explorer that does a lot more than NERDTree called Netrw. See :help netrw.
Did I tell you that you shouldn't use tabs if you don't have a solid understanding of what they are and what they do?

I think the command you want is :tabe something.html. That creates a new tab in the current window with the file something.html in it, and will create a new file if that doesn't exist. (Technically it won't create the new file until you save it).
If you like using tabs, it's probably worth your time to read :help tab-page-commands.

Command + T
Creates a new tab in MacVim.

Related

set tab-stop = 2 in vim permanently for a file

How to set the tab size as 2 for a file permanently in vim as whenever I open a file in other editors like nano or upload the file in github then my indentations are all incorrent whenever I try to resize the tab to 2 for an existing file which has all incorrect indentations. The tab-stop=2 does not permanently resizes the tab and I see all incorrect indentation when I open the same file in nano or view it in github.
Tabs don't have an inherent size so it is up to each program to decide how to display them and there is simply no way to guarantee that a tab will always look the same everywhere.
This is precisely the main issue people have with tabs: you can tell $SOME_TOOL and $SOME_OTHER_TOOL that a tab takes two spaces but that setting can't possibly be carried over to every tool.
Modelines are editor-specific (and they are too intrusive anyway) and Editorconfig is not universally supported so there is really no universal solution beyond using spaces for indentation.

UltiSnips doesn't automatically reload changes to snippets file

(Documenting this here because I couldn't find a good answer online.)
When using UltiSnips, the documentation says (2:12 in this screencast) that writing the .snippets file is enough to cause an automatic reload of the snippet. However, this doesn't work for me. What's happening?
I had this question myself, as frequently updating my own .snippets files and not having them immediately available is unpleasant. After some experiments I discovered the answer:
:call UltiSnips#RefreshSnippets()
In case you are curious, I found it by typing :call <C-d> (a very long list, by the way).
However, this command does not update the autocompletion list of YouCompleteMe (which is mostly irrelevant, but sometimes you might want to browse through your options with description next to it).
Looks like the UltiSnips reload applies within a vim instance. So make sure to open & save the snippets file within the vim instance that you want the changes to take effect in.
To help with this, the command :UltiSnipsEdit will open the .snippets file corresponding to your correct file. You can configure how the snippets file opens using this command:
g:UltiSnipsEditSplit Defines how the edit window is opened. Possible
values:
|normal| Default. Opens in the current window.
|tabdo| Opens the window in a new tab.
|horizontal| Splits the window horizontally.
|vertical| Splits the window vertically.
|context| Splits the window vertically or
horizontally depending on context.
Sample usage in .vimrc: let g:UltiSnipsEditSplit="context"
Note that this doesn't work as well if you'd like to make the changes in a different snippets file (e.g. you're working on a cpp file and you want to add a snippet for all c files (and your cpp.snippets file does extends c). If you're in this situation and you're editing your snippets frequently, consider keeping that snippets file open in a tab/pane.
Other suggestions / input welcome!
(from Documentation)
I haven't been able to find a satisfactory answer to this either. Until somebody can post something better, I recommend just using
:e!
This reloads the current window to the most recently-saved file. I know it's not much, but it's what I'm using until I find a better way to do it.

How to change VIM PHP auto formatting options

I have tried googling this extensively, but all I can find are plugins which format code in the author's preferred way. What I would like to do is change the auto format options so that I can setup VIM to use the same formatting as the other editors my team uses.
For example, I would like:
public function test($param)
{
// code here
}
rather than:
public function test($param){
// code here
}
or
public function test($param)
{
// code here
}
Is this possible without a plugin? Are there formatting templates somewhere that I can edit? Thanks :)
Is this possible without a plugin?
Yes.
Are there formatting templates somewhere that I can edit?
Sure. Vim is the most customizable text editor in universe. :)
So, let's start understanding snippets.
Snippets are blocks of text that can be inserted in your text using some pre-defined keys. The idea of snippets is to easily put in your file some chunk of text you use often. Snippets are these "templates" you mentioned.
To use snippets with Vim, you need to install the garbas/vim-snipmate plugin. You probably had it installed, since it seems that you can use them. This plugin search in you .vim folder for .snippets files and open them every time you open a file with predetermined extension. For example, when you create the foo.html file, vim-snipmate plugin searches for the html.snippets file and load it. After that, everytime you type, for example, html and press tab, Vim will write the <html> tag, because in your html.snippets file there's a snippet telling Vim to do so. Every programming language needs its own .snippets file, and loads it at the start. It's common to have a _.snippets file too, that loads with all file extension. It's a global snippet file.
To edit your snippets, you have to find where are your .snippets files. In Linux, open your terminal and type:
cd ~/.vim
find -name *.snippets
And then you'll see where are your snippet files. Assuming they are ~/.vim/snippets, for example, you open your java snippets with a:
vim ~/.vim/snippets/java.snippets
A .snippets file commonly looks like this: java.snippets file
These +-- lines are compressed lines you can expand and contract typing za in normal mode. In the blue line you always see snippet something written. The something is the shortcut you need to type and press tab when you're editing a file to use the snippet. For example in this java.snippets file there is a snippet called snippet po. So, when you're editing a java file, type po and press tab, Vim will inserted protected {}.
Snippets have a simple language, you can understand a lot just by seeing them in the .snippets file and typing them in another one. If you want to understand more about creating snippets, Google about vim snippets, and you'll find lots of stuff about it.
If you find that you don't have snippets in your .vim folder, or have insufficient ones, you can install a lot of excelent scripts with the honza/vim-snippets extension on Github.

vim - How to open multiple windows inside multiple tabs from command line?

I did my best to find the answer to this; my apologies if I missed it.
Some quick background: I'm looking at the protocol parsers in Bro, specifically those that have been created using BinPAC. In each folder I'm interested in, there are multiple files I want to open, using the naming convention {protocol-name}-protocol.pac, and {protocol-name}-analyzer.pac. The best way to view each protocol's files is side-by-side.
Now, what I want to do is have a separate tab open for each pair. That is, the first tab will have {protocol1}-protocol.pac in one window, and {protocol1}-analyzer.pac in an adjacent window, the second tab will have {protocol2}-protocol.pac in one window, and {protocol2}-analyzer.pac in an adjacent window, and so on.
I'm trying to figure out a way to do the above for all of the relevant folders in one command from the command line. I know how to open all the relevant files in separate tabs, or separate windows, but I can't figure out a way to combine these to get the behavior I want. I could do this manually i.e. open one pair, create a new tab and open another pair etc., but that's obnoxious and repetitive, so I'd much rather do it in one command if possible.
Anyone have any idea how to do this? Or if it's even possible?
It should be possible using --remote-tab option of vim
First start a vim server
$ gvim --servername MYSERVER &
Now add your protocol files one by one to the vim server, while adding replace the -protocol in the file name to -analyzer and open it in vs[plit] to get the corresponding analyzer file.
$ find . -name \*.pac -exec gvim --servername MYSERVER --remote-tab "+execute 'vs ' . substitute(expand('%:p'), '-protocol', '-analyzer', '')" {} \;
Refer http://vimdoc.sourceforge.net/htmldoc/remote.html for more details.
Hope this is helpful

I want Vim to be able to save and close similarly to Photoshop in regards to buffers?

This is my issue with Vim: you have it open for a couple of days. You're ready to close vim. You don't necessarily want to save all files... you want to skip any files which don't have modified changes, and you want to be left (or be asked) what you want to do with the remaining buffers with unmodified changes…
For anyone that has used Photoshop, this is very familiar… you use it for a week, and when you close Photoshop, it is really trying to close the application, and skips all files which haven't been touched, let's you chose what you want to do with the remaining files, and then closes itself.
It seems like every time I close Vim, I have to go through this circus of doing :qa, then running into a file, doing :bd!, then doing :qa again, run into a file I want, :w, and it's just a huge pain. There has to be a better way of doing this.
If it isn't already obvious… I have :set hidden in my .vimrc.
How about
:confirm qa
It asks you for each modified file whether to save or abandon it (or all remaining). This is the same behavior that GVIM exhibits when you close it via the X in the window title.
does :xa! solve your problem ?
:xa[ll]! Write all changed buffers, even the ones that are readonly,
and exit Vim. If there are buffers without a file name or
which cannot be written for another reason, Vim will not quit.
I was able to find the plugin BufOnly, and then with the help of someone else on StackOverflow, I got an answer that satisfies me:
https://stackoverflow.com/a/14690570/240287

Resources