by edit I mean the act of opening it, modifying it, and saving it.
I have some windows .reg files I want to edit from vim. It looks like I'm stuck at telling vim to use proper encoding when opening them:
commands like :e ++enc=latin-1 are not working.
What is the proper way to edit .reg files in windows gvim?
It works for me. My settings are:
filetype on
:echo &filetype &encoding &fileencoding &ff
registry utf-8 utf-16le dos
You either need to explictly specify the encoding:
:edit ++enc=utf-16le file.reg
or ensure that 'fileencodings' starts with ucs-bom, and you use a Unicode encoding (you'll probably need the latter also for the previous method to correctly see all characters):
:set encoding=utf-8
:set fencs^=ucs-bom
I believe Windows uses a UTF-16 encoding, so try reloading the file with
:e! ++enc=utf-16le
Related
I have a config file under my python project, called "logging.conf", the file looks like:
[formatters]
keys: console, logging
[formatter_console]
format: %(asctime)s | %(message)s
[formatter_logging]
format: %(message)s
etc etc etc
Tried :syntax on, nothing happened, the .conf files look very plain. Is there anyway I can turn on some syntax to make the .conf file more colorful and readable?
You can check vim.org or the Internet for a suitable syntax.
As a first approximation, this somewhat looks like DOS / Windows INI files. Vim comes with a syntax for them; try
:setf dosini
If that suits you, you can add a filetype detection rule:
:autocmd BufRead,BufNewFile logging.conf setf dosini
See :help ftdetect for details.
Your file looks plain, it is correct. ( I assume that you have already set conf as the filetype of your current buffer :set ft? to verify).
if you check your $VIMRUNTIME/syntax/conf.vim
you will see, there are three different colors will be shown in a conf file:
hi comment, lines starting with #
hi string, text wrapped by ' or "
and normal text
your current text has no comment, no quoted text. so it shows just in one color.
The file you show (python conf) is actually ini structure. try Ingo's answer.
I almost have that kind of text style but mine was the ansible hosts file, I found a lot of options here filetype.vim. I used povini or texmf for my ansible hosts file text highlights
To configure it in your .vimrc.
Grab the string before setf, so in case of provini, it is au BufNewFile,BufRead .povrayrc
Replace the last string(.povrayrc) with *.conf
The final config is au BufNewFile,BufRead *.conf for your .vimrc
The key issue here is that the syntax in your conf file is usually associated with .ini files. Setting the type (ingo's answer) fixes this issue. Alternatively you could just rename your file.
mv logging.conf logging.ini
echo "syntax on" >> ~/.vimrc
Setting .ini as the file extension maybe more desirable than having custom rules. (I do understand that this may not be possible in all cases)
Adding set listchars=tab:▸\ ,trail:·,extends:#,nbsp:· line is causing "CONVERSION ERROR" while saving the vimrc file.
I am using gvim 7.3 under Window 7.
I have added this character using ("▸") using ctrl-vu 25B8.
my vimrc file has also set encoding=utf-8
Is it possible to resolve this error.
Thanks
Try saving with :w ++enc=utf-8. Also, it's recommended to put scriptencoding utf-8 at the top of your .vimrc.
I solved this error (although not from listchars) by changing the buffer's encoding and then saving:
:set fileencoding=utf-8
:write
Got the answer from the link below, As I need to put a single space after ▸\ charter
Vim doesn't support unicode?
I usually have to read .txt files with long lines, and at the same time edit some source file, and I like to see word wrap on the .txt files, and not in the ones that aren't.
Of course I can :set wrap and :set linebreak, but is there any way to make it automatucally, and dependent of the file extension?
There are two options that I can think of. Firstly, you can use an autocmd as suggested by Tassos:
:au BufNewFile,BufRead *.txt set wrap
See:
:help autocmd
An alternative (that is probably more applicable if you've got multiple settings as you have suggested): create a file in the after/ftplugin directory of your vim configuration folder (see below) called txt.vim and it will be sourced whenever you open a .txt file. You can put it in the plain ftplugin directory (rather than after/ftplugin), but it any built-in settings for .txt files will then not be loaded.
Put any commands you want in this file:
" This is txt.vim in the ftplugin directory
set wrap
set linebreak
See:
:help after-directory
:help ftplugin
Vim Configuration Folder
On Windows this would typically be something like:
C:\Documents and Settings\%USERNAME%\vimfiles\after\ftplugin\txt.vim
(I think), or
C:\Program Files\Vim\vimfiles\after\ftplugin\txt.vim
or even:
C:\vim\vimfiles\after\ftplugin\txt.vim
On Linux, it is:
~/.vim/after/ftplugin/txt.vim
For more info, see:
:help runtimepath
I guess :autocmd BufNewFile,BufRead *.txt set wrap should do the trick
you can do lot more with autocommand, refer here: http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/
I'm having a headache trying to figure out why vim isn't copying to a system buffer.
Here's my workflow:
vim asd
y1y
:q
vim qwe
p
On computerA and computerB, this works as I want it to: the line yanked from the file asd is put into the file qwe.
On computerC, this doesn't work.
All systems are running Ubuntu 8.04. computerA has the vim-full package installed, computerB and computerC have the vim package installed. computerA has xorg installed, is using the fluxbox window manager, and is accessed locally. computerB and computerC don't have X, and I'm sshing into both of them.
I've done a lot of reading and thought it was because computerC was compiled with -clipboard, but I ran vim --version on all three computers and only computerA was compiled with +clipboard.
Am I missing something obvious? I believe the user's .vimrc and the global vimrc files are the same. I can post output of vim --version and contents of vimrc files if that would help.
Vim by default doesn't copy to a system buffer. The only way that it would remember the contents is if the multiple instances of vim use the same .viminfo file. It's possible that the .viminfo file isn't being written due to file permissions or due to a different setting in 'viminfo' (the option).
For more information on the viminfo configuration, see
:help 'viminfo'
To look at your current configuration on each computer, do:
:set viminfo?
As an aside, if you want to use the system clipboard (which must be present, so you'd need to do ssh -X), you can use:
:set clipboard+=unnamed
Then all copy and paste operations will use the X11 selection buffer. Of course, you need vim compiled with +clipboard for this to work, so it won't solve your immediate problem. See:
:help 'clipboard'
for more information.
Perhaps you could post the result of the following on each computer?
:set viminfo?
:set clipboard?
This would help us to diagnose the problem in more detail. Could you also try:
vim asd
"ayy
:q
vim qwe
"ap
This will use register a instead of the unnamed register.
Why not use "+y and "+p, they work directly with the system's clipboard buffer
vim asd
"+y
:q
vim qwe
"+p
Take a look in .viminfo
if the last thing you yanked was hi it should contain:
Registers:
""0 LINE 0
hi
Maybe the file permissions are messed up?
It doesn't exactly solve your problem, but a perfectly fine workaround would be
vim asd
y1y
:sp qwe
p
:q
Which will open asd and yank from it (obviously), then split-open qwe, put your just-yanked item, and close the split-opened file. With this approach, there's no need to close the document you're working on a start a new instance of Vim.
And if you don't like the horizontal split, :vsp makes a vertical split, which can sometimes be easier to read/use.
OK, I don't know about the system buffer issue, but what about simply opening the new file with :e filename, then pasting, then saving and :e #ing your way back to the original file if needed? Yeah I know, tabs and splits are cool, but simple can do the job too.
You could also use ex, either from the target file to read the content to be included via the :r command supplying a line range, or from the source file to append the selected text (either via simple line number/range or the almighty g command) with :w >> filename.
Try this:
set clipboard=unnamed
It allows copying and pasting through the system clipboard.
So I work in a PHP shop, and we all use different editors, and we all have to work on Windows. I use vim, and everyone in the shop keeps complaining that whenever I edit a file there is a newline at the bottom. I've searched around and found that this is a documented behavior of vi & vim... but I was wondering if there was some way to disable this feature. (It would be best if I could disable it for specific file extensions).
If anyone knows about this, that would be great!
And for vim 7.4+ you can use (preferably on your .vimrc) (thanks to 罗泽轩 for that last bit of news!):
:set nofixendofline
Now regarding older versions of vim.
Even if the file was already saved with new lines at the end:
vim -b file
and once in vim:
:set noeol
:wq
done.
alternatively you can open files in vim with :e ++bin file
Yet another alternative:
:set binary
:set noeol
:wq
see more details at Why do I need vim in binary mode for 'noeol' to work?
Add the following command to your .vimrc to turn of the end-of-line option:
autocmd FileType php setlocal noeol binary fileformat=dos
However, PHP itself will ignore that last end-of-line - it shouldn't be an issue. I am almost certain that in your case there is something else which is adding the last newline character, or possibly there is a mixup with windows/unix line ending types (\n or \r\n, etc).
Update:
An alternative solution might be to just add this line to your .vimrc:
set fileformats+=dos
There is another way to approach this if you are using Git for source control. Inspired by an answer here, I wrote my own filter for use in a gitattributes file.
To install this filter, save it as noeol_filter somewhere in your $PATH, make it executable, and run the following commands:
git config --global filter.noeol.clean noeol_filter
git config --global filter.noeol.smudge cat
To start using the filter only for yourself, put the following line in your $GIT_DIR/info/attributes:
*.php filter=noeol
This will make sure you do not commit any newline at eof in a .php file, no matter what Vim does.
And now, the script itself:
#!/usr/bin/python
# a filter that strips newline from last line of its stdin
# if the last line is empty, leave it as-is, to make the operation idempotent
# inspired by: https://stackoverflow.com/questions/1654021/how-can-i-delete-a-newline-if-it-is-the-last-character-in-a-file/1663283#1663283
import sys
if __name__ == '__main__':
try:
pline = sys.stdin.next()
except StopIteration:
# no input, nothing to do
sys.exit(0)
# spit out all but the last line
for line in sys.stdin:
sys.stdout.write(pline)
pline = line
# strip newline from last line before spitting it out
if len(pline) > 2 and pline.endswith("\r\n"):
sys.stdout.write(pline[:-2])
elif len(pline) > 1 and pline.endswith("\n"):
sys.stdout.write(pline[:-1])
else:
sys.stdout.write(pline)
I have not tried this option, but the following information is given in the vim help system (i.e. help eol):
'endofline' 'eol' boolean (default on)
local to buffer
{not in Vi}
When writing a file and this option is off and the 'binary' option
is on, no <EOL> will be written for the last line in the file. This
option is automatically set when starting to edit a new file, unless
the file does not have an <EOL> for the last line in the file, in
which case it is reset.
Normally you don't have to set or
reset this option. When 'binary' is
off the value is not used when writing
the file. When 'binary' is on it is
used to remember the presence of a
for the last line in the file,
so that when you write the file the
situation from the original file can
be kept. But you can change it if you
want to.
You may be interested in the answer to a previous question as well: "Why should files end with a newline".
I've added a tip on the Vim wiki for a similar (though different) problem:
http://vim.wikia.com/wiki/Do_not_auto-add_a_newline_at_EOF
OK, you being on Windows complicates things ;)
As the 'binary' option resets the 'fileformat' option (and writing with 'binary' set always writes with unix line endings), let's take out the big hammer and do it externally!
How about defining an autocommand (:help autocommand) for the BufWritePost event? This autocommand is executed after every time you write a whole buffer. In this autocommand call a small external tool (php, perl or whatever script) that strips off the last newline of the just written file.
So this would look something like this and would go into your .vimrc file:
autocmd! "Remove all autocmds (for current group), see below"
autocmd BufWritePost *.php !your-script <afile>
Be sure to read the whole vim documentation about autocommands if this is your first time dealing with autocommands. There are some caveats, e.g. it's recommended to remove all autocmds in your .vimrc in case your .vimrc might get sourced multiple times.
I've implemented Blixtor's suggestions with Perl and Python post-processing, either running inside Vim (if it is compiled with such language support), or via an external Perl script. It's available as the PreserveNoEOL plugin on vim.org.
Starting with vim v7.4 you can use
:set nofixendofline
There is some information about that change here: http://ftp.vim.org/vim/patches/7.4/7.4.785 .
Maybe you could look at why they are complaining. If a php file has a newline after the ending ?>, php will output it as part of the page. This is not a problem unless you try to send headers after the file is included.
However, the ?> at the end of a php file is optional. No ending ?>, no problem with a newline at the end of the file.
Try to add in .vimrc
set binary
I think I've found a better solution than the accepted answer. The alternative solutions weren't working for me and I didn't want to have to work in binary mode all the time.
Fortunately this seems to get the job done and I haven't encountered any nasty side-effects yet: preserve missing end-of-line at end of text files. I just added the whole thing to my ~/.vimrc.
Would it be possible for you to use a special command for saving these files?
If you do :set binary, :w and :set nobinary the file will be written without newline if there was none to start with.
This sequence of commands could be put into a user defined command or a mapping, of course.
I found this vimscript plugin is helpful for this situation.
Plugin 'vim-scripts/PreserveNoEOL'
Or read more at github