Save file In a different folder in vim - windows

I'm working with several files in gvim in Windows 7. I need to test the files (Python scripts) in linux. So apart from their original location I want to also save the files in a folder called linux. I want to do this with new files that I will be creating/modifying. That's why I want to use a mapping with the % sign to get the name of the current file Into the new path.
The problem I'm having is that the % sign is escaped with a backslash, so this doesn't work :
:w C:\projects\linux\%:t
Being the original location:
C:\projects\foo\
Is there a simple way to just save the current file in a different folder? (I have read that the % sign is a filename character, so I could erase it from the string isfname and it should work but I think I am making it more complicated than what it really is.)

Sorry, late to the party, but you could also use the workaround
:exe 'w C:\projects\linux\' . expand('%:t')

My recollection is that you can escape the backslash by doubling it (but I'm not on Windows at present so I can't confirm it immediately). You don't need to escape them all, just the one which is causing trouble:
:w C:\projects\linux\\%:t

You might be able to do this sort of thing fairly automatically using the autocmd feature.
The following (untested) line in your platform's equivalent of ~/.vimrc will update a copy of a file when gvim makes modifications:
" clear commands
autocmd!
" when writing buffers, save a copy -- see :help filename-modifiers
autocmd BufWritePost c:/path/to/source/directory w %:t
The :t will take just the tail of the pathname; if you're working with multi-level directories, perhaps :p:. would be better. See the documentation for more details.

If you change the last backslash to a forward slash it will work:
:w C:\projects\linux/%:t

Related

how to use quote marks in a for loop in windows command line

Beginner's question here so I'd be grateful for a baby explanation.
I'm trying to create a concatenation text file that lists the files (with paths) in a certain folder, with the word "file" appended to the beginning of each line, as well as quotation marks. I want the text file to look like this:
file 'file:DriveLetter:\path\filename1.mp3'
file 'file:DriveLetter:\path\filename2.mp3'
etc
The command I'm running is as follows:
(for %i in (*.mp3) do #echo file 'file:%cd%\%i') > mylist.txt
But I receive the following error
%i') was unexpected at this time.
However, if I use double quotes instead of single, the command works. But this causes problems in my next step, which is to use ffmpeg to concatenate the files - it refuses to read the double quote marks.
Any advice is much appreciated. I'm open to an alternative method.
I just tried and everything is working fine. I believe that some character has been altered. Are you sure you're typing your command in your command prompt instead of using some editor (which does automatic modifications) and copying/pasting into a command prompt afterwards?

vimdiff E97 in Powershell

I am having trouble getting vimdiff to work on a Windows 10 machine. I am running vim from Powershell. Powershell is also declared in $myvimrc as my shell of choice:
set shell=C:\WINDOWS\system32\WindowsPowershell\v1.0\powershell.exe
The documents I am attempting to compare are not saved as files. I open two vertical splits, enter text into each, and run :windo diffthis. The output is E97: Cannot create diffs.
This article says I may need to download and use a different diff.exe than the one installed with gvim. I have downloaded the recommended "GnuWin32 diff" package and added the install directory to my Windows Path ($env:path). Continuing to follow these directions, I've commented out the default diffexpr declaration, but still get E97.
I have also tried calling my own function to no avail. In this attempt, I've made sure to escape backslashes, and have also copied the downloaded diff.exe to a directory I am confident I have full permissions to. To help with troubleshooting, I've temporarily saved the two files I wish to compare, and specified their full paths explicitly rather than using vim's v:fname_in and v:fname_new (and v:fname_out).
set diffexpr=TestDiff()
function TestDiff()
silent execute "!& " . "C:\\diff.exe" . " -a --binary " "C:\\a.edi" . " " . "C:\\b.edi" . " > " . "C:\\tmp.txt"
endfunction
I've researched this error by running :h E97, which returns the following information:
Vim will do a test if the diff output looks alright. If it doesn't,
you will get an error message. Possible causes:
The "diff" program cannot be executed.
The "diff" program doesn't produce normal "ed" style diffs (see above).
The 'shell' and associated options are not set correctly. Try if filtering works with a command like ":!sort".
You are using 'diffexpr' and it doesn't work. If it's not clear what the problem is set the 'verbose' option to one or more to see
more messages.
The self-installing Vim for MS-Windows includes a diff program. If
you don't have it you might want to download a diff.exe. For example
from http://gnuwin32.sourceforge.net/packages/diffutils.htm.
I believe that I pass the first two of these requirements, as tmp.txt is generated and follows the example "ed" style diff that is provided in the help file. I have not set other shell-related parameters (shelltype, shellpipe, etc) in $myvimrc, but executing other commands with :! complete without issue.
There is no additional information in :messages, only the E97 error.
Edit:
If I remove set shell=C:\WINDOWS\system32\WindowsPowershell\v1.0\powershell.exe from $myvimrc, defaulting the shell back to cmd.exe, diffthis works as expected. It seems that others have also had this problem when using Powershell.
Below are captures of the command windows that pop up when running diffthis with both shells. The commands used are nearly identical.
I had speculated that the forward slashes apparent in the Powershell version of this attempt were problematic, but I am able to run this command exactly (with dummy files in place of the .tmp files) and it outputs what seems to be an adequate file for use with diff. I've also tried adding a substitute of forward slashes to back slashes to no avail.
The problem boils down to how vim/Powershell are A) encapsulating the command in quotes and B) handling white space in path names.
I am able to bypass this problem with the following changes to $myvimrc:
set shell=powershell
set shellcmdflag=-c
set shellquote="
set shellxquote=
Also a change in the default MyDiff() function within the if that sets the path to diff in the cmd variable:
" below is the default
" let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
let cmd = "C:/diff"
This approach is dependent upon copying the diff.exe that ships with vim to a directory that doesn't contain spaces in the path for simplicity (C:\diff.exe).
The resulting command that is executed is:
powershell -c C:/diff -a --binary C:/<redacted>/a.tmp C:/<redacted>/b.tmp > C:/<redacted>/c.tmp

How can I remove the last n characters of filenames in a certain directory (in Mac terminal)- unix?

I am trying to rename using "mv", because "rename" command doesn't work in my Mac terminal.
I have a bunch of files named
DTM001_ACGGT-TTAGGC.fq
DTM156_GGTTG-ACAGTG.fq
...etc
I wish to rename them to
DTM001.fq
DTM156.fq
I suppose the easier way is to remove the last 13 characters before the file extension?
I tried these links:
mac os x terminal batch rename
Rename file by removing last n characters
Removing last n characters from Unix Filename before the extension
but none have worked for me, perhaps because I do not fully understand how to manipulate the answers for my specific case or some answers use "rename" command which I cannot access.
The macOS Terminal is simply an interface to an interactive program called a shell. The default shell's name is bash.
What you are looking for is known as a shell script, or a bash script, to rename files.
The questions you referenced have the answer. To reiterate:
cd directory_with_the_files
for file in *.fq; do
mv -vn "${file}" "${file%_*}.fq"
done
You can type this all in at the command line, or place it into a file and execute it with:
bash file_containing_the_commands
This will go through all .fq files in the current directory, renaming them to what you want. The -v option to mv simply means to print the rename as it happens (useful to know that it's doing something), and the -n flag means don't accidentally overwrite any files (in case you type something in wrong or come across duplicate numbers).
All the magic is happening in the ${file%_*}.fq, which says
"remove everything after the first _ and add the .fq back". This is known as a "shell parameter expansion," which you can read more about in the Bash Reference Manual. It's somewhat obtusely worded, but here is the relevant bit to this particular use case:
${parameter%word}
The word is expanded to produce a
pattern just as in filename expansion. If the pattern matches a
trailing portion of the expanded value of parameter, then the result
of the expansion is the value of parameter with the shortest matching
pattern (the '%' case) deleted.
The simplest way is to use rename - see instructions at the end for installation on a Mac.
So, in answer to your question, you can see what would happen if you replace (the command is actually s for "substitute") everything from the first underscore to the end of the filename with .fq:
rename --dry-run 's/_.*/.fq/' *fq
'DTM001_ACGGT-TTAGGC.fq' would be renamed to 'DTM001.fq'
'DTM156_GGTTG-ACAGTG.fq' would be renamed to 'DTM156.fq'
If that looks good, remove the --dry-run and run it again for real.
You can use rename on your Mac, if you install it. By default, Apple doesn't ship a package manager with macOS. So, many folk use homebrew from the homebrew website.
If you have that, you can simply install rename with:
brew install rename
Then, you'll have a package manager and you can benefit for all sorts of lovely software including new, up-to-date versions of all the out-of-date, ancient versions of your favourite tools that Apple ships:
PHP
Perl
ImageMagick
GNU sed
GNU awk
GNU find
GNU Parallel
zeromq
htop
socat
sox
ffmpeg
youtube-dl
zenity
redis
feh
mosquitto
doxygen
pandoc etc.

vim temporary files with native vim on windows

When running a native compilation of gVim under Win7 I have the following in my vimrc:
if has ("win32")
let $TMP="C:/tmp"
setlocal equalprg=tidy\ --output-xhtml\ y\ -utf8\ --wrap-attributes\ 1\ --vertical-space\ 1\ --indent\ auto\ --wrap\ 0\ --show-body-only\ auto\ --preserve-entities\ 1\ -q\ -f\ shellpipe=2>
endif
This should create a temp file. However, after running the command, I see:
shell returned 1
E485: Can't read file C:\tmp\VIoC935.tmp
The common recommendation for native windows E485 errors is to set the tmp variable, which I have, as you can see from my vimrc snippet. If I remove the let statement, I get a similar result:
shell returned 1
E485: Can't read file C:\Users\ksk\AppData\Local\Temp\VIfFA01.tmp
In both cases; both directories exist and gVim can write a file to those locations, i.e.,
:w C:\Users\ksk\AppData\Local\Temp\VIfFA01.tmp
in the current buffer will write this file without error.
Interestingly, while writing this, I found that if I create a new buffer, and delete the original buffer, the equalprg function runs without error (with and without the "let" statement in vimrc)
This doesn't apply to the original question, but I ran into the same symptoms because of the setting of my SHELL variable within gvim. I was launching gvim from a Cygwin window, and it picked up the SHELL setting from within cygwin: /bin/bash. I had to do a ":set SHELL=C:/cygwin/bin/bash" from within gvim, and then the temporary file problem went away. So check the setting of your SHELL variable within gVim (via ":set shell") when trying to troubleshoot this kind of problem.
Interestingly, while writing this, I found that if I create a new buffer, and delete the original buffer, the equalprg function runs without error (with and without the "let" statement in vimrc)
That's probably because you use setlocal in your vimrc
E485: Can't read file C:\tmp\VIoC935.tmp
I know the reason, but I don't know how to fix it without changing indentation options. The issue is caused by the '>' symbol of your indent command, it should work after removing the last part of it or even only the '>' symbol. It's so because '>' has a special meaning in shell commands. In *nix one can probably just escape it, but this doesn't work for me in Windows.
P.S. I know this is not a full answer, but maybe it will help you solve the issue.
Update. Small discussion in the comments discovered that the correct way of escaping is to enclose the last argument in double quotes (the variant 2) below). So the working command is:
setlocal equalprg=tidy\ --output-xhtml\ y\ -utf8\ --wrap-attributes\ 1\ --vertical-space\ 1\ --indent\ auto\ --wrap\ 0\ --show-body-only\ auto\ --preserve-entities\ 1\ -q\ -f\ "shellpipe=2>"

Syntax highlighting in Bash vi-input mode

If I enable bash's input mode using set -o vi, then press Esc followed by v, I get a vi window which allows me to edit a temporary file which is executed once I leave. In that window I would like to enjoy Vim syntax highlighting for Bash scripts. It doesn't suffice to execute :syntax enable. The problem might be related to the fact that the temporary file has no .sh ending nor a #!/bin/bash head which could be used to determine the filetype.
I'd use the shorter formulation:
au BufRead,BufNewFile bash-fc-* set filetype=sh
I believe this type of autocmd is the canonical way to handle filetype assignments (at least, my .vimrc has a number of them).
#Eric Fortis, please chime in or correct me if there's a reason you did it differently.
Add this to your .vimrc
if expand('%:t') =~?'bash-fc-\d\+'
setfiletype sh
endif
the temporary files are of the form bash-fc-3537253897, so the regex matches if the file begins with bash-fc- and applies the filetype.

Resources