Vim Tips Wiki
No edit summary
 
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1108
 
|id=1108
  +
|previous=1107
|title=Replace selected shell command with full path when editing scripts
 
  +
|next=1109
|created=January 16, 2006 16:35
+
|created=January 16, 2006
 
|complexity=basic
 
|complexity=basic
 
|author=l1th10n
 
|author=l1th10n
 
|version=5.7
 
|version=5.7
 
|rating=6/3
 
|rating=6/3
 
}}
|text=
 
I am in the situation where I write some bash scripts and sudoers file, but it was tiresome going to a bash shell to find the paths of certain commands, so I'd thought that I could use the 'which' command to return the path of the selected word, however, this would not work directly with the 'which' command. To get around this I instead used< drum roll>:
+
When writing bash scripts and sudoers files, it can be tedious to determine the paths of the commands. Here is a procedure to use the <tt>which</tt> command to return the path of the selected word:
   
  +
<pre>
!which $(cat) .
+
!which $(cat)
  +
</pre>
   
 
To convert a bash command to a fully-explicit bash command, the key sequence would be "w!which $(cat)". $(cat) simply uses the selected text from the pipe and uses this text as an argument to the 'which' command. This method is great for using commands that use arguments instead of stdin. However, as far as I know, it works only in bash.
   
 
This command could be mapped to a key sequence if required.
   
 
==Comments==
So convert a bash command to a fully-explicit bash command the key sequence would be "w!which $(cat)". $(cat) simply uses the selected text from the pipe and uses this text as an argument to the 'which' command. This method is great for using commands that use arguments instead of stdin., however, as far as I know it works only in bash, I don't know enough about other shells to know whether this will be effective or not.
 
 
For this to work with csh and tcsh you have to enclose the 'cat' command in backticks. So you have to use:
  +
<pre>
 
which `cat`
  +
</pre>
   
 
Also make sure your 'shellpipe' is set to "|&amp; tee". See {{help|'shellpipe'}}.
   
 
The backtick version also works with bash.
   
This command could be mapped to some key sequence if required, but there are a million other tips that show you how to do this.
 
 
 
}}
 
 
== Comments ==
 
 
For this to work with csh and tcsh you have to inclose the 'cat'
 
command in backticks. So you have to use:
 
 
which `cat`
 
 
Also make sure your 'shellpipe' is set to "|&amp; tee".
 
See :h 'shellpipe' for info about it.
 
 
( PS:
 
The backtick version also works with bash.)
 
 
'''Anonymous'''
 
, January 17, 2006 0:29
 
 
----
 
----
In fact, the $(cmd) notation is general for all bourne compatible shells.
+
In fact, the $(cmd) notation is general for all bourne compatible shells. But as bourne ''and'' csh-like shells accept the `cmd` notation, it is more general (although I prefer $(cmd) when dealing with bourne shells for it is recursive).
But as bourne AND csh-like shells accept the `cmd` notation, it is more general (although I prefer $(cmd) when dealing with bourne shells for it is recursive)
 
   
'''Anonymous'''
 
, January 17, 2006 2:19
 
 
----
 
----
 
This will also do the trick on Unix:
:echo globpath(substitute($PATH, ':', ',', 'g'), 'cat')
 
  +
<pre>
will also do the trick on unix.
 
 
:echo globpath(substitute($PATH, ':', ',', 'g'), 'cat')
  +
</pre>
   
On Windows, you will have to translate ';' (instead of ':') into ',', and replace 'cat' by 'cat.exe'.
+
On Windows, you will have to translate ';' (instead of ':') into ',', and replace 'cat' by 'cat.exe'.
   
These solutions will work even without 'which' installed on the system.
+
These solutions will work even without 'which' installed on the system. The path translation mechanism is wrapped in <tt>:SearchInPATH</tt> from {{script|id=229}}.
The path translation mecanism is wrapped in :SearchInPATH from [/scripts/script.php?script_id=229 vimscript&#35;229].
 
   
hermitte {at} free {dot} fr
 
, January 17, 2006 12:54
 
 
----
 
----
Simpler:
+
Simpler:
  +
<pre>
 
 
!xargs which
 
!xargs which
  +
</pre>
   
'''Anonymous'''
 
, January 18, 2006 8:44
 
 
----
 
----
I'm missing the point of the $(cat) notation. What is this doing?
+
I'm missing the point of the $(cat) notation. What is this doing?
 
To just get the full path to a command, you can do
 
   
 
To just get the full path to a command, you can do
:.!which cat
 
  +
<pre>
 
:.!which cat
  +
</pre>
   
 
This will insert the text "/usr/bin/cat" at the cursor.
 
This will insert the text "/usr/bin/cat" at the cursor.
   
dsm26--AT--excite.com
 
, January 18, 2006 9:23
 
 
----
 
----
dsm26: I believe the $() notation passes the currently highlighted word (in visual mode) to the shell.
+
I believe the $() notation passes the currently highlighted word (in visual mode) to the shell.
   
'''Anonymous'''
 
, February 8, 2006 15:18
 
 
----
 
----
<!-- parsed by vimtips.py in 0.596802 seconds-->
 

Revision as of 09:37, 24 December 2007

Tip 1108 Printable Monobook Previous Next

created January 16, 2006 · complexity basic · author l1th10n · version 5.7


When writing bash scripts and sudoers files, it can be tedious to determine the paths of the commands. Here is a procedure to use the which command to return the path of the selected word:

!which $(cat)

To convert a bash command to a fully-explicit bash command, the key sequence would be "w!which $(cat)". $(cat) simply uses the selected text from the pipe and uses this text as an argument to the 'which' command. This method is great for using commands that use arguments instead of stdin. However, as far as I know, it works only in bash.

This command could be mapped to a key sequence if required.

Comments

For this to work with csh and tcsh you have to enclose the 'cat' command in backticks. So you have to use:

which `cat`

Also make sure your 'shellpipe' is set to "|& tee". See :help 'shellpipe'.

The backtick version also works with bash.


In fact, the $(cmd) notation is general for all bourne compatible shells. But as bourne and csh-like shells accept the `cmd` notation, it is more general (although I prefer $(cmd) when dealing with bourne shells for it is recursive).


This will also do the trick on Unix:

:echo globpath(substitute($PATH, ':', ',', 'g'), 'cat')

On Windows, you will have to translate ';' (instead of ':') into ',', and replace 'cat' by 'cat.exe'.

These solutions will work even without 'which' installed on the system. The path translation mechanism is wrapped in :SearchInPATH from script#229.


Simpler:

!xargs which

I'm missing the point of the $(cat) notation. What is this doing?

To just get the full path to a command, you can do

:.!which cat

This will insert the text "/usr/bin/cat" at the cursor.


I believe the $() notation passes the currently highlighted word (in visual mode) to the shell.