I would like to change some vim parameters so I can see the spaces+eol, and set the tabulations as 4 spaces. I found a few commands that allow me to do that but i have to type the commands every time I wanna use them. Is there a way so save them so they stay even when I close vim?
Put those commands in a vimrc file. Vim loads this on startup and runs the commands in it, as if you had typed them in.
See also: https://vi.stackexchange.com/questions/13398/are-there-any-comprehensive-docs-for-vimrc
Related
I have a text that's around 1000 lines long, and I have a bunch of vim commands I want to apply to it. Lots of regex find-and-replace stuff. For example:
:%s/^[^\$\$]/def
:%s/\$\$/
%s/='/ = ['
I could copy and paste those commands one by one, but that's work I'll have to do again every time I receive a new version of this file. I'm wondering if there's a way to save a long list of commands that I can then apply to the file? Thanks!
registers
You can put them all into a register, either by typing those commands in a scratch buffer and then yanking into a register, or via explicit assignment:
:let #a = "%s///\n%s///"
Then, execute via :#a
Depending on your Vim configuration, the register contents may persist across sessions (cp. :help viminfo), but you must be careful to not override them.
scripts
A longer-lasting and more scalable solution is putting the commands into a separate script file, ideally with a .vim extension. You can put them anywhere (except ~/.vim/plugin, unless you want them executed automatically), and then run them via
:source /path/to/script.vim
custom commands
If you need a set of commands very often, you can give them a name and turn them into a custom command:
:command! MyCommands %s/// | %s///
Put this into your ~/.vimrc, or (if it's filetype-specific), make it a :help ftplugin.
Normally, you can run vim command from the shell using
vim -c YourCommandHere
In Vim,
I can open Vim Ex command Window with q:
It seems to me all the commands are not shared in other instance of Vim.
e.g. I have other Vim instance in a second console.
I'm wondering whether I can share all the Ex commands that I have typed for last ten days
No, command history is not shared. Histories (as well as marks, jumps, opened files) are stored in a viminfo file. The contents are written when quitting Vim, and read during startup.
But (and here comes the interesting part), you can manually trigger these with the :rv[iminfo] and :wv[iminfo] commands. So, first execute :wv in the one Vim instance to export its history, then import via :rv in the second instance. This could even be automated via :autocmds.
Due to some complicated environmental variables required, I have chosen to run Make through GNU screen. Using the screen vim plugin, I have the following setup in my .vimrc:
map <Leader>mm :call ScreenShellSend("cd ".expand("%:p:h")." && make 2>&1 | tee /path/to/errorfile") <CR>
Roughly translated, this will run make in the current working directory through an existing screen session with all of the required environment variables preset. I can then see the output of that command in a separate terminal window.
My question is, assuming I output the results of make to a text file, how do I tell automate the vim make process to:
A.) set make to use a vimscript function, i.e. call SreenShellSend() instead of an external program.
B.) set errorfile to /path/to/errorfile
Unfortunately, you cannot set 'makeprg' to a Vim function or command, it has to be a shell program. (I wish for an enhancement that treats commands starting with a colon as Vim commands, similar to the :help exception in 'keywordprg', but haven't come around to implementing this.)
There are workarounds, though. One is to use vim --remote-send as 'makeprg' and use this other temporary Vim instance to call back into the original Vim. What I do though is overriding the :make command through the cmdalias.vim plugin for those buffers. That alias then simply :calls my function.
Once you're able to invoke a Vim function, setting the 'errorfile' is just a matter of putting a function wrapper around ScreenShellSend() and setting it in there.
I work on multiple MAC OS X systems, which do not save changes after log out. As you know VIM is on every new mac, just type in vim in the terminal. I always bring my vimrc file with me, and the problem is that every single time I start vim i have to load it with :so command.
I want to use the fact that vim is available on every unix, but I also want to take advantage of the nifty integration with the terminal for doing quick tests, I just switch back to the terminal, but for that I need to close vim. When I reopen it, I again have to load the vimrc. And I am a newb, I don't even have plugins yet...
I want to keep the integration with the terminal and only develop a super quick way of introducing my changes to vim. Think of the conditions as if though you are sitting on newly installed operating system.
Thanks !
Not directly to your question, but you can always invoke vim with -u, which will let you specify your vimrc file instead of launching vim and then running :so.
The default location for your .vimrc file is ~/.vimrc (on the mac, ~ is /Users/_you_, eg /Users/matt). If you can write your file there, it will be loaded when vim starts up every time.
The system vimrc file on the mac is at /usr/share/vim/vimrc, but it sounds like these systems are not under your control, so you won't be able to write that file. Have a look at: How can I override ~/.vim and ~/.vimrc paths (but no others) in vim?, which uses the -u option to change the path vim looks for plugins under. But, in all cases, you must either be able to write your .vimrc into your home directory (which it sounds like your system does not permit) or specify the path to it at runtime (as with the command-line option I mention above, or with the normal command :so which you're currently using).
Depending on the kind of testing you need to do, you can always run shell commands within vim, by using the ! in normal mode. For instance, I frequently make changes to a python file in a buffer, and then (in normal mode) run !nosetests within vim—that writes terminal output into a temporary buffer at the bottom, and doesn't require me to leave or suspend vim. I can review the output, and any key-press takes me back to my buffer.
I don't have any experience with Mac-Os terminal. However I think if you could cp your .vimrc file to your home directory. everytime you start vim, vim will load the .vimrc file from your home directory.
If you want to swtich back to terminal from vim to do some testing/execute some commands and back to vim. you could consider to:
open multiple terminal windows
try something like screen or tmux. personaly I am using tmux, and it's very nice.
try Conque Shell plugin: http://www.vim.org/scripts/script.php?script_id=2771 I have this plugin installed too.
type Ctrl-z in vim to back to terminal
If you want to sync your .vimrc on different machines, you could put your .vimrc file in
a scm repository like gitHub, bitbucket... (I perfer this option, since you could have different branches for different settings)
dropbox
I hope this helps.
How is it possible that your changes are not saved after you log out? What would be the point of such a machine? An internet kiosk in an airport? Do you log as a user without a "home" directory?
If you have a "home" directory, just create a blank ~/.vimrc and put your settings there.
If you don't have a "home" directory but you are able to write somewhere else, create a blank vimrc file where you can, write your settings there and learn this command by heart:
$ vim -u /path/to/your/vimrc
If you don't have a "home" directory and you are really sure that you can't save anything on these machines, put your settings in a file somewhere online, preferably a place under your control, and learn this command by heart:
$ vim -u http://domain.name/yourvimrc
If you are lucky, the command you use will be remembered by your shell for you and it will be easy to issue it again without much typing.
For running your tests, you can either:
Hit <C-z> to suspend Vim. You are back at the prompt from where you started Vim and you can do your thing. Type $ fg to go back to Vim.
Type :sh to launch a new shell from the current directory. To go back to Vim, type $ exit.
I frequently accidentally open a binary executable, i.e. "foo", when I mean to open the associated source code "foo.cpp". The root of the problem is that tab completion, i.e. :e fo<tab> typically lands on the binary instead of the source code.
Is there a way to get vim to only tab complete names of text files? Or alternatively, change the tab completion order?
Sometimes my hasty tab completion error happens outside of vim; for those cases, what is the best way to prevent vim from opening files that are not text?
Not exactly what you need, but I have something like this in my .vimrc
" ignore these files when completing names and in Ex
set wildignore=.svn,CVS,.git,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif,*.pdf,*.bak,*.beam
" set of file name suffixes that will be given a lower priority when it comes to matching wildcards
set suffixes+=.old
For tab completion outside of vim, that will depend on your shell. Most shells have some form of autocompletion support. In particular, Zsh has the ability to autocomplete e.g. remote hosts for ssh. I'm not a wizard with these things, but it would probably be relatively simple to get your shell to drop files with certain suffixes from the autocompletion list when the command you are typing starts with "vim".
A quick google search turn up this page, which has this:
# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
'*?.old' '*?.pro'
It should not be too difficult to modify this logic to get what you want (if you use Zsh).
Maybe you can find this useful:
set wildmenu
set wildmode=longest,list
(taken and using from How do I make vim do normal (bash like) tab completion for file names?)