I'm usually working with a file list for sorting/deleting/etc files, and I like to sometimes run arbitrary command on those files (which have either a full path or already present in current directory).

Now, I do know i can always make a mapping with visual selection, that would then just select current line and use it with [command here], but i was wondering if there was a better way, especially since other things like <cWORD> don't need visual selection, but alas only work on current word.

.w !commandhere

This work fine for things like bash but, if I need the input to be in front, then it (obviously) doesn't work. I recall managing to do this using:

.w !commandhere %

But that didn't work here. (edit: Nevermind on this part, just learned that % is actually the currently opened file's filename...)

Here I tried a couple things with getline:

.w !commandhere execute getline('.')

This doesn't work, but:

execute getline('.')

This work in at least getting the actual string (in the message/error buffer I presume?).

Lastly, this one got closer to the solution:

exec '!commandhere '.getline('.')

But still not it yet. (guessing this isn't working because of space/parentheses in the filenames)

I already know how to make it as a mapping. Just wondering if there other ways to do this without using visual selection, for the current line (where the blinking cursor is).

1 Answer 1


You're close!

You can use shellescape() to quote the argument to pass it to the shell, which will take care of spaces and any shell metacharacters that might be present in the line.

exec '!commandhere '.shellescape(getline('.'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.